Class: AwesomeScaffoldGenerator
- Inherits:
-
Rails::Generator::NamedBase
- Object
- Rails::Generator::NamedBase
- AwesomeScaffoldGenerator
- Defined in:
- lib/awesome_scaffold_generator.rb
Overview
config.gem “thoughtbot-factory_girl”, :lib => ‘factory_girl’, :source => ‘gems.github.com’
Instance Attribute Summary collapse
-
#controller_class_name ⇒ Object
readonly
Returns the value of attribute controller_class_name.
-
#controller_class_nesting ⇒ Object
readonly
Returns the value of attribute controller_class_nesting.
-
#controller_class_nesting_depth ⇒ Object
readonly
Returns the value of attribute controller_class_nesting_depth.
-
#controller_class_path ⇒ Object
readonly
Returns the value of attribute controller_class_path.
-
#controller_file_path ⇒ Object
readonly
Returns the value of attribute controller_file_path.
-
#controller_name ⇒ Object
readonly
Returns the value of attribute controller_name.
-
#controller_plural_name ⇒ Object
(also: #controller_table_name)
readonly
Returns the value of attribute controller_plural_name.
-
#controller_singular_name ⇒ Object
(also: #controller_file_name)
readonly
Returns the value of attribute controller_singular_name.
-
#default_file_extension ⇒ Object
readonly
Returns the value of attribute default_file_extension.
-
#resource_edit_path ⇒ Object
readonly
Returns the value of attribute resource_edit_path.
Instance Method Summary collapse
-
#initialize(runtime_args, runtime_options = {}) ⇒ AwesomeScaffoldGenerator
constructor
A new instance of AwesomeScaffoldGenerator.
- #manifest ⇒ Object
Constructor Details
#initialize(runtime_args, runtime_options = {}) ⇒ AwesomeScaffoldGenerator
Returns a new instance of AwesomeScaffoldGenerator.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/awesome_scaffold_generator.rb', line 18 def initialize(runtime_args, = {}) super @controller_name = @name.pluralize base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name) if @controller_class_nesting.empty? @controller_class_name = @controller_class_name_without_nesting else @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" end @resource_generator = "awesome_scaffold" @default_file_extension = "html.haml" @resource_edit_path = "/edit" end |
Instance Attribute Details
#controller_class_name ⇒ Object (readonly)
Returns the value of attribute controller_class_name.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_class_name @controller_class_name end |
#controller_class_nesting ⇒ Object (readonly)
Returns the value of attribute controller_class_nesting.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_class_nesting @controller_class_nesting end |
#controller_class_nesting_depth ⇒ Object (readonly)
Returns the value of attribute controller_class_nesting_depth.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_class_nesting_depth @controller_class_nesting_depth end |
#controller_class_path ⇒ Object (readonly)
Returns the value of attribute controller_class_path.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_class_path @controller_class_path end |
#controller_file_path ⇒ Object (readonly)
Returns the value of attribute controller_file_path.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_file_path @controller_file_path end |
#controller_name ⇒ Object (readonly)
Returns the value of attribute controller_name.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_name @controller_name end |
#controller_plural_name ⇒ Object (readonly) Also known as: controller_table_name
Returns the value of attribute controller_plural_name.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_plural_name @controller_plural_name end |
#controller_singular_name ⇒ Object (readonly) Also known as: controller_file_name
Returns the value of attribute controller_singular_name.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def controller_singular_name @controller_singular_name end |
#default_file_extension ⇒ Object (readonly)
Returns the value of attribute default_file_extension.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def default_file_extension @default_file_extension end |
#resource_edit_path ⇒ Object (readonly)
Returns the value of attribute resource_edit_path.
5 6 7 |
# File 'lib/awesome_scaffold_generator.rb', line 5 def resource_edit_path @resource_edit_path end |
Instance Method Details
#manifest ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/awesome_scaffold_generator.rb', line 38 def manifest record do |m| #just so you can see what the variables are # => "yoda/bob" #p @name # yoda/bob #p @controller_name # yoda/bobs #p @controller_class_name_without_nesting # Bobs #p @controller_class_nesting # yoda #p @controller_plural_name #bobs #p @controller_singular_name #bobs #p @controller_file_path #yoda/bobs #p @controller_class_path # ["yoda"] # Check for class naming collisions. m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper") m.class_collisions(class_path, "#{class_name}") # Controller, helper, views, spec and cucumber directories. m.directory(File.join('app/models')) m.directory(File.join('app/controllers', controller_class_path)) m.directory(File.join('app/helpers', controller_class_path)) m.directory(File.join('app/views', controller_class_path, controller_file_name)) m.directory(File.join('spec/controllers', controller_class_path)) m.directory(File.join('spec/models')) m.directory(File.join('spec/helpers', class_path)) m.directory File.join('spec/fixtures') m.directory File.join('spec/views', controller_class_path, controller_file_name) # Controller spec, class, and helper. m.template 'awesome_scaffold:controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb") m.template "awesome_scaffold:controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") m.template 'awesome_scaffold:helper_spec.rb', File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb") m.template "#{@resource_generator}:helper.rb", File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb") for action in scaffold_views m.template( "awesome_scaffold:view_#{action}_haml.erb", File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}") ) end # Model class, unit test, and fixtures. m.template 'awesome_scaffold:model.rb', File.join('app/models', "#{@controller_singular_name.singularize}.rb") m.template 'model:fixtures.yml', File.join('spec/fixtures', "#{@controller_singular_name}.yml") m.template 'awesome_scaffold:model_spec.rb', File.join('spec/models', "#{@controller_singular_name.singularize}_spec.rb") # View specs m.template "awesome_scaffold:edit_haml_spec.rb", File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb") m.template "awesome_scaffold:index_haml_spec.rb", File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb") m.template "awesome_scaffold:new_haml_spec.rb", File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb") m.template "awesome_scaffold:show_haml_spec.rb", File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb") # Cucumber features m.template 'awesome_scaffold:controller.feature', File.join('features', "#{@controller_singular_name}.feature") # Factories.rb m.factories m.route_resources(name) # done m.puts "\n\e[32m" # green m.puts "AWESOME! everything worked." m.puts "\e[0m" # clear m.puts "Now put this in your paths.rb file (if you haven't already):\n" paths = "when /the (.+) list/\n" << " eval(\"\#{$1.pluralize}_path\")\n\n" << "when /show page for the (.+) named (.+)/\n" << " item = $1.singularize.capitalize.constantize.find_by_name($2)\n" << " eval(\"\#{$1.singularize}_path(\#{item.id})\")\n\n" << "when /new (.+) page/\n" << " eval(\"new_\#{$1.singularize}_path\")\n\n" << "when /edit page for the (.+) named (.+)/\n" << " item = $1.singularize.capitalize.constantize.find_by_name($2)\n" << " eval(\"edit_\#{$1.singularize}_path(\#{item.id})\")\n\n" m.puts "\e[33m" # yellow m.puts(paths) m.puts "\e[0m" # clear m.puts "Press [ENTER] to continue" m.pause m.puts "And put this in your global_steps.rb file (if you haven't already):\n\n" steps = "Given /^I have ?a? (.+) named (.+)$/ do |model_name, names|\n" steps << " names.split(', ').each do |n|\n" steps << " Factory(model_name.singularize, :name => n)\n" steps << " end\n" steps << "end\n\n" steps << "Given /^I have no (.+)$/ do |model_name|\n" steps << " model_name.singularize.capitalize.constantize.delete_all\n" steps << "end\n\n" steps << "Then /^I should have ([0-9]+|no) (.+) records?$/ do |count, model_name|\n" steps << " model_name.singularize.capitalize.constantize.count.should == count.to_i\n" steps << "end\n\n" steps << "Then /^I should have ([0-9]+|no) (.+) records? named (.+)$/ do |count, model_name, name|\n" steps << " model_name.singularize.capitalize.constantize.count(:conditions => \"name = '\#{name.gsub('\"', '')}'\").should == count.to_i\n" steps << "end\n\n" m.puts "\e[33m" # yellow m.puts(steps) m.puts "\e[0m" # clear m.puts "Press [ENTER] to continue" m.pause m.puts "And put this in your application_helper.rb file (if you haven't already):\n\n" st = "def st(arg)\n" st << " return t(arg)unless(RAILS_ENV == 'test')\n" st << " arg\n" st << "end\n\n" m.puts "\e[33m" # yellow m.puts(st) m.puts "\e[0m" # clear m.puts "Press [ENTER] to continue" m.pause m.puts "And don't forget to put this in your cucumber.rb file\n" m.puts "\e[33m" # yellow m.puts "config.gem \"thoughtbot-factory_girl\", :lib => 'factory_girl', :source => 'http://gems.github.com'\n\n" m.puts "\e[0m" # clear # run the say command (OSX only, I guess) #m.system("say -v zarvox The #{singular_name} scaffold was created, my master") unless [:skip_migration] m.migration_template( 'awesome_scaffold:migration.rb', 'db/migrate', :assigns => { :migration_name => "Create#{singular_name.pluralize.capitalize}", :attributes => attributes }, :migration_file_name => "create_#{controller_singular_name.gsub(/\//, '_').pluralize}" ) end end end |