diff --git a/Gemfile b/Gemfile index 7293f830..183712ab 100644 --- a/Gemfile +++ b/Gemfile @@ -13,4 +13,5 @@ group :development do gem "simplecov", ">= 0" end +gem 'indefinite_article' gem 'devise' diff --git a/draft_generators.gemspec b/draft_generators.gemspec index ea37250e..88a8f55d 100644 --- a/draft_generators.gemspec +++ b/draft_generators.gemspec @@ -95,6 +95,7 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q.freeze, [">= 0"]) + s.add_runtime_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, ["~> 3.5.0"]) s.add_development_dependency(%q.freeze, ["~> 3.12"]) s.add_development_dependency(%q.freeze, ["~> 1.0"]) diff --git a/lib/draft_generators.rb b/lib/draft_generators.rb index 81eb0e06..83d63e63 100644 --- a/lib/draft_generators.rb +++ b/lib/draft_generators.rb @@ -3,6 +3,7 @@ require "devise_customization_service" require "rails_tag_service" require "devise" +require "indefinite_article" module DraftGenerators end diff --git a/lib/generators/draft/account/account_generator.rb b/lib/generators/draft/account/account_generator.rb new file mode 100644 index 00000000..539515f3 --- /dev/null +++ b/lib/generators/draft/account/account_generator.rb @@ -0,0 +1,189 @@ +require "rails/generators/named_base" + +module Draft + class AccountGenerator < Rails::Generators::NamedBase + source_root File.expand_path("../templates", __FILE__) + + argument :attributes, type: :array, default: [], + banner: "field:type field:type" + + include Rails::Generators::ResourceHelpers + + def generate_required_attributes + unless attributes_has_password_digest? + password_digest = Rails::Generators::GeneratedAttribute.parse(["password_digest", :string, nil].compact.join(":")) + attributes.unshift(password_digest) + end + unless attributes_has_email? + email = Rails::Generators::GeneratedAttribute.parse(["email", :string, nil].compact.join(":")) + attributes.unshift(email) + end + end + + def generate_model + invoke "draft:model", paramaterize_attributes + edit_model + end + + def generate_routes + authentication_routes + end + + def generate_before_actions + authentication_helpers + end + + def generate_controller + template "controllers/authentication_controller.rb", "app/controllers/#{singular_table_name.underscore}_authentication_controller.rb" + end + + def create_root_folder + empty_directory File.join("app/views", "#{singular_table_name.underscore}_authentication") + end + + def generate_view_files + available_views.each do |view| + filename = view_authentication_filename_with_extensions(view) + template filename, File.join("app/views/#{singular_table_name.underscore}_authentication", File.basename(filename)) + end + end + + private + + def authentication_routes + log :route, "Authentication routes" + + route <<-RUBY.gsub(/^ /, "") + + # Routes for the #{singular_table_name.humanize} account: + + # SIGN UP FORM + get("/#{singular_table_name.underscore}_sign_up", { :controller => "#{singular_table_name.underscore}_authentication", :action => "sign_up_form" }) + # CREATE RECORD + post("/insert_#{singular_table_name.underscore}", { :controller => "#{singular_table_name.underscore}_authentication", :action => "create" }) + + # EDIT PROFILE FORM + get("/edit_#{singular_table_name.underscore}_profile", { :controller => "#{singular_table_name.underscore}_authentication", :action => "edit_profile_form" }) + # UPDATE RECORD + post("/modify_#{singular_table_name.underscore}", { :controller => "#{singular_table_name.underscore}_authentication", :action => "update" }) + + # DELETE RECORD + get("/cancel_#{singular_table_name.underscore}_account", { :controller => "#{singular_table_name.underscore}_authentication", :action => "destroy" }) + + # ------------------------------ + + # SIGN IN FORM + get("/#{singular_table_name.underscore}_sign_in", { :controller => "#{singular_table_name.underscore}_authentication", :action => "sign_in_form" }) + # AUTHENTICATE AND STORE COOKIE + post("/#{singular_table_name.underscore}_verify_credentials", { :controller => "#{singular_table_name.underscore}_authentication", :action => "create_cookie" }) + + # SIGN OUT + get("/#{singular_table_name.underscore}_sign_out", { :controller => "#{singular_table_name.underscore}_authentication", :action => "destroy_cookies" }) + + #------------------------------ + RUBY + end + + def authentication_helpers + log :controller, "Authentication before_actions" + + application_controller <<-RUBY.gsub(/^ /, "") + + before_action(:load_current_#{singular_table_name.underscore}) + + # Uncomment the "before_action" if you want to force #{plural_table_name} to sign in before any other actions + # Remember to also uncomment the "skip_before_action" in the corresponding authentication controller + # before_action(:force_#{singular_table_name.underscore}_sign_in) + + def load_current_#{singular_table_name.underscore} + the_id = session[:#{singular_table_name.underscore}_id] + @current_#{singular_table_name.underscore} = #{class_name.singularize}.where({ :id => the_id }).first + end + + def force_#{singular_table_name.underscore}_sign_in + if @current_#{singular_table_name.underscore} == nil + redirect_to("/#{singular_table_name.underscore}_sign_in", { :notice => "You have to sign in first." }) + end + end + RUBY + end + + def edit_model + sentinel = /.*ApplicationRecord\n/ + content = " validates :email, :uniqueness => { :case_sensitive => false }\n"\ + " validates :email, :presence => true\n"\ + " has_secure_password\n" + if model_exists? + inside "app/models" do + insert_into_file "#{singular_table_name.underscore}.rb", content, after: sentinel + end + end + end + + def route(routing_code) + sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/ + + inside "config" do + insert_into_file "routes.rb", routing_code, after: sentinel + end + end + + def application_controller(app_code) + sentinel = /::Base$/ + + inside "app/controllers" do + insert_into_file "application_controller.rb", app_code, after: sentinel + end + end + + def available_views + %w(sign_up sign_in edit_profile edit_profile_with_errors) + end + + def view_filename_with_extensions(name) + filename = [name, :html, :erb].compact.join(".") + folders = ["views"] + filename = File.join(folders, filename) if folders.any? + filename + end + + def view_authentication_filename_with_extensions(name) + filename = [name, :html, :erb].compact.join(".") + folders = ["views", "authentication"] + filename = File.join(folders, filename) if folders.any? + filename + end + + def controller_filename_with_extensions(name) + filename = [name,:rb].compact.join(".") + folders = ["controllers"] + filename = File.join(folders, filename) if folders.any? + filename + end + + def model_exists? + File.exist?(File.join(destination_root, model_path)) + end + + def model_path + @model_path ||= File.join("app", "models", "#{file_path}.rb") + end + + def attributes_has_email? + attributes.any? { |attribute| attribute.column_name.include?("email") } + end + + def attributes_has_password_digest? + attributes.any?{ |attribute| attribute.column_name.include?("password_digest") } + end + + def paramaterize_attributes + array = [singular_table_name.underscore] + attributes.each do |attribute| + array.push(attribute.column_name + ":" + attribute.type.to_s) + end + array + end + + end +end diff --git a/lib/generators/draft/account/templates/controllers/authentication_controller.rb b/lib/generators/draft/account/templates/controllers/authentication_controller.rb new file mode 100644 index 00000000..f888a770 --- /dev/null +++ b/lib/generators/draft/account/templates/controllers/authentication_controller.rb @@ -0,0 +1,97 @@ +class <%= class_name.singularize %>AuthenticationController < ApplicationController + # Uncomment this if you want to force <%= plural_table_name %> to sign in before any other actions + # skip_before_action(:force_<%= singular_table_name.underscore %>_sign_in, { :only => [:sign_up_form, :create, :sign_in_form, :create_cookie] }) + # Remember to also uncomment the "before_action" in the ApplicationController + + def sign_in_form + render({ :template => "<%= singular_table_name.underscore %>_authentication/sign_in.html.erb" }) + end + + def create_cookie + <%= singular_table_name.underscore %> = <%= class_name.singularize %>.where({ :email => params.fetch("query_email") }).first + + the_supplied_password = params.fetch("query_password") + + if <%= singular_table_name.underscore %> != nil + are_they_legit = <%= singular_table_name.underscore %>.authenticate(the_supplied_password) + + if are_they_legit == false + redirect_to("/<%= singular_table_name.underscore %>_sign_in", { :alert => "Incorrect password." }) + else + session[:<%= singular_table_name.underscore %>_id] = <%= singular_table_name.underscore %>.id + + redirect_to("/", { :notice => "Signed in successfully." }) + end + else + redirect_to("/<%= singular_table_name.underscore %>_sign_in", { :alert => "No <%= singular_table_name.underscore %> with that email address." }) + end + end + + def destroy_cookies + reset_session + + redirect_to("/", { :notice => "Signed out successfully." }) + end + + def sign_up_form + render({ :template => "<%= singular_table_name.underscore %>_authentication/sign_up.html.erb" }) + end + + def create + @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new +<% attributes.each do |attribute| -%> +<% if attribute.field_type == :check_box -%> + @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>", false) +<% elsif attribute.column_name != "password_digest" -%> + @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>") +<% else -%> + @<%= singular_table_name.underscore %>.password = params.fetch("query_password") + @<%= singular_table_name.underscore %>.password_confirmation = params.fetch("query_password_confirmation") +<% end -%> +<% end -%> + + save_status = @<%= singular_table_name.underscore %>.save + + if save_status == true + session[:<%= singular_table_name.underscore %>_id] = @<%= singular_table_name.underscore %>.id + + redirect_to("/", { :notice => "<%= singular_table_name.humanize %> account created successfully."}) + else + redirect_to("/<%= singular_table_name.underscore %>_sign_up", { :alert => @<%= singular_table_name%>.errors.full_messages.to_sentence }) + end + end + + def edit_profile_form + render({ :template => "<%= singular_table_name.underscore %>_authentication/edit_profile.html.erb" }) + end + + def update + @<%= singular_table_name.underscore %> = @current_<%= singular_table_name.underscore %> +<% attributes.each do |attribute| -%> +<% if attribute.field_type == :check_box -%> + @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>", false) +<% elsif attribute.column_name != "password_digest" -%> + @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>") +<% else -%> + @<%= singular_table_name.underscore %>.password = params.fetch("query_password") + @<%= singular_table_name.underscore %>.password_confirmation = params.fetch("query_password_confirmation") +<% end -%> +<% end -%> + + if @<%= singular_table_name.underscore %>.valid? + @<%= singular_table_name.underscore %>.save + + redirect_to("/", { :notice => "<%= singular_table_name.humanize %> account updated successfully."}) + else + render({ :template => "<%= singular_table_name.underscore %>_authentication/edit_profile_with_errors.html.erb" , :alert => @<%= singular_table_name%>.errors.full_messages.to_sentence }) + end + end + + def destroy + @current_<%= singular_table_name.underscore %>.destroy + reset_session + + redirect_to("/", { :notice => "<%= class_name.singularize %> account cancelled" }) + end + +end diff --git a/lib/generators/draft/account/templates/views/authentication/edit_profile.html.erb b/lib/generators/draft/account/templates/views/authentication/edit_profile.html.erb new file mode 100644 index 00000000..79844bc8 --- /dev/null +++ b/lib/generators/draft/account/templates/views/authentication/edit_profile.html.erb @@ -0,0 +1,38 @@ +
+
+ Edit <%= class_name.titleize %> Account +
+ +
+
+<% attributes.each do |attribute| -%> +<% if attribute.column_name != "password_digest" -%> +
+ +<% if attribute.field_type == :text_area -%> + +<% elsif attribute.field_type == :check_box -%> + .<%= attribute.column_name %> %>> +<% else -%> + +<% end -%> +
+<% end -%> +<% end -%> + +
+ + +
+ +
+ + +
+ + +
+
+
diff --git a/lib/generators/draft/account/templates/views/authentication/edit_profile_with_errors.html.erb b/lib/generators/draft/account/templates/views/authentication/edit_profile_with_errors.html.erb new file mode 100644 index 00000000..2b7dfc87 --- /dev/null +++ b/lib/generators/draft/account/templates/views/authentication/edit_profile_with_errors.html.erb @@ -0,0 +1,44 @@ +
+
+ Edit <%= class_name.titleize %> Account +
+ + <%% if @<%= singular_table_name %>.errors.any? %> + <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> +
+ <%%= message %> +
+ <%% end %> + <%% end %> +
+
+ <% attributes.each do |attribute| -%> + <% if attribute.column_name != "password_digest" %> +
+ + <% if attribute.field_type == :text_area %> + + <% elsif attribute.field_type == :check_box %> + .<%= attribute.column_name %> %>> + <% else %> + + <% end %> +
+ <% end -%> + <% end %> +
+ + +
+ +
+ + +
+ + +
+
+
diff --git a/lib/generators/draft/account/templates/views/authentication/sign_in.html.erb b/lib/generators/draft/account/templates/views/authentication/sign_in.html.erb new file mode 100644 index 00000000..81236543 --- /dev/null +++ b/lib/generators/draft/account/templates/views/authentication/sign_in.html.erb @@ -0,0 +1,23 @@ +

