Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveSuppport:Concern and Private Methods

Tags:

This is a great idea about concern in rails: http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns

And it's also a good idea to make very small methods that are not part of a public API. Without using concerns, those become private methods in a ruby class.

Does it makes sense to create private methods inside of a Rails ActiveSupport::Concern module? If so, does private work both for regular instance methods and class methods in the concern definition?

like image 483
justingordon Avatar asked Feb 18 '13 01:02

justingordon


People also ask

What are private methods in Rails?

If a method is private, it may be called only within the context of the calling object---it is never possible to access another object instance's private methods directly, even if the object is of the same class as the caller. For protected methods, they are accessible from objects of the same class (or children).

What are rails concerns?

A Rails Concern is a module that extends the ActiveSupport::Concern module. Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them.

Does Ruby have private methods?

Understanding Private Methods in RubyYou can only use a private method by itself. It's the same method, but you have to call it like this. Private methods are always called within the context of self .

What is active support concern?

ActiveSupport's Concern module allows us to mix in callbacks, class and instance methods, and create associations on target objects. This module has an included method, which takes a block, as well as an append_features method and class_methods block, which you can read about in the source code.


1 Answers

Does it makes sense to create private methods inside of a Rails ActiveSupport::Concern module?

Considering that concerns are smart modules that will eventually be included in other classes — yes, it does. It's just a portable code, extractable behavior and I'd like to consider it as part of my controller (or model, etc.) as I'm writing it. So basically you just declare methods private or protected as you normally would.

Maybe the post you linked have been updated since 2013, but DHH does exactly that in the one of the examples there:

module Dropboxed   extend ActiveSupport::Concern    included do     before_create :generate_dropbox_key   end    def rekey_dropbox     generate_dropbox_key     save!   end    private # <- Let's list some privates    def generate_dropbox_key     self.dropbox_key = SignalId::Token.unique(24) do |key|        self.class.find_by_dropbox_key(key)     end   end end 

As to private class methods, I agree with @Hugo and never used them myself, but here's how you can achieve this:

module Dropboxed   extend ActiveSupport::Concern    included do     private_class_method :method_name   end    module ClassMethods     def method_name     end   end end 
like image 80
jibiel Avatar answered Sep 27 '22 23:09

jibiel