From ada41866471149ee95fdb7e0e7066ebefbfcf038 Mon Sep 17 00:00:00 2001 From: Raghu Betina Date: Sat, 20 May 2017 08:33:24 -0500 Subject: [PATCH 1/2] Disambiguating by breaking more conventions - Using strings as keys to the `params` hash following the heuristic: "If we could change it to 'zebra' on a whim, then use a string; otherwise use a symbol." - Making variable and input names longer and more pedantic. - Adding a non-error version of forms. --- .../draft/layout/templates/_flashes.html.erb | 38 +++++----- .../draft/layout/templates/_navbar.html.erb | 2 +- .../draft/layout/templates/layout.html.erb | 4 +- .../draft/resource/resource_generator.rb | 12 ++-- .../templates/controllers/controller.rb | 71 ++++++++++--------- .../controllers/read_only_controller.rb | 10 +-- .../templates/views/create_row.html.erb | 6 +- .../templates/views/destroy_row.html.erb | 6 +- .../templates/views/edit_form.html.erb | 19 +---- .../views/edit_form_with_errors.html.erb | 54 ++++++++++++++ .../resource/templates/views/index.html.erb | 24 ++----- .../templates/views/new_form.html.erb | 19 +---- .../views/new_form_with_errors.html.erb | 54 ++++++++++++++ .../resource/templates/views/show.html.erb | 12 ++-- .../templates/views/update_row.html.erb | 2 +- 15 files changed, 203 insertions(+), 130 deletions(-) create mode 100644 lib/generators/draft/resource/templates/views/edit_form_with_errors.html.erb create mode 100644 lib/generators/draft/resource/templates/views/new_form_with_errors.html.erb diff --git a/lib/generators/draft/layout/templates/_flashes.html.erb b/lib/generators/draft/layout/templates/_flashes.html.erb index 1cd878a7..929d25f2 100644 --- a/lib/generators/draft/layout/templates/_flashes.html.erb +++ b/lib/generators/draft/layout/templates/_flashes.html.erb @@ -1,23 +1,27 @@ -
-
- <%% if notice.present? %> - -
+
> diff --git a/lib/generators/draft/layout/templates/layout.html.erb b/lib/generators/draft/layout/templates/layout.html.erb index 50ec9368..36fe11ad 100644 --- a/lib/generators/draft/layout/templates/layout.html.erb +++ b/lib/generators/draft/layout/templates/layout.html.erb @@ -22,9 +22,9 @@ <%%= render "shared/navbar" %> -
- <%%= render "shared/flashes" %> + <%%= render "shared/flashes" %> +
<%%= yield %>
diff --git a/lib/generators/draft/resource/resource_generator.rb b/lib/generators/draft/resource/resource_generator.rb index 6dfb346d..3b34661f 100644 --- a/lib/generators/draft/resource/resource_generator.rb +++ b/lib/generators/draft/resource/resource_generator.rb @@ -14,9 +14,9 @@ def generate_controller return if skip_controller? if read_only? - template "controllers/read_only_controller.rb", "app/controllers/#{plural_table_name.underscore}_controller.rb" + template "controllers/read_only_controller.rb", "app/controllers/#{plural_table_name}_controller.rb" else - template "controllers/controller.rb", "app/controllers/#{plural_table_name.underscore}_controller.rb" + template "controllers/controller.rb", "app/controllers/#{plural_table_name}_controller.rb" end end @@ -48,8 +48,8 @@ def generate_specs return # return if read_only? || skip_controller? || skip_model? - template "specs/crud_spec.rb", "spec/features/crud_#{plural_table_name.underscore}_spec.rb" - template "specs/factories.rb", "spec/factories/#{plural_table_name.underscore}.rb" + template "specs/crud_spec.rb", "spec/features/crud_#{plural_table_name}_spec.rb" + template "specs/factories.rb", "spec/factories/#{plural_table_name}.rb" end private @@ -70,7 +70,7 @@ def golden_seven_routes get("/#{plural_table_name}/:id_to_display", { :controller => "#{plural_table_name}", :action => "show" }) # UPDATE - get("/#{plural_table_name}/:prefill_with_id/edit", { :controller => "#{plural_table_name}", :action => "edit_form" }) + get("/#{plural_table_name}/:id_to_edit/edit", { :controller => "#{plural_table_name}", :action => "edit_form" }) #{skip_post? ? "get" : "post"}("/update_#{singular_table_name}/:id_to_modify", { :controller => "#{plural_table_name}", :action => "update_row" }) # DELETE @@ -132,7 +132,7 @@ def available_views elsif skip_redirect? %w(index show new_form create_row edit_form update_row destroy_row) else - %w(index new_form edit_form show) + %w(index show new_form new_form_with_errors edit_form edit_form_with_errors) end end diff --git a/lib/generators/draft/resource/templates/controllers/controller.rb b/lib/generators/draft/resource/templates/controllers/controller.rb index 8a8b0c6c..c6b151b3 100644 --- a/lib/generators/draft/resource/templates/controllers/controller.rb +++ b/lib/generators/draft/resource/templates/controllers/controller.rb @@ -1,97 +1,102 @@ class <%= plural_table_name.camelize %>Controller < ApplicationController def index - @<%= plural_table_name.underscore %> = <%= class_name.singularize %>.all + @list_of_<%= plural_table_name %> = <%= class_name.singularize %>.all.order({ :created_at => :desc }) - render("<%= singular_table_name.underscore %>_templates/index.html.erb") + render("<%= singular_table_name %>_templates/index.html.erb") end def show - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params[:id_to_display]) + id_of_<%= singular_table_name %>_to_show = params.fetch("id_to_display") - render("<%= singular_table_name.underscore %>_templates/show.html.erb") + @<%= singular_table_name %>_to_show = <%= class_name.singularize %>.find(id_of_<%= singular_table_name %>_to_show) + + render("<%= singular_table_name %>_templates/show.html.erb") end def new_form -<% unless skip_validation_alerts? -%> - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new -<% end -%> - render("<%= singular_table_name.underscore %>_templates/new_form.html.erb") + render("<%= singular_table_name %>_templates/new_form.html.erb") end def create_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new + @<%= singular_table_name %> = <%= class_name.singularize %>.new <% attributes.each do |attribute| -%> - @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params[:<%= attribute.column_name %>] + @<%= singular_table_name %>.<%= attribute.column_name %> = params.fetch("<%= attribute.column_name %>_from_form") <% end -%> <% unless skip_validation_alerts? -%> - save_status = @<%= singular_table_name.underscore %>.save + save_status = @<%= singular_table_name %>.save if save_status == true - redirect_to("/<%= @plural_table_name.underscore %>", :notice => "<%= singular_table_name.humanize %> created successfully.") + redirect_to("/<%= plural_table_name %>", :notice => "<%= singular_table_name.humanize %> created successfully.") else - render("<%= singular_table_name.underscore %>_templates/new_form.html.erb") + render("<%= singular_table_name %>_templates/new_form_with_errors.html.erb") end <% else -%> - @<%= singular_table_name.underscore %>.save + @<%= singular_table_name %>.save <% unless skip_redirect? -%> - redirect_to("/<%= @plural_table_name.underscore %>") + redirect_to("/<%= plural_table_name %>") <% else -%> - @current_count = <%= class_name.singularize %>.count + @current_<%= singular_table_name %>_count = <%= class_name.singularize %>.count - render("<%= singular_table_name.underscore %>_templates/create_row.html.erb") + render("<%= singular_table_name %>_templates/create_row.html.erb") <% end -%> <% end -%> end def edit_form - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params[:prefill_with_id]) + id_of_<%= singular_table_name %>_to_prefill = params.fetch("id_to_edit") - render("<%= singular_table_name.underscore %>_templates/edit_form.html.erb") + @<%= singular_table_name %> = <%= class_name.singularize %>.find(id_of_<%= singular_table_name %>_to_prefill) + + render("<%= singular_table_name %>_templates/edit_form.html.erb") end def update_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params[:id_to_modify]) + id_of_<%= singular_table_name %>_to_change = params.fetch("id_to_modify") + + @<%= singular_table_name %>_to_change = <%= class_name.singularize %>.find(id_of_<%= singular_table_name %>_to_change) <% attributes.each do |attribute| -%> - @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params[:<%= attribute.column_name %>] + @<%= singular_table_name %>_to_change.<%= attribute.column_name %> = params.fetch("<%= attribute.column_name %>_from_form") <% end -%> <% unless skip_validation_alerts? -%> - save_status = @<%= singular_table_name.underscore %>.save + save_status = @<%= singular_table_name %>_to_change.save if save_status == true - redirect_to("/<%= @plural_table_name.underscore %>/#{@<%= singular_table_name.underscore %>.id}", :notice => "<%= singular_table_name.humanize %> updated successfully.") + redirect_to("/<%= plural_table_name %>/#{@<%= singular_table_name %>_to_change.id}", :notice => "<%= singular_table_name.humanize %> updated successfully.") else - render("<%= singular_table_name.underscore %>_templates/edit_form.html.erb") + render("<%= singular_table_name %>_templates/edit_form_with_errors.html.erb") end <% else -%> - @<%= singular_table_name.underscore %>.save + @<%= singular_table_name %>_to_change.save <% unless skip_redirect? -%> - redirect_to("/<%= @plural_table_name.underscore %>/#{@<%= singular_table_name.underscore %>.id}") + redirect_to("/<%= plural_table_name %>/#{@<%= singular_table_name %>_to_change.id}") <% else -%> - render("<%= singular_table_name.underscore %>_templates/update_row.html.erb") + render("<%= singular_table_name %>_templates/update_row.html.erb") <% end -%> <% end -%> end def destroy_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params[:id_to_remove]) + id_of_<%= singular_table_name %>_to_delete = params.fetch("id_to_remove") + + @<%= singular_table_name %>_to_toast = <%= class_name.singularize %>.find(id_of_<%= singular_table_name %>_to_delete) - @<%= singular_table_name.underscore %>.destroy + @<%= singular_table_name %>_to_toast.destroy <% unless skip_validation_alerts? -%> - redirect_to("/<%= @plural_table_name.underscore %>", :notice => "<%= singular_table_name.humanize %> deleted successfully.") + redirect_to("/<%= plural_table_name %>", :notice => "<%= singular_table_name.humanize %> deleted successfully.") <% else -%> <% unless skip_redirect? -%> - redirect_to("/<%= @plural_table_name.underscore %>") + redirect_to("/<%= plural_table_name %>") <% else -%> - @remaining_count = <%= class_name.singularize %>.count + @remaining_<%= singular_table_name %>_count = <%= class_name.singularize %>.count - render("<%= singular_table_name.underscore %>_templates/destroy_row.html.erb") + render("<%= singular_table_name %>_templates/destroy_row.html.erb") <% end -%> <% end -%> end diff --git a/lib/generators/draft/resource/templates/controllers/read_only_controller.rb b/lib/generators/draft/resource/templates/controllers/read_only_controller.rb index a370bf75..18a2aea9 100644 --- a/lib/generators/draft/resource/templates/controllers/read_only_controller.rb +++ b/lib/generators/draft/resource/templates/controllers/read_only_controller.rb @@ -1,13 +1,15 @@ class <%= plural_table_name.camelize %>Controller < ApplicationController def index - @<%= plural_table_name.underscore %> = <%= class_name.singularize %>.all + @list_of_<%= plural_table_name %> = <%= class_name.singularize %>.all.order({ :created_at => :desc }) - render("<%= singular_table_name.underscore %>_templates/index.html.erb") + render("<%= singular_table_name %>_templates/index.html.erb") end def show - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params[:id]) + id_of_<%= singular_table_name %>_to_show = params.fetch("id_to_display") - render("<%= singular_table_name.underscore %>_templates/index.html.erb") + @<%= singular_table_name %>_to_show = <%= class_name.singularize %>.find(id_of_<%= singular_table_name %>_to_show) + + render("<%= singular_table_name %>_templates/show.html.erb") end end diff --git a/lib/generators/draft/resource/templates/views/create_row.html.erb b/lib/generators/draft/resource/templates/views/create_row.html.erb index c3fe0906..00b79431 100644 --- a/lib/generators/draft/resource/templates/views/create_row.html.erb +++ b/lib/generators/draft/resource/templates/views/create_row.html.erb @@ -1,11 +1,11 @@ -

