Class: ActiveRecord::Zuul::Generators::SubjectGenerator

Inherits:
Generators::Base
  • Object
show all
Includes:
Zuul::Generators::OrmHelpers
Defined in:
lib/generators/zuul/subject_generator.rb

Instance Method Summary collapse

Methods included from Zuul::Generators::OrmHelpers

#migration_exists?, #migration_path, #model_exists?, #model_path

Instance Method Details

#generate_modelObject



15
16
17
# File 'lib/generators/zuul/subject_generator.rb', line 15

def generate_model
  invoke "active_record:model", [name].concat(attributes.map {|attr| "#{attr.name}:#{attr.type}" }), :migration => true unless model_exists? && behavior == :invoke
end

#inject_zuul_contentObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/zuul/subject_generator.rb', line 19

def inject_zuul_content
  content = <<CONTENT
  # Setup authorization for your subject model
  acts_as_authorization_subject
CONTENT

  class_path = if namespaced?
    class_name.to_s.split("::")
  else
    [class_name]
  end

  indent_depth = class_path.size - 1
  content = content.split("\n").map { |line| "  " * indent_depth + line } .join("\n") << "\n"

  inject_into_class(model_path, class_path.last, content) if model_exists?
end