Class: Para::ResourceGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/para/resource/resource_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_resource_to_component_controllerObject



21
22
23
24
25
26
27
28
# File 'lib/generators/para/resource/resource_generator.rb', line 21

def add_resource_to_component_controller
  gsub_file "app/controllers/admin/#{ component_name.singularize }_component_controller.rb", /# You can access @component here/ do
    <<-RUBY
      @q = @component.#{ plural_file_name }.ransack(params[:q])
      @resources = @q.result.page(params[:page])
    RUBY
  end
end

#copy_resource_controllerObject



17
18
19
# File 'lib/generators/para/resource/resource_generator.rb', line 17

def copy_resource_controller
  template 'resource_controller.rb', "app/controllers/admin/#{ plural_file_name }_controller.rb"
end

#generate_modelObject



42
43
44
45
46
47
48
# File 'lib/generators/para/resource/resource_generator.rb', line 42

def generate_model
  generate 'model',
    file_name,
    attributes.map { |attr|
      "#{ attr.name }:#{ attr.type }"
    }.insert(-1, 'component:references').join(' ')
end

#insert_belongs_to_to_resourceObject



58
59
60
61
62
# File 'lib/generators/para/resource/resource_generator.rb', line 58

def insert_belongs_to_to_resource
  inject_into_file "app/models/#{ file_name }.rb", after: "belongs_to :component" do
    ", class_name: 'Para::Component::Base'"
  end
end

#insert_has_many_to_componentObject



64
65
66
67
68
69
# File 'lib/generators/para/resource/resource_generator.rb', line 64

def insert_has_many_to_component
  inject_into_file "app/components/#{ component_name.underscore }_component.rb", after: "register :#{ component_name.underscore }, self" do
    "\n\n      has_many :#{ plural_file_name }, class_name: '::#{ class_name }', inverse_of: :component,
      foreign_key: :component_id, dependent: :destroy"
  end
end

#insert_relation_to_showObject



36
37
38
39
40
# File 'lib/generators/para/resource/resource_generator.rb', line 36

def insert_relation_to_show
  append_to_file "app/views/admin/#{ component_name.underscore }_component/show.html.haml" do
    "\n\n= listing_for(@resources)"
  end
end

#insert_routeObject



30
31
32
33
34
# File 'lib/generators/para/resource/resource_generator.rb', line 30

def insert_route
  inject_into_file 'config/routes.rb', after: "component :#{ component_name.underscore } do" do
    "\n      resources :#{ plural_file_name }"
  end
end

#migrateObject



54
55
56
# File 'lib/generators/para/resource/resource_generator.rb', line 54

def migrate
  rake 'db:migrate' if options[:migrate]
end

#orderableObject



50
51
52
# File 'lib/generators/para/resource/resource_generator.rb', line 50

def orderable
  generate 'para:orderable', file_name if options[:orderable]
end

#welcomeObject



13
14
15
# File 'lib/generators/para/resource/resource_generator.rb', line 13

def welcome
  say 'Creating resource...'
end