You created <%= singular_table_name.humanize.downcase %> #<%%= @<%= singular_table_name %>.id %>!

+

You created a <%= singular_table_name.humanize.downcase %>!

- There are now <%%= @current_count %> <%= plural_table_name.humanize.downcase %>. + There are now <%%= @current_<%= singular_table_name %>_count %> <%= plural_table_name.humanize.downcase %>.

- + Click here diff --git a/lib/generators/draft/resource/templates/views/destroy_row.html.erb b/lib/generators/draft/resource/templates/views/destroy_row.html.erb index 60a796ea..638046a9 100644 --- a/lib/generators/draft/resource/templates/views/destroy_row.html.erb +++ b/lib/generators/draft/resource/templates/views/destroy_row.html.erb @@ -1,11 +1,11 @@ -

You deleted <%= singular_table_name.humanize.downcase %> #<%%= @<%= singular_table_name %>.id %>!

+

You deleted a <%= singular_table_name.humanize.downcase %>!

- There are now <%%= @remaining_count %> <%= plural_table_name.humanize.downcase %>. + There are now <%%= @remaining_<%= singular_table_name %>_count %> <%= plural_table_name.humanize.downcase %>.

- + Click here diff --git a/lib/generators/draft/resource/templates/views/edit_form.html.erb b/lib/generators/draft/resource/templates/views/edit_form.html.erb index eb6f78b0..ac4bbba6 100644 --- a/lib/generators/draft/resource/templates/views/edit_form.html.erb +++ b/lib/generators/draft/resource/templates/views/edit_form.html.erb @@ -1,18 +1,3 @@ -<% unless skip_validation_alerts? -%> - -<%% if @<%= singular_table_name %>.errors.any? %> - <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> -

