EppO/rolify — Issue #26: suggestion: move code injected into User to a method and inject the method instead
Author: hazah
Created: 2012-01-10 18:06:06+00:00
State: open

Maybe it's just me, but I've always favored the declarative approach rather then the procedural one when drawing up a specification. What are your thoughts in regards to moving the injected text out of the model and into its own method? That gives you two things, first, a cleaner User model :). Second, and I think more importantly, it allows you to have finer control over the monkey patching that takes place. Currently, since all the code is dumped into the user's lap, you have to be very careful by not adding anything to the scheme. Hiding it behind a method will give you more flexibility on how the User model will get augmented.

turn this:

```

inject_into_class(model_path, user_cname.camelize) do
" include Rolify::Roles\n" +
" #{'# ' if !options[:dynamic_shortcuts]}extend Rolify::Dynamic\n" +
" has_and_belongs_to_many :roles#{", :class_name => \"" + role_cname.camelize + "\"" if role_cname != "Role"}, :join_table => :#{user_cname.tableize + "_" + role_cname.tableize}\n"
end

```

into this:

```

inject_into_class(model_path, user_cname.camelize) do
"rolify"
end

def self.rolify option = { :role_cname => 'Role', :dynamic => false }

include Rolify::Roles
extend Rolify::Dynamic if options[:dynamic]
has_and_belongs_to_many :roles, :class_name => options[:role_cname].constantize, :join_table => :"#{self.class.to_s.tabletize}_#{options[:role_cname].tableize}"

end

```

Thoughts?