Sign in

+ +
+
+ + +
+ +
+ + +
+ + +
+ +
+ +

+ Or, Sign up instead. +

diff --git a/lib/generators/draft/account/templates/views/authentication/sign_up.html.erb b/lib/generators/draft/account/templates/views/authentication/sign_up.html.erb new file mode 100644 index 00000000..5c94ff42 --- /dev/null +++ b/lib/generators/draft/account/templates/views/authentication/sign_up.html.erb @@ -0,0 +1,43 @@ +

Sign up

+ +
+ +
+ + +
+ <% attributes.each do |attribute| -%> + <% if attribute.column_name != "password_digest" && attribute.column_name != "email" -%> +
+ + <%- if attribute.field_type == :check_box -%> + + <%- elsif attribute.field_type == :text_area -%> + + <%- else -%> + + <%- end -%> +
+ <%- end -%> + <% end -%> + +
+ + +
+ +
+ + +
+ + +
+ +
+ +

+ Or, Sign in instead. +

diff --git a/lib/generators/draft/layout/layout_generator.rb b/lib/generators/draft/layout/layout_generator.rb index e4b3c7e2..5340ef0a 100644 --- a/lib/generators/draft/layout/layout_generator.rb +++ b/lib/generators/draft/layout/layout_generator.rb @@ -29,7 +29,7 @@ def generate_layout template "_flashes.html.erb", "app/views/shared/_flashes.html.erb" unless skip_cdn? - template "_bootstrapcdn_assets.html.erb", "app/views/shared/_bootstrapcdn_assets.html.erb" + template "_cdn_assets.html.erb", "app/views/shared/_cdn_assets.html.erb" end end @@ -50,7 +50,7 @@ def skip_cdn? end def app_resources - route_names.reject { |name| /^rails_info.*/.match(name) || /^rails_mailers.*/.match(name) || name.pluralize != name } + route_names.reject { |name| /^rails_*/.match(name) || /batch_action*/.match(name) || /admin_*/.match(name) || name.pluralize != name } end def devise_routes @@ -60,7 +60,7 @@ def devise_routes end def route_names - @route_names ||= Rails.application.routes.routes.map(&:name).uniq.compact + @route_names ||= Rails.application.routes.routes.reject { |route| route.verb != "GET" }.map(&:name).uniq.compact end end end diff --git a/lib/generators/draft/layout/templates/_bootstrapcdn_assets.html.erb b/lib/generators/draft/layout/templates/_bootstrapcdn_assets.html.erb deleted file mode 100644 index 1c658843..00000000 --- a/lib/generators/draft/layout/templates/_bootstrapcdn_assets.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<% if bootswatch? -%> - -<% else -%> - -<% end -%> - - - - - diff --git a/lib/generators/draft/layout/templates/_cdn_assets.html.erb b/lib/generators/draft/layout/templates/_cdn_assets.html.erb new file mode 100644 index 00000000..c87ed4ee --- /dev/null +++ b/lib/generators/draft/layout/templates/_cdn_assets.html.erb @@ -0,0 +1,13 @@ + +<% if bootswatch? -%> + +<% else -%> + +<% end -%> + + + + + + + diff --git a/lib/generators/draft/layout/templates/layout.html.erb b/lib/generators/draft/layout/templates/layout.html.erb index 37e68178..1b0c07dc 100644 --- a/lib/generators/draft/layout/templates/layout.html.erb +++ b/lib/generators/draft/layout/templates/layout.html.erb @@ -5,23 +5,24 @@ <%= Rails.application.class.parent_name %> -<% unless skip_cdn? -%> - <%%= render "shared/bootstrapcdn_assets" %> + + + + + -<% end -%> <%%= csrf_meta_tags %> - - <%%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - <%%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> - - - - + <%%= csp_meta_tag %> + + <%%= stylesheet_link_tag 'application', media: 'all' %> + <%%# javascript_pack_tag 'application' %> + + + <%%= render "shared/cdn_assets" %> - diff --git a/lib/generators/draft/resource/resource_generator.rb b/lib/generators/draft/resource/resource_generator.rb index 7d49da2a..c5e3366b 100644 --- a/lib/generators/draft/resource/resource_generator.rb +++ b/lib/generators/draft/resource/resource_generator.rb @@ -1,3 +1,5 @@ +require 'indefinite_article' + module Draft class ResourceGenerator < Rails::Generators::NamedBase source_root File.expand_path("../templates", __FILE__) @@ -30,13 +32,13 @@ def generate_model end def create_root_folder - empty_directory File.join("app/views", "#{singular_table_name}_templates") + empty_directory File.join("app/views", "#{plural_table_name}") end def generate_view_files available_views.each do |view| filename = view_filename_with_extensions(view) - template filename, File.join("app/views", "#{singular_table_name}_templates", File.basename(options[:new_form_name].presence || filename)) + template filename, File.join("app/views", "#{plural_table_name}", File.basename(options[:new_form_name].presence || filename)) end end @@ -46,7 +48,7 @@ def generate_routes if read_only? read_only_routes else - golden_seven_routes + golden_five_routes end end @@ -61,7 +63,7 @@ def generate_specs private - def golden_seven_routes + def golden_five_routes log :route, "RESTful routes" route <<-RUBY.gsub(/^ /, "") @@ -69,19 +71,19 @@ def golden_seven_routes # Routes for the #{singular_table_name.humanize} resource: # CREATE - get("/#{plural_table_name}/new", { :controller => "#{plural_table_name}", :action => "new_form" }) - #{skip_post? ? "get" : "post"}("/create_#{singular_table_name}", { :controller => "#{plural_table_name}", :action => "create_row" }) - + post("/insert_#{singular_table_name}", { :controller => "#{plural_table_name}", :action => "create" }) + # READ get("/#{plural_table_name}", { :controller => "#{plural_table_name}", :action => "index" }) - get("/#{plural_table_name}/:id_to_display", { :controller => "#{plural_table_name}", :action => "show" }) - + + get("/#{plural_table_name}/:path_id", { :controller => "#{plural_table_name}", :action => "show" }) + # UPDATE - get("/#{plural_table_name}/:prefill_with_id/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" }) - + + post("/modify_#{singular_table_name}/:path_id", { :controller => "#{plural_table_name}", :action => "update" }) + # DELETE - get("/delete_#{singular_table_name}/:id_to_remove", { :controller => "#{plural_table_name}", :action => "destroy_row" }) + get("/delete_#{singular_table_name}/:path_id", { :controller => "#{plural_table_name}", :action => "destroy" }) #------------------------------ RUBY @@ -95,9 +97,9 @@ def read_only_routes # Routes for the #{singular_table_name.humanize} resource: # READ - get("/#{plural_table_name}", { :controller => "#{plural_table_name}", :action => "index" }) - get("/#{plural_table_name}/:id_to_display", { :controller => "#{plural_table_name}", :action => "show" }) - + get("/#{plural_table_name}", { :controller => "#{plural_table_name}", :action => "index", :via => "get"}) + get("/#{plural_table_name}/:path_id", { :controller => "#{plural_table_name}", :action => "show", :via => "get"}) + #------------------------------ RUBY end @@ -158,7 +160,7 @@ def available_views elsif only_new_form? %w(association_new_form) else - %w(index new_form new_form_with_errors edit_form edit_form_with_errors show) + %w(index show) end end diff --git a/lib/generators/draft/resource/templates/controllers/controller.rb b/lib/generators/draft/resource/templates/controllers/controller.rb index 5f930053..a7766f4f 100644 --- a/lib/generators/draft/resource/templates/controllers/controller.rb +++ b/lib/generators/draft/resource/templates/controllers/controller.rb @@ -1,39 +1,41 @@ class <%= plural_table_name.camelize %>Controller < ApplicationController def index - @<%= plural_table_name.underscore %> = <%= class_name.singularize %>.all + matching_<%= plural_table_name.underscore %> = <%= class_name.singularize %>.all - render("<%= singular_table_name.underscore %>_templates/index.html.erb") + @list_of_<%= plural_table_name.underscore %> = matching_<%= plural_table_name.underscore %>.order({ :created_at => :desc }) + + render({ :template => "<%= plural_table_name.underscore %>/index.html.erb" }) end def show - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params.fetch("id_to_display")) + the_id = params.fetch("path_id") - render("<%= singular_table_name.underscore %>_templates/show.html.erb") - end + matching_<%= plural_table_name.underscore.downcase %> = <%= class_name.singularize %>.where({ :id => the_id }) - def new_form - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new + @the_<%= singular_table_name.underscore %> = matching_<%= plural_table_name.underscore.downcase %>.at(0) - render("<%= singular_table_name.underscore %>_templates/new_form.html.erb") + render({ :template => "<%= plural_table_name.underscore %>/show.html.erb" }) end - def create_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new - + def create + the_<%= singular_table_name.underscore %> = <%= class_name.singularize %>.new <% attributes.each do |attribute| -%> - @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("<%= attribute.column_name %>") +<% if attribute.field_type == :check_box -%> + the_<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>", false) +<% else -%> + the_<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>") +<% end -%> <% end -%> <% unless skip_validation_alerts? -%> - if @<%= singular_table_name.underscore %>.valid? - @<%= singular_table_name.underscore %>.save - - redirect_back(:fallback_location => "/<%= @plural_table_name.underscore %>", :notice => "<%= singular_table_name.humanize %> created successfully.") + if the_<%= singular_table_name.underscore %>.valid? + the_<%= singular_table_name.underscore %>.save + redirect_to("/<%= plural_table_name.underscore %>", { :notice => "<%= singular_table_name.humanize %> created successfully." }) else - render("<%= singular_table_name.underscore %>_templates/new_form_with_errors.html.erb") + redirect_to("/<%= plural_table_name.underscore %>", { :notice => "<%= singular_table_name.humanize %> failed to create successfully." }) end <% else -%> - @<%= singular_table_name.underscore %>.save + the_<%= singular_table_name.underscore %>.save <% unless skip_redirect? -%> redirect_to("/<%= @plural_table_name.underscore %>") @@ -45,52 +47,51 @@ def create_row <% end -%> end - def edit_form - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params.fetch("prefill_with_id")) - - render("<%= singular_table_name.underscore %>_templates/edit_form.html.erb") - end - - def update_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params.fetch("id_to_modify")) + def update + the_id = params.fetch("path_id") + the_<%= singular_table_name.underscore %> = <%= class_name.singularize %>.where({ :id => the_id }).at(0) <% attributes.each do |attribute| -%> - @<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("<%= attribute.column_name %>") +<% if attribute.field_type == :check_box -%> + the_<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>", false) +<% else -%> + the_<%= singular_table_name.underscore %>.<%= attribute.column_name %> = params.fetch("query_<%= attribute.column_name %>") +<% end -%> <% end -%> <% unless skip_validation_alerts? -%> - if @<%= singular_table_name.underscore %>.valid? - @<%= singular_table_name.underscore %>.save - - redirect_to("/<%= @plural_table_name.underscore %>/#{@<%= singular_table_name.underscore %>.id}", :notice => "<%= singular_table_name.humanize %> updated successfully.") + if the_<%= singular_table_name.underscore %>.valid? + the_<%= singular_table_name.underscore %>.save + redirect_to("/<%= plural_table_name.underscore %>/#{the_<%= singular_table_name.underscore %>.id}", { :notice => "<%= singular_table_name.humanize %> updated successfully."} ) else - render("<%= singular_table_name.underscore %>_templates/edit_form_with_errors.html.erb") + redirect_to("/<%= plural_table_name.underscore %>/#{the_<%= singular_table_name.underscore %>.id}", { :alert => "<%= singular_table_name.humanize %> failed to update successfully." }) end <% else -%> - @<%= singular_table_name.underscore %>.save + the_<%= singular_table_name.underscore %>.save <% unless skip_redirect? -%> - redirect_to("/<%= @plural_table_name.underscore %>/#{@<%= singular_table_name.underscore %>.id}") + redirect_to("/<%= plural_table_name.underscore %>/#{the_<%= singular_table_name.underscore %>.id}") <% else -%> - render("<%= singular_table_name.underscore %>_templates/update_row.html.erb") -<% end -%> + render({ :template => "<%= plural_table_name.underscore %>/show.html.erb" }) + <% end -%> <% end -%> end - def destroy_row - @<%= singular_table_name.underscore %> = <%= class_name.singularize %>.find(params.fetch("id_to_remove")) + def destroy + the_id = params.fetch("path_id") + the_<%= singular_table_name.underscore %> = <%= class_name.singularize %>.where({ :id => the_id }).at(0) - @<%= singular_table_name.underscore %>.destroy + the_<%= singular_table_name.underscore %>.destroy <% unless skip_validation_alerts? -%> - redirect_to("/<%= @plural_table_name.underscore %>", :notice => "<%= singular_table_name.humanize %> deleted successfully.") + redirect_to("/<%= plural_table_name.underscore %>", { :notice => "<%= singular_table_name.humanize %> deleted successfully."} ) <% else -%> <% unless skip_redirect? -%> - redirect_to("/<%= @plural_table_name.underscore %>") + redirect_to("/<%= plural_table_name.underscore %>") <% else -%> @remaining_count = <%= class_name.singularize %>.count - - render("<%= singular_table_name.underscore %>_templates/destroy_row.html.erb") + + redirect_to("/<%= plural_table_name.underscore %>", { :notice => "<%= singular_table_name.humanize %> deleted successfully."} ) <% end -%> <% end -%> end diff --git a/lib/generators/draft/resource/templates/views/association_new_form.html.erb b/lib/generators/draft/resource/templates/views/association_new_form.html.erb index 26cdfe6c..b089dced 100644 --- a/lib/generators/draft/resource/templates/views/association_new_form.html.erb +++ b/lib/generators/draft/resource/templates/views/association_new_form.html.erb @@ -19,20 +19,20 @@ <% end -%> <% elsif attribute.field_type == :check_box -%> -
+
<% if with_sentinels? -%> <% end -%> - <%%= "checked" if @<%= singular_table_name %>.<%= attribute.column_name %> %><% end -%>> + <%%= "checked" if @<%= singular_table_name %>.<%= attribute.column_name %> %><% end -%>> <% if with_sentinels? -%> <% end -%>
<% else -%> -
+
@@ -41,7 +41,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -49,7 +49,7 @@ <% if with_sentinels? -%> <% end -%> - value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> + value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> <% if with_sentinels? -%> <% end -%> @@ -61,7 +61,7 @@ <% end -%> <% end -%> - 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 4b29fac4..ee86d0e3 100644 --- a/lib/generators/draft/resource/templates/views/edit_form.html.erb +++ b/lib/generators/draft/resource/templates/views/edit_form.html.erb @@ -17,21 +17,21 @@ <% end -%> <% if attribute.field_type == :check_box -%> -
+
<% if with_sentinels? -%> <% end -%> - .<%= attribute.column_name %> %>> + .<%= attribute.column_name %> %>> <% if with_sentinels? -%> <% end -%> -
<% else -%> -
+
@@ -40,7 +40,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -48,7 +48,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -60,7 +60,7 @@ <% 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 index 1f63ca02..1e3053f1 100644 --- 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 @@ -4,7 +4,7 @@ <% end -%> <%% if @<%= singular_table_name %>.errors.any? %> <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> -
+
<%%= message %>
<%% end %> @@ -30,21 +30,21 @@ <% end -%> <% if attribute.field_type == :check_box -%> -
+
<% if with_sentinels? -%> <% end -%> - .<%= attribute.column_name %> %>> + .<%= attribute.column_name %> %>> <% if with_sentinels? -%> <% end -%> -
<% else -%> -
+
@@ -53,7 +53,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -61,7 +61,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -73,7 +73,7 @@ <% end -%> <% end -%> - diff --git a/lib/generators/draft/resource/templates/views/index.html.erb b/lib/generators/draft/resource/templates/views/index.html.erb index 2dea0e2f..7688ebc5 100644 --- a/lib/generators/draft/resource/templates/views/index.html.erb +++ b/lib/generators/draft/resource/templates/views/index.html.erb @@ -1,15 +1,53 @@ -
-
-

