Commit 1198ed354d5d15b589213ee1490b6df0c61b5537
1 parent
60f50df7
Exists in
master
and in
1 other branch
updating coulda
Showing
5 changed files
with
64 additions
and
9 deletions
Show diff stats
vendor/plugins/coulda/features/step_definitions/model_steps.rb
| @@ -26,6 +26,13 @@ When /^I generate a model "(.*)" with file "(.*)"$/ do |model, file| | @@ -26,6 +26,13 @@ When /^I generate a model "(.*)" with file "(.*)"$/ do |model, file| | ||
| 26 | "cd .." | 26 | "cd .." |
| 27 | end | 27 | end |
| 28 | 28 | ||
| 29 | +When /^I generate a Post model with title, body, and User$/ do | ||
| 30 | + system "cd #{@rails_root} && " << | ||
| 31 | + "script/generate model Post title:string body:text user:belongs_to && " << | ||
| 32 | + "rake db:migrate " << | ||
| 33 | + "cd .." | ||
| 34 | +end | ||
| 35 | + | ||
| 29 | # MODEL | 36 | # MODEL |
| 30 | 37 | ||
| 31 | Then /^the "(.*)" model should have "(.*)" macro$/ do |model, macro| | 38 | Then /^the "(.*)" model should have "(.*)" macro$/ do |model, macro| |
vendor/plugins/coulda/features/step_definitions/view_steps.rb
| @@ -4,12 +4,32 @@ When /^I generate a "([^\"]*)" view for "([^\"]*)"$/ do |view, resource| | @@ -4,12 +4,32 @@ When /^I generate a "([^\"]*)" view for "([^\"]*)"$/ do |view, resource| | ||
| 4 | "cd .." | 4 | "cd .." |
| 5 | end | 5 | end |
| 6 | 6 | ||
| 7 | -When /^a standard "new" view for "posts" should be generated$/ do | 7 | +When /^a SemiFormal "new" view for "posts" should be generated$/ do |
| 8 | assert_generated_file("app/views/posts/new.html.erb") do | 8 | assert_generated_file("app/views/posts/new.html.erb") do |
| 9 | - "<h1>New post</h1>\n\n" << | ||
| 10 | - "<% form_for(@post) do |form| %>\n" << | ||
| 11 | - " <%= form.error_messages %>\n" << | ||
| 12 | - " <%= form.submit 'Create', :disable_with => 'Please wait...' %>\n" << | 9 | + "<h1>New post</h1>\n\n" << |
| 10 | + "<% form_for(@post) do |form| %>\n" << | ||
| 11 | + " <%= form.error_messages %>\n" << | ||
| 12 | + " <fieldset class=\"inputs\">\n" << | ||
| 13 | + " </fieldset>\n" << | ||
| 14 | + " <fieldset class=\"buttons\">\n" << | ||
| 15 | + " <%= form.submit 'Create', :disable_with => 'Please wait...' %>\n" << | ||
| 16 | + " </fieldset>\n" << | ||
| 17 | + "<% end %>" | ||
| 18 | + end | ||
| 19 | +end | ||
| 20 | + | ||
| 21 | +Then /^a SemiFormal "new" view for "posts" should be generated with fields$/ do | ||
| 22 | + assert_generated_file("app/views/posts/new.html.erb") do | ||
| 23 | + "<h1>New post</h1>\n\n" << | ||
| 24 | + "<% form_for(@post) do |form| %>\n" << | ||
| 25 | + " <%= form.error_messages %>\n" << | ||
| 26 | + " <fieldset class=\"inputs\">\n" << | ||
| 27 | + " <%= form.string :title %>\n" << | ||
| 28 | + " <%= form.text :body %>\n" << | ||
| 29 | + " </fieldset>\n" << | ||
| 30 | + " <fieldset class=\"buttons\">\n" << | ||
| 31 | + " <%= form.submit 'Create', :disable_with => 'Please wait...' %>\n" << | ||
| 32 | + " </fieldset>\n" << | ||
| 13 | "<% end %>" | 33 | "<% end %>" |
| 14 | end | 34 | end |
| 15 | end | 35 | end |
vendor/plugins/coulda/features/view_generator.feature
| @@ -7,5 +7,12 @@ Feature: Rails view generator | @@ -7,5 +7,12 @@ Feature: Rails view generator | ||
| 7 | Given a Rails app | 7 | Given a Rails app |
| 8 | And the coulda plugin is installed | 8 | And the coulda plugin is installed |
| 9 | When I generate a "new" view for "Posts" | 9 | When I generate a "new" view for "Posts" |
| 10 | - Then a standard "new" view for "posts" should be generated | 10 | + Then a SemiFormal "new" view for "posts" should be generated |
| 11 | + | ||
| 12 | + Scenario: View generator for new action | ||
| 13 | + Given a Rails app | ||
| 14 | + And the coulda plugin is installed | ||
| 15 | + When I generate a Post model with title, body, and User | ||
| 16 | + And I generate a "new" view for "Posts" | ||
| 17 | + Then a SemiFormal "new" view for "posts" should be generated with fields | ||
| 11 | 18 |
vendor/plugins/coulda/generators/support/generator_helper.rb
| @@ -18,9 +18,21 @@ module Coulda | @@ -18,9 +18,21 @@ module Coulda | ||
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | def columns_for_form | 20 | def columns_for_form |
| 21 | - resource_class.content_columns. | 21 | + resource_class.constantize.content_columns. |
| 22 | collect { |column| [column.name, column.type] }. | 22 | collect { |column| [column.name, column.type] }. |
| 23 | - delete_if { |column| REMOVABLE_COLUMNS.include?(column.first) } | 23 | + delete_if { |column| remove_column?(column.first) } |
| 24 | + end | ||
| 25 | + | ||
| 26 | + def active_record_defined? | ||
| 27 | + models = Dir.glob(File.join( RAILS_ROOT, 'app', 'models', '*.rb')). | ||
| 28 | + collect { |path| path[/.+\/(.+).rb/,1] }. | ||
| 29 | + collect {|model| model.classify } | ||
| 30 | + models.include?(resource_class) | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def remove_column?(column) | ||
| 34 | + REMOVABLE_COLUMNS.include?(column) || | ||
| 35 | + !(column =~ /_id$/).nil? | ||
| 24 | end | 36 | end |
| 25 | end | 37 | end |
| 26 | end | 38 | end |
vendor/plugins/coulda/generators/view/templates/view_new.html.erb
| @@ -2,5 +2,14 @@ | @@ -2,5 +2,14 @@ | ||
| 2 | 2 | ||
| 3 | <%% form_for(@<%= resource %>) do |form| %> | 3 | <%% form_for(@<%= resource %>) do |form| %> |
| 4 | <%%= form.error_messages %> | 4 | <%%= form.error_messages %> |
| 5 | - <%%= form.submit 'Create', :disable_with => 'Please wait...' %> | 5 | + <fieldset class="inputs"> |
| 6 | +<% if active_record_defined? -%> | ||
| 7 | +<% columns_for_form.each do |attribute_name, attribute_type| -%> | ||
| 8 | + <%%= form.<%= attribute_type %> :<%= attribute_name %> %> | ||
| 9 | +<% end -%> | ||
| 10 | +<% end -%> | ||
| 11 | + </fieldset> | ||
| 12 | + <fieldset class="buttons"> | ||
| 13 | + <%%= form.submit 'Create', :disable_with => 'Please wait...' %> | ||
| 14 | + </fieldset> | ||
| 6 | <%% end %> | 15 | <%% end %> |