Class: Mongoid::Generators::RolifyGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/mongoid/rolify_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_modelObject



11
12
13
# File 'lib/generators/mongoid/rolify_generator.rb', line 11

def generate_model
  invoke "mongoid:model", [ name ]
end

#inject_role_classObject



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_contentsObject



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_pathObject



27
28
29
# File 'lib/generators/mongoid/rolify_generator.rb', line 27

def model_path
  File.join("app", "models", "#{file_path}.rb")
end

#role_referenceObject



23
24
25
# File 'lib/generators/mongoid/rolify_generator.rb', line 23

def role_reference
  class_name.demodulize.underscore
end

#user_referenceObject



19
20
21
# File 'lib/generators/mongoid/rolify_generator.rb', line 19

def user_reference
  user_cname.demodulize.underscore
end