- <%% end %> -<%% end %> - -<% end -%>

Edit <%= singular_table_name.humanize.downcase %> #<%%= @<%= singular_table_name %>.id %>

@@ -37,9 +22,9 @@ <% if attribute.field_type == :text_area -%> - + <% else -%> - + <% end -%>
<% end -%> diff --git a/lib/generators/draft/resource/templates/views/edit_form_with_errors.html.erb b/lib/generators/draft/resource/templates/views/edit_form_with_errors.html.erb new file mode 100644 index 00000000..2fbadf57 --- /dev/null +++ b/lib/generators/draft/resource/templates/views/edit_form_with_errors.html.erb @@ -0,0 +1,54 @@ +<% unless skip_validation_alerts? -%> + +<%% if @<%= singular_table_name %>.errors.any? %> + <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> +
+ + + <%%= message %> +
+ <%% end %> +<%% end %> + +<% end -%> +

+ Edit <%= singular_table_name.humanize.downcase %> #<%%= @<%= singular_table_name %>.id %> +

+ +
+ +
method="post"<% end -%>> +<% attributes.each do |attribute| -%> + +<% if attribute.field_type == :check_box -%> +
+ +
+<% else -%> +
+ + +<% if attribute.field_type == :text_area -%> + +<% else -%> + +<% end -%> +
+<% end -%> + +<% end -%> + +
+ +
+ + + Go back + diff --git a/lib/generators/draft/resource/templates/views/index.html.erb b/lib/generators/draft/resource/templates/views/index.html.erb index 2caca9d1..3b9c8007 100644 --- a/lib/generators/draft/resource/templates/views/index.html.erb +++ b/lib/generators/draft/resource/templates/views/index.html.erb @@ -20,40 +20,24 @@ <% end -%> - - Created at - - - - Updated at - - - <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> + <%% @list_of_<%= plural_table_name %>.each do |the_current_<%= singular_table_name %>| %> - <%%= <%= singular_table_name %>.id %> + <%%= the_current_<%= singular_table_name %>.id %> <% attributes.each do |attribute| -%> - <%%= <%= singular_table_name %>.<%= attribute.column_name %> %> + <%%= the_current_<%= singular_table_name %>.<%= attribute.column_name %> %> <% end -%> - <%%= time_ago_in_words(<%= singular_table_name %>.created_at) %> ago - - - - <%%= time_ago_in_words(<%= singular_table_name %>.updated_at) %> ago - - - - + Show details diff --git a/lib/generators/draft/resource/templates/views/new_form.html.erb b/lib/generators/draft/resource/templates/views/new_form.html.erb index bdffdd12..d6db322f 100644 --- a/lib/generators/draft/resource/templates/views/new_form.html.erb +++ b/lib/generators/draft/resource/templates/views/new_form.html.erb @@ -1,18 +1,3 @@ -<% unless skip_validation_alerts? -%> - -<%% if @<%= singular_table_name %>.errors.any? %> - <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> - - <%% end %> -<%% end %> - -<% end -%>

