From b4a16b60062204c90a0b6f16507070da260e3680 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Thu, 18 Oct 2018 16:44:56 -0400 Subject: [PATCH 1/4] Add custom templates :tada: :tada: Now we can use our Radius custom templates for a scaffolded controller and action views. A project using this will still need to have `slim-rails` in it's own Gemfile since we are not monkey-patching any generators. However, because of that, we need to work around how the generators work. By default, rails will look for a template, and generate that first one it finds. Since our custom templates are in a gem, rails will find a template (default) in the project and spit it out. We want to check `radius-rails` first for any templates before going back to the defaults. Those changes are in `lib/radius-rails.rb`. --- lib/radius-rails.rb | 8 +++ lib/radius/rails/version.rb | 2 +- .../scaffold_controller/controller.rb.tt | 67 +++++++++++++++++++ .../slim/scaffold/_form.html.slim.tt | 44 ++++++++++++ lib/templates/slim/scaffold/edit.html.slim.tt | 10 +++ .../slim/scaffold/index.html.slim.tt | 27 ++++++++ lib/templates/slim/scaffold/new.html.slim.tt | 8 +++ lib/templates/slim/scaffold/show.html.slim.tt | 11 +++ 8 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 lib/templates/rails/scaffold_controller/controller.rb.tt create mode 100644 lib/templates/slim/scaffold/_form.html.slim.tt create mode 100644 lib/templates/slim/scaffold/edit.html.slim.tt create mode 100644 lib/templates/slim/scaffold/index.html.slim.tt create mode 100644 lib/templates/slim/scaffold/new.html.slim.tt create mode 100644 lib/templates/slim/scaffold/show.html.slim.tt diff --git a/lib/radius-rails.rb b/lib/radius-rails.rb index deacfe9..f50c7f3 100644 --- a/lib/radius-rails.rb +++ b/lib/radius-rails.rb @@ -1 +1,9 @@ require "radius/rails" + +module RadiusRails + class Railtie < Rails::Railtie + config.app_generators do |g| + g.templates.unshift File::expand_path('../templates', __FILE__) + end + end +end diff --git a/lib/radius/rails/version.rb b/lib/radius/rails/version.rb index 3b06d85..4f0ce31 100644 --- a/lib/radius/rails/version.rb +++ b/lib/radius/rails/version.rb @@ -1,5 +1,5 @@ module Radius module Rails - VERSION = "2.0.0" + VERSION = "2.1.0" end end diff --git a/lib/templates/rails/scaffold_controller/controller.rb.tt b/lib/templates/rails/scaffold_controller/controller.rb.tt new file mode 100644 index 0000000..d7573fe --- /dev/null +++ b/lib/templates/rails/scaffold_controller/controller.rb.tt @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +<% if namespaced? -%> +require_dependency "<%= namespaced_path %>/application_controller" + +<% end -%> +<% module_namespacing do -%> +class <%= controller_class_name %>Controller < ApplicationController + before_action :set_<%= singular_table_name %>, only: %i[show edit update destroy] + + def index + @<%= plural_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).all + end + + def show + end + + def new + @<%= singular_table_name %> = <%= orm_class.build(class_name) %> + end + + def edit + end + + def create + @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> + + if @<%= orm_instance.save %> + redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %> + else + render :new + end + end + + def update + if @<%= orm_instance.update("#{singular_table_name}_params") %> + redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> + else + render :edit + end + end + + def destroy + @<%= orm_instance.destroy %>! + redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> + end + +private + + def set_<%= singular_table_name %> + @<%= singular_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).find(params[:id]) + end + + def <%= "#{singular_table_name}_params" %> + <%- if attributes_names.empty? -%> + params.fetch(:<%= singular_table_name %>, {}) + <%- else -%> + params.require(:<%= singular_table_name %>) + .permit( +<% attributes_names.each do |name| -%> + :<%= name %>, +<% end -%> + ) + <%- end -%> + end +end +<% end -%> diff --git a/lib/templates/slim/scaffold/_form.html.slim.tt b/lib/templates/slim/scaffold/_form.html.slim.tt new file mode 100644 index 0000000..b1aaa84 --- /dev/null +++ b/lib/templates/slim/scaffold/_form.html.slim.tt @@ -0,0 +1,44 @@ += form_for @<%= singular_table_name %> do |f| + .row + .col-lg-8 + = render "layouts/partials/form_errors", resource: @<%= singular_table_name %> + + .row + .col-lg-8 + .panel.panel-default + .panel-heading + .panel-title <%= singular_table_name.titleize %> Details + .panel-body +<% attributes.each do |attribute| -%> + .form-group.has-feedback +<% if attribute.password_digest? -%> + = f.label :password + = f.password_field :password, class: "form-control" + .form-group.has-feedback + = f.label :password_confirmation + = f.password_field :password_confirmation, class: "form-control" +<% else -%> + = f.label :<%= attribute.column_name %> +<% case attribute.field_type -%> +<% when :check_box -%> + .checkbox.c-checkbox.mt0 + label + = f.check_box :<%= attribute.column_name %>, class: 'form-control' + span.fa.fa-check +<% when :datetime_select -%> + .datepicker.input-group.date + = f.text_field :<%= attribute.column_name %>, class: 'form-control' + span.input-group-addon + span.fa.fa-calendar +<% else -%> + = f.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control" +<% end -%> +<% end -%> +<% end -%> + + .row + .col-lg-8 + .panel.panel-default + .panel-body + = f.submit class: "btn btn-sm pull-right btn-primary" + = link_to 'Cancel', <%= index_helper %>_url, class: "btn btn-sm btn-default" diff --git a/lib/templates/slim/scaffold/edit.html.slim.tt b/lib/templates/slim/scaffold/edit.html.slim.tt new file mode 100644 index 0000000..1e18647 --- /dev/null +++ b/lib/templates/slim/scaffold/edit.html.slim.tt @@ -0,0 +1,10 @@ +.content-heading + | Edit <%= singular_table_name.titleize %> + +ol.breadcrumb + li + = link_to "<%= plural_table_name.titleize %>", <%= index_helper %>_url + li + = link_to "Back", @<%= singular_table_name %> + += render partial: 'form' diff --git a/lib/templates/slim/scaffold/index.html.slim.tt b/lib/templates/slim/scaffold/index.html.slim.tt new file mode 100644 index 0000000..34a7468 --- /dev/null +++ b/lib/templates/slim/scaffold/index.html.slim.tt @@ -0,0 +1,27 @@ +.content-heading + .btn-toolbar.pull-right + | <%= plural_table_name.titleize %> + | <%= plural_table_name.titleize %> + +ol.breadcrumb + li = link_to '<%= plural_table_name.titleize %>', <%= index_helper %>_path + +.row + .col-sm-12 + .panel.panel-default + .panel-heading + | My <%= plural_table_name.titleize %> + + table.table.table-hover.table-generic + thead + tr +<% attributes.reject(&:password_digest?).each do |attribute| -%> + th <%= attribute.human_name %> +<% end -%> + + tbody + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + tr +<% attributes.reject(&:password_digest?).each do |attribute| -%> + td = <%= singular_table_name %>.<%= attribute.name %> +<% end -%> diff --git a/lib/templates/slim/scaffold/new.html.slim.tt b/lib/templates/slim/scaffold/new.html.slim.tt new file mode 100644 index 0000000..ab36305 --- /dev/null +++ b/lib/templates/slim/scaffold/new.html.slim.tt @@ -0,0 +1,8 @@ +.content-heading + | New <%= singular_table_name.titleize %> + +ol.breadcrumb + li + = link_to "<%= plural_table_name.titleize %>", <%= index_helper %>_url + += render partial: 'form' diff --git a/lib/templates/slim/scaffold/show.html.slim.tt b/lib/templates/slim/scaffold/show.html.slim.tt new file mode 100644 index 0000000..2937828 --- /dev/null +++ b/lib/templates/slim/scaffold/show.html.slim.tt @@ -0,0 +1,11 @@ +p#notice = notice + +<% attributes.each do |attribute| -%> +p + strong <%= attribute.human_name %>: + = @<%= singular_table_name %>.<%= attribute.name %> +<% end -%> + +=> link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) +'| +=< link_to 'Back', <%= index_helper %>_path From f36c68b65729b9fa3eef7766cbaa467014704077 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Tue, 23 Oct 2018 16:20:59 -0400 Subject: [PATCH 2/4] Radius-ize the show.html.slim generator --- .../scaffold_controller/controller.rb.tt | 4 +-- .../slim/scaffold/_form.html.slim.tt | 15 +++------- .../slim/scaffold/index.html.slim.tt | 29 +++++++++++-------- lib/templates/slim/scaffold/show.html.slim.tt | 23 ++++++++++----- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/lib/templates/rails/scaffold_controller/controller.rb.tt b/lib/templates/rails/scaffold_controller/controller.rb.tt index d7573fe..2e204e1 100644 --- a/lib/templates/rails/scaffold_controller/controller.rb.tt +++ b/lib/templates/rails/scaffold_controller/controller.rb.tt @@ -26,7 +26,7 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> if @<%= orm_instance.save %> - redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> else render :new end @@ -34,7 +34,7 @@ class <%= controller_class_name %>Controller < ApplicationController def update if @<%= orm_instance.update("#{singular_table_name}_params") %> - redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> else render :edit end diff --git a/lib/templates/slim/scaffold/_form.html.slim.tt b/lib/templates/slim/scaffold/_form.html.slim.tt index b1aaa84..5767025 100644 --- a/lib/templates/slim/scaffold/_form.html.slim.tt +++ b/lib/templates/slim/scaffold/_form.html.slim.tt @@ -1,14 +1,10 @@ = form_for @<%= singular_table_name %> do |f| .row - .col-lg-8 - = render "layouts/partials/form_errors", resource: @<%= singular_table_name %> - - .row - .col-lg-8 + .col-sm-12.col-md-10.col-lg-8 .panel.panel-default - .panel-heading - .panel-title <%= singular_table_name.titleize %> Details + .panel-body + = render "layouts/partials/form_errors", resource: @<%= singular_table_name %> <% attributes.each do |attribute| -%> .form-group.has-feedback <% if attribute.password_digest? -%> @@ -36,9 +32,6 @@ <% end -%> <% end -%> - .row - .col-lg-8 - .panel.panel-default - .panel-body + .panel-footer = f.submit class: "btn btn-sm pull-right btn-primary" = link_to 'Cancel', <%= index_helper %>_url, class: "btn btn-sm btn-default" diff --git a/lib/templates/slim/scaffold/index.html.slim.tt b/lib/templates/slim/scaffold/index.html.slim.tt index 34a7468..0c16090 100644 --- a/lib/templates/slim/scaffold/index.html.slim.tt +++ b/lib/templates/slim/scaffold/index.html.slim.tt @@ -1,27 +1,32 @@ .content-heading .btn-toolbar.pull-right - | <%= plural_table_name.titleize %> + = link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path, class: 'btn btn-sm btn-success', title: "Create new <%= singular_table_name %>" | <%= plural_table_name.titleize %> ol.breadcrumb li = link_to '<%= plural_table_name.titleize %>', <%= index_helper %>_path .row - .col-sm-12 + .col-lg-12 .panel.panel-default .panel-heading - | My <%= plural_table_name.titleize %> - - table.table.table-hover.table-generic - thead - tr + .table-responsive + table.table.table-hover.table-generic + thead + tr <% attributes.reject(&:password_digest?).each do |attribute| -%> - th <%= attribute.human_name %> + th <%= attribute.human_name %> <% end -%> + th + th + th - tbody - - @<%= plural_table_name %>.each do |<%= singular_table_name %>| - tr + tbody + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + tr <% attributes.reject(&:password_digest?).each do |attribute| -%> - td = <%= singular_table_name %>.<%= attribute.name %> + td = <%= singular_table_name %>.<%= attribute.name %> <% end -%> + td = link_to 'Show', <%= singular_table_name %> + td = link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) + td = link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/lib/templates/slim/scaffold/show.html.slim.tt b/lib/templates/slim/scaffold/show.html.slim.tt index 2937828..6f35298 100644 --- a/lib/templates/slim/scaffold/show.html.slim.tt +++ b/lib/templates/slim/scaffold/show.html.slim.tt @@ -1,11 +1,18 @@ -p#notice = notice +.content-heading + .btn-toolbar.pull-right + = link_to "Edit", edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), class: 'btn btn-sm btn-info' + | <%= singular_table_name.titleize %> +ol.breadcrumb + li + = link_to "<%= plural_table_name.titleize %>", <%= index_helper %>_url + +.row + .col-sm-12.col-md-10.col-lg-8 + .panel.panel-default + .panel-body <% attributes.each do |attribute| -%> -p - strong <%= attribute.human_name %>: - = @<%= singular_table_name %>.<%= attribute.name %> + p + strong <%= attribute.human_name %>: + = @<%= singular_table_name %>.<%= attribute.name %> <% end -%> - -=> link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) -'| -=< link_to 'Back', <%= index_helper %>_path From f8d6ebc72f95365898fd98dd2880b59965e210bd Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Tue, 23 Oct 2018 16:36:22 -0400 Subject: [PATCH 3/4] Explicitly state top-level Rails namespace --- lib/radius-rails.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/radius-rails.rb b/lib/radius-rails.rb index f50c7f3..d83fcca 100644 --- a/lib/radius-rails.rb +++ b/lib/radius-rails.rb @@ -1,9 +1,11 @@ require "radius/rails" -module RadiusRails - class Railtie < Rails::Railtie - config.app_generators do |g| - g.templates.unshift File::expand_path('../templates', __FILE__) +module Radius + module Rails + class Railtie < ::Rails::Railtie + config.app_generators do |g| + g.templates.unshift File.expand_path('templates', __dir__) + end end end end From 49ff04424d9e3b7951babb4d3deae49d3b910a95 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Tue, 23 Oct 2018 16:39:14 -0400 Subject: [PATCH 4/4] Generic styling for generic tables --- .../radius-theme/app/customizations.scss | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/assets/stylesheets/radius-theme/app/customizations.scss b/app/assets/stylesheets/radius-theme/app/customizations.scss index 59ffcc1..6381e1a 100644 --- a/app/assets/stylesheets/radius-theme/app/customizations.scss +++ b/app/assets/stylesheets/radius-theme/app/customizations.scss @@ -2,3 +2,20 @@ float: none; margin: 0 auto; } + +.table-generic tr td a { + display: block; + text-decoration: none; + color: #656565; + &:hover { + color: #656565; + text-decoration: none; + } +} + +.table.table-hover.table-generic { + th, td { + text-align: center; + vertical-align: middle; + } +}