Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a method available to both my controller and model in Rails?

I have a private method in my Rails app to connect to Amazon S3, execute a passed block of code, then close the connection to S3. It looks like so;

def S3
  AWS::S3::Base.establish_connection!(
    :access_key_id     => 'Not telling',
    :secret_access_key => 'Really not telling'
  )
  data = yield
  AWS::S3::Base.disconnect
  data
end

It is called like this (as an example);

send_data(S3 {AWS::S3::S3Object.value("#{@upload_file.name}",'bucket')}, :filename => @upload_file.name)

I call this method in a number of ways in my controller and model so have it included in both classes as a private method. This works fine and I'm happy with it but it's not very DRY.

How can I make this method accessible to both my model and controller but only have the code appear once? This is more of a Ruby question than a Rails question and reflects my newness to OOP. I'm guessing a module or a mix-in is the answer but I haven't really been using either of these up until now and need a little hand-holding.

Thanks.

like image 887
brad Avatar asked Aug 25 '10 00:08

brad


People also ask

Can we call controller method in model Rails?

As a design choice, its not recommended to call controller helpers from your models. You can just simply pass the required details to your model methods as arguments.

How do you pass a value from view to controller in Ruby on Rails?

To send data from view to controller you can use query string as you used in popular. html. erb and in answer controller you get data in params[:question_id] , use instance variable in new action as @question = params[:question_id] and in answer/new. html use hidden field f.


1 Answers

Modules are used for 3 different things in ruby. First is namespacing. Having class or constant definitions inside a module won't collide with classes or constants outside that module. Something like this

class Product
  def foo
    puts 'first'
  end
end

module Affiliate
  class Product
    puts 'second'
  end
end

p = Product.new
p.foo # => 'first'

p = Affiliate::Product.new
p.foo # => 'second'

The second use for modules is as a place to stick methods that don't really have a place anywhere else. You can do this inside a class too, but using a module sort of tells people reading the code that it is not meant to be instanciated. Something like this

module Foo
  def self.bar
    puts 'hi'
  end
end

Foo.bar #=> 'hi'

Finally (and the most confusing) is that modules can be included into other classes. Using them this way is also referred to as a mixin, because you are "mixing in" all the methods into whatever you are including.

module Foo
  def bar
    puts 'hi'
  end
end

class Baz
  include Foo
end

b = Baz.new
b.bar #=> 'hi'

Mixins are actually a way more complected topic then I am covering here, but going deeper would probably be confusing.

Now, to me, S3 seems to be something that really belongs in the controller, since controllers are usually the things dealing with incoming and outgoing connections. If that is the case, I would just have a protected method on application controller, since that will be accessible to all other controllers, but still be private.

If you do have a good reason for it being in the model too, I would go for a mixin. Something like

module AwsUtils
private
  def S3
    AWS::S3::Base.establish_connection!\
      :access_key_id     => 'Not telling',
      :secret_access_key => 'Really not telling'

    data = yield
    AWS::S3::Base.disconnect
    data
  end
end

If you put that in lib/aws_utils.rb, you should be able to use it by adding include AwsUtils in both your controller and your model. Rails knows to look for classes and modules in lib, but only if the name matches (in wide case). I called it AwsUtils because I know what rails will look for when it sees that (aws_utils.rb), and to be honest, I have no idea what it will need for S3Utils ;-)

Feel free to ask for more info if I wasn't clear on something. Modules tend to be one of those things in ruby that while amazing, are downright baffling to newcomers.

like image 163
Matt Briggs Avatar answered Oct 14 '22 09:10

Matt Briggs