- All <%= plural_table_name.humanize.downcase %> -

+

+ List of all <%= plural_table_name.humanize.downcase %> +

+ - +
+ +

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 -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :datetime -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :date -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :time -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :integer -%> + +<% else -%> + +<% end -%> +
+ +<% end -%> +<% end -%> + +
+ +
<% if with_sentinels? -%> @@ -18,13 +56,12 @@ -<% end -%>
-
-
- +<% end -%> + +
- <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> + <%% @list_of_<%= plural_table_name %>.each do |<%= singular_table_name.indefinitize.gsub(" ", "_") %>| %> <% if with_sentinels? -%> - + <% end -%> <% if with_sentinels? -%> - + <% end -%> <% attributes.each do |attribute| -%> <% if with_sentinels? -%> - + <% end -%> <% if with_sentinels? -%> - + <% end -%> <% end -%> <% if with_sentinels? -%> - + <% end -%> <% if with_sentinels? -%> - + - + <% end -%> <% if with_sentinels? -%> - + <% end -%> <%% end %>
ID @@ -48,55 +85,55 @@
- <%%= <%= singular_table_name %>.id %> + <%%= <%= singular_table_name.indefinitize.gsub(" ", "_") %>.id %> - <%%= <%= singular_table_name %>.<%= attribute.column_name %> %> + <%%= <%= singular_table_name.indefinitize.gsub(" ", "_") %>.<%= attribute.column_name %> %> - <%%= time_ago_in_words(<%= singular_table_name %>.created_at) %> ago + <%%= time_ago_in_words(<%= singular_table_name.indefinitize.gsub(" ", "_") %>.created_at) %> ago - <%%= time_ago_in_words(<%= singular_table_name %>.updated_at) %> ago + <%%= time_ago_in_words(<%= singular_table_name.indefinitize.gsub(" ", "_") %>.updated_at) %> ago - + .id %>"> 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 1d81d37d..02563aaa 100644 --- a/lib/generators/draft/resource/templates/views/new_form.html.erb +++ b/lib/generators/draft/resource/templates/views/new_form.html.erb @@ -18,21 +18,21 @@ <% end -%> <% if attribute.field_type == :check_box -%> -
+
<% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> -
<% else -%> -
+
@@ -41,7 +41,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -49,7 +49,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -61,7 +61,7 @@ <% 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 index cb6232b6..330a7755 100644 --- 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 @@ -3,7 +3,7 @@ <%% if @<%= singular_table_name %>.present? %> <%% if @<%= singular_table_name %>.errors.any? %> <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> -
+
<%%= message %>
<%% end %> @@ -31,21 +31,21 @@ <% end -%> <% if attribute.field_type == :check_box -%> -
+
<% if with_sentinels? -%> <% end -%> - <%%= "checked" if @<%= singular_table_name %>.<%= attribute.column_name %> %><% end -%>> + <%%= "checked" if @<%= singular_table_name %>.<%= attribute.column_name %> %><% end -%>> <% if with_sentinels? -%> <% end -%> -
<% else -%> -
+
@@ -54,7 +54,7 @@ <% if with_sentinels? -%> <% end -%> - + <% if with_sentinels? -%> <% end -%> @@ -62,7 +62,7 @@ <% if with_sentinels? -%> <% end -%> - value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> + value="<%%= @<%= singular_table_name %>.<%= attribute.column_name %> %>"<% end -%>> <% if with_sentinels? -%> <% end -%> @@ -74,7 +74,7 @@ <% end -%> <% end -%> - diff --git a/lib/generators/draft/resource/templates/views/show.html.erb b/lib/generators/draft/resource/templates/views/show.html.erb index 91b2cc1b..2ba15abe 100644 --- a/lib/generators/draft/resource/templates/views/show.html.erb +++ b/lib/generators/draft/resource/templates/views/show.html.erb @@ -1,34 +1,21 @@ -
-
-

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

