Class: Mongoid::Generators::RolifyGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Mongoid::Generators::RolifyGenerator
- Defined in:
- lib/generators/mongoid/rolify_generator.rb
Instance Method Summary collapse
- #generate_model ⇒ Object
- #inject_role_class ⇒ Object
- #model_contents ⇒ Object
- #model_path ⇒ Object
- #role_reference ⇒ Object
- #user_reference ⇒ Object
Instance Method Details
#generate_model ⇒ Object
11 12 13 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 11 def generate_model invoke "mongoid:model", [ name ] end |
#inject_role_class ⇒ Object
15 16 17 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 15 def inject_role_class inject_into_file(model_path, model_contents, :after => "include Mongoid::Document\n") end |
#model_contents ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 31 def model_contents content = <<RUBY has_and_belongs_to_many :%{user_cname} belongs_to :resource, :polymorphic => true field :name, :type => String index({ :name => 1, :resource_type => 1, :resource_id => 1 }, { :unique => true}) validates :resource_type, :inclusion => { :in => Rolify.resource_types }, :allow_nil => true scopify RUBY content % { :user_cname => user_cname.constantize.collection_name } end |
#model_path ⇒ Object
27 28 29 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 27 def model_path File.join("app", "models", "#{file_path}.rb") end |
#role_reference ⇒ Object
23 24 25 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 23 def role_reference class_name.demodulize.underscore end |
#user_reference ⇒ Object
19 20 21 |
# File 'lib/generators/mongoid/rolify_generator.rb', line 19 def user_reference user_cname.demodulize.underscore end |