Class: Everything::Scaffold

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/thesilverspoon.rb

Overview

If you use the NamedBase inheritance, a ‘name’ parameter has to follow the ‘rails g integratedscaffold’. Won’t work otherwise. If you don’t want this, use ::Base

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Scaffold

Returns a new instance of Scaffold.



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

def initialize(*args, &block)

  # This is a standard initializer

  super # Call parent class
  @model_attributes = [] # Initialize empty array for 'thor' attributes

  # This method splits the name:type arguments in the parameters list
  g_parameters.each do |arg|
    @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
  end

  # Creates a list of all the standard actions in the controller
  @controller_actions=all_actions

end

Class Method Details

.source_rootObject

this sets the path where the code looks for templates within the gem



16
17
18
# File 'lib/thesilverspoon.rb', line 16

def self.source_root
  File.expand_path("../templates", __FILE__)
end

Instance Method Details

#create_controllerObject



45
46
47
48
49
# File 'lib/thesilverspoon.rb', line 45

def create_controller

  # creates the controller using the 'controller.rb' template and puts it into the rails app
  template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
end

#create_helperObject



58
59
60
61
62
# File 'lib/thesilverspoon.rb', line 58

def create_helper

  # creates the helper using the 'helper.rb' template and puts it into the rails app
  template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
end

#create_migrationObject



51
52
53
54
55
56
# File 'lib/thesilverspoon.rb', line 51

def create_migration

  # creates the migration file using the 'migration.rb' template and puts it into the rails app with a time stamp
  template 'migration.rb', "db/migrate/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_create_#{model_path.pluralize.gsub('/', '_')}.rb"

end

#create_modelObject



38
39
40
41
42
43
# File 'lib/thesilverspoon.rb', line 38

def create_model

  # creates the model using the 'model.rb' template and puts it into the rails app
  template 'model.rb', "app/models/#{model_path}.rb"

end

#create_routesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/thesilverspoon.rb', line 64

def create_routes

  # adds the routes for the gem into the routes file
  namespaces = plural_name.split('/')
  resource = namespaces.pop
  route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
    "namespace(:#{namespace}){ #{acc} }"
  }

  s=%{match "#{resource}_integrated_view"=>"#{resource}#integrated_view"\n}

  inject_into_file "config/routes.rb", s, :after=>"resources :#{resource}\n"

end

#create_viewsObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/thesilverspoon.rb', line 80

def create_views

  # creates the standard views using the '[action].html.erb' templates and puts it into the rails app
  controller_actions.each do |action|
    if action!="create" and action!="update" and action!="destroy" and action!="parse_save_from_excel"
      template "views/erb/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
    end
  end
  if form_partial?
    template "views/erb/_form.html.erb", "app/views/#{plural_name}/_form.html.erb"
  end
end