Add a new <%= singular_table_name.humanize.downcase %>

@@ -37,9 +22,9 @@ <% if attribute.field_type == :text_area -%> - + <% else -%> - value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> + <% end -%> <% end -%> diff --git a/lib/generators/draft/resource/templates/views/new_form_with_errors.html.erb b/lib/generators/draft/resource/templates/views/new_form_with_errors.html.erb new file mode 100644 index 00000000..3c195d88 --- /dev/null +++ b/lib/generators/draft/resource/templates/views/new_form_with_errors.html.erb @@ -0,0 +1,54 @@ +<% unless skip_validation_alerts? -%> + +<%% if @<%= singular_table_name %>.errors.any? %> + <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> +
+ + + <%%= message %> +
+ <%% end %> +<%% end %> + +<% end -%> +

+ Add a new <%= singular_table_name.humanize.downcase %> +

+ +
+ +
method="post"<% end -%>> +<% attributes.each do |attribute| -%> + +<% if attribute.field_type == :check_box -%> +
+ +
+<% else -%> +
+ + +<% if attribute.field_type == :text_area -%> + +<% else -%> + value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> +<% end -%> +
+<% end -%> + +<% end -%> + +
+ +
+ + + Go back + diff --git a/lib/generators/draft/resource/templates/views/show.html.erb b/lib/generators/draft/resource/templates/views/show.html.erb index ad19cebf..2eba63f0 100644 --- a/lib/generators/draft/resource/templates/views/show.html.erb +++ b/lib/generators/draft/resource/templates/views/show.html.erb @@ -1,8 +1,8 @@

