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

前端 未结 3 1998
面向向阳花
面向向阳花 2021-02-08 09:54

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:         


        
相关标签:
3条回答
  • 2021-02-08 10:19

    Your hunch is correct: you can put a module in the lib directory. In order to make these methods available to your models, simply include it with:

    class Model < ActiveRecord::Base
      include MyModule
    end
    

    The included module's instance methods will become instance methods on your class. (This is known as a mixin)

    module MyModule
      def S3
        #...
      end
    end
    
    0 讨论(0)
  • 2021-02-08 10:36

    You can write a module as :

    module MyModule
      def self.S3(args*)
        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
    

    and then call it in your controller or model as

    MyModule.S3(params*)

    0 讨论(0)
  • 2021-02-08 10:41

    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.

    0 讨论(0)
提交回复
热议问题