- -
- -<% if with_sentinels? -%> - -<% end -%> -
- - Edit <%= singular_table_name.humanize.downcase %> +

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

+ + +
-<% if with_sentinels? -%> - -<% end -%> <% unless read_only? -%> <% if with_sentinels? -%> <% end -%> -
- + @@ -36,7 +23,6 @@ <% end -%> <% end -%> -
<% attributes.each do |attribute| -%> @@ -44,13 +30,13 @@ <%= attribute.human_name %> <% if with_sentinels? -%> - + <% end -%>
- <%%= @<%= singular_table_name %>.<%= attribute.column_name %> %> + <%%= @the_<%= singular_table_name %>.<%= attribute.column_name %> %>
<% if with_sentinels? -%> - + <% end -%> <% end -%> @@ -61,7 +47,7 @@ <% end -%>
- <%%= time_ago_in_words(@<%= singular_table_name %>.created_at) %> ago + <%%= time_ago_in_words(@the_<%= singular_table_name %>.created_at) %> ago
<% if with_sentinels? -%> @@ -74,17 +60,63 @@ <% end -%>
- <%%= time_ago_in_words(@<%= singular_table_name %>.updated_at) %> ago + <%%= time_ago_in_words(@the_<%= singular_table_name %>.updated_at) %> ago
<% if with_sentinels? -%> <% end -%>
-
-
+ +
<% if with_sentinels? -%> <% end -%> + +

+ Edit <%= singular_table_name.humanize.downcase %> +

+ +
method="post" <% end -%>> +<% attributes.each do |attribute| -%> +<% if attribute.field_type == :check_box -%> +
+ .<%= attribute.column_name %> %>> + + +
+ +<% else -%> +
+ + +<% if attribute.field_type == :text_area -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :datetime -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :date -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :time -%> + +<% elsif attribute.field_type.to_s.gsub(/_.*/, "").to_sym == :integer -%> + +<% else -%> + +<% end -%> +
+ +<% end -%> +<% end -%> + +
+ +