- <%= singular_table_name.humanize %> #<%%= @<%= singular_table_name %>.id %> details + <%= singular_table_name.humanize %> #<%%= @<%= singular_table_name %>_to_show.id %> details

- + Edit <%= singular_table_name.humanize.downcase %> @@ -14,7 +14,7 @@ <%= attribute.human_name %>
- <%%= @<%= singular_table_name %>.<%= attribute.column_name %> %> + <%%= @<%= singular_table_name %>_to_show.<%= attribute.column_name %> %>
<% end -%> @@ -22,19 +22,19 @@ Created at
- <%%= time_ago_in_words(@<%= singular_table_name %>.created_at) %> ago + <%%= time_ago_in_words(@<%= singular_table_name %>_to_show.created_at) %> ago
Updated at
- <%%= time_ago_in_words(@<%= singular_table_name %>.updated_at) %> ago + <%%= time_ago_in_words(@<%= singular_table_name %>_to_show.updated_at) %> ago
<% unless read_only? -%> - + Delete <%= singular_table_name.humanize.downcase %> <% end -%> diff --git a/lib/generators/draft/resource/templates/views/update_row.html.erb b/lib/generators/draft/resource/templates/views/update_row.html.erb index 42048f44..b61e6b50 100644 --- a/lib/generators/draft/resource/templates/views/update_row.html.erb +++ b/lib/generators/draft/resource/templates/views/update_row.html.erb @@ -1,7 +1,7 @@

You updated <%= singular_table_name.humanize.downcase %> #<%%= @<%= singular_table_name %>.id %>!

- + Click here From 3f9455e6f3cf06db646963db3913fe6a5076f69f Mon Sep 17 00:00:00 2001 From: Raghu Betina Date: Thu, 15 Feb 2018 15:47:32 -0600 Subject: [PATCH 2/2] Remove hotfix --- lib/generators/draft/resource/resource_generator.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/generators/draft/resource/resource_generator.rb b/lib/generators/draft/resource/resource_generator.rb index 3b34661f..5f414649 100644 --- a/lib/generators/draft/resource/resource_generator.rb +++ b/lib/generators/draft/resource/resource_generator.rb @@ -44,9 +44,7 @@ def generate_routes end def generate_specs - # Hotfix to prevent specs during MSM Associations - return - # return if read_only? || skip_controller? || skip_model? + return if read_only? || skip_controller? || skip_model? template "specs/crud_spec.rb", "spec/features/crud_#{plural_table_name}_spec.rb" template "specs/factories.rb", "spec/factories/#{plural_table_name}.rb"