diff --git a/lib/generators/phlex/install/install_generator.rb b/lib/generators/phlex/install/install_generator.rb index a9d02985..8b057d9b 100644 --- a/lib/generators/phlex/install/install_generator.rb +++ b/lib/generators/phlex/install/install_generator.rb @@ -15,11 +15,11 @@ def configure_tailwind end end - def create_application_component + def create_base_component template "base_component.rb.erb", Rails.root.join("app/components/base.rb") end - def create_application_view + def create_base_view template "base_view.rb.erb", Rails.root.join("app/views/base.rb") end @@ -27,6 +27,10 @@ def create_initializer template "phlex.rb.erb", Rails.root.join("config/initializers/phlex.rb") end + def create_scaffold_controller_template + copy_file "controller.rb.tt", Rails.root.join("lib/templates/rails/scaffold_controller/controller.rb.tt") + end + private def tailwind_configuration_path @@ -46,9 +50,5 @@ def tailwind_configuration_files ], ) end - - def create_scaffold_controller_template - copy_file "controller.rb.tt", Rails.root.join("lib/templates/rails/scaffold_controller/controller.rb.tt") - end end end diff --git a/lib/generators/phlex/install/templates/application_component.rb b/lib/generators/phlex/install/templates/application_component.rb deleted file mode 100644 index 68250e88..00000000 --- a/lib/generators/phlex/install/templates/application_component.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -class ApplicationComponent < Phlex::HTML - include Phlex::Rails::Helpers::Routes - include Phlex::Rails::Helpers::LinkTo - include Phlex::Rails::Helpers::DOMID - include Phlex::Rails::Helpers::FormWith - include Phlex::Rails::Helpers::ButtonTo - include Phlex::Rails::Helpers::Pluralize - - register_value_helper :notice - - private def format_object(object) - # Name the Classes explicitly otherwise you get unexpected results - # e.g. an empty Array will return '[]' which looks bad on the index page - super || case object - when TrueClass, - FalseClass, - BigDecimal, - ActiveSupport::TimeWithZone, - Date - - object.to_s - end - end - - if Rails.env.development? - def before_template - comment { "Before #{self.class.name}" } - super - end - end -end diff --git a/lib/generators/phlex/install/templates/base_component.rb.erb b/lib/generators/phlex/install/templates/base_component.rb.erb index dfe3e7b6..2dc2e7a8 100644 --- a/lib/generators/phlex/install/templates/base_component.rb.erb +++ b/lib/generators/phlex/install/templates/base_component.rb.erb @@ -5,6 +5,28 @@ class Components::Base < Phlex::HTML # Include any helpers you want to be available across all components include Phlex::Rails::Helpers::Routes + # The following are used by the phlex_scaffold generator but can be removed if you don't use that + include Phlex::Rails::Helpers::LinkTo + include Phlex::Rails::Helpers::DOMID + include Phlex::Rails::Helpers::FormWith + include Phlex::Rails::Helpers::ButtonTo + include Phlex::Rails::Helpers::Pluralize + include Phlex::Rails::Helpers::Notice + + # Used by the phlex_scaffold generator + private def format_object(object) + # Name the Classes explicitly otherwise you get unexpected results + # e.g. an empty Array will return '[]' which looks bad on the index page + super || case object + when TrueClass, + FalseClass, + BigDecimal, + ActiveSupport::TimeWithZone, + Date + + object.to_s + end + end if Rails.env.development? def before_template diff --git a/lib/generators/phlex/install/templates/controller.rb.tt b/lib/generators/phlex/install/templates/controller.rb.tt index 9ac2180f..9d63cb08 100644 --- a/lib/generators/phlex/install/templates/controller.rb.tt +++ b/lib/generators/phlex/install/templates/controller.rb.tt @@ -15,7 +15,7 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= plural_table_name %> = <%= orm_class.all(class_name) %> respond_to do |format| - format.html { render <%= class_name.pluralize %>::IndexView.new(<%= plural_name %>: @<%= plural_table_name %>) } + format.html { render Views::<%= class_name.pluralize %>::Index.new(<%= plural_name %>: @<%= plural_table_name %>) } format.json end end @@ -23,19 +23,19 @@ class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %>/1 or <%= route_url %>/1.json def show respond_to do |format| - format.html { render <%= class_name.pluralize %>::ShowView.new(<%= singular_name %>: @<%= singular_name %>) } + format.html { render Views::<%= class_name.pluralize %>::Show.new(<%= singular_name %>: @<%= singular_name %>) } format.json end end # GET <%= route_url %>/new def new - render <%= class_name.pluralize %>::NewView.new(<%= singular_name %>: <%= orm_class.build(class_name) %>) + render Views::<%= class_name.pluralize %>::New.new(<%= singular_name %>: <%= orm_class.build(class_name) %>) end # GET <%= route_url %>/1/edit def edit - render <%= class_name.pluralize %>::EditView.new(<%= singular_name %>: @<%= singular_name %>) + render Views::<%= class_name.pluralize %>::Edit.new(<%= singular_name %>: @<%= singular_name %>) end # POST <%= route_url %> or <%= route_url %>.json @@ -47,7 +47,7 @@ class <%= controller_class_name %>Controller < ApplicationController format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> } format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render <%= class_name.pluralize %>::NewView.new(<%= singular_name %>: @<%= singular_table_name %>), status: :unprocessable_entity } + format.html { render Views::<%= class_name.pluralize %>::New.new(<%= singular_name %>: @<%= singular_table_name %>), status: :unprocessable_entity } format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end @@ -60,7 +60,7 @@ class <%= controller_class_name %>Controller < ApplicationController format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> } format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } else - format.html { render <%= class_name.pluralize %>::EditView.new(<%= singular_name %>: @<%= singular_table_name %>), + format.html { render Views::<%= class_name.pluralize %>::Edit.new(<%= singular_name %>: @<%= singular_table_name %>), status: :unprocessable_entity } format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end diff --git a/lib/generators/rails/phlex_scaffold/phlex_scaffold_generator.rb b/lib/generators/rails/phlex_scaffold/phlex_scaffold_generator.rb index 0536e7ab..d02d85aa 100644 --- a/lib/generators/rails/phlex_scaffold/phlex_scaffold_generator.rb +++ b/lib/generators/rails/phlex_scaffold/phlex_scaffold_generator.rb @@ -12,9 +12,9 @@ def create_root_folder end def create_model_view - create_file File.join(view_folder, "#{singular_name}_view.rb"), <<~RUBY - module #{module_name} - class #{class_name}View < ApplicationView + create_file File.join(view_folder, "#{singular_name}.rb"), <<~RUBY + module Views::#{module_name} + class #{class_name} < Views::Base def initialize(#{singular_name}:) @#{singular_name} = #{singular_name} end @@ -34,9 +34,9 @@ def view_template end def create_index_view - create_file File.join(view_folder, "index_view.rb"), <<~RUBY - module #{module_name} - class IndexView < ApplicationView + create_file File.join(view_folder, "index.rb"), <<~RUBY + module Views::#{module_name} + class Index < Views::Base def initialize(#{plural_name}:) @#{plural_name} = #{plural_name} end @@ -48,7 +48,7 @@ def view_template div(id: "#{plural_name}") do #{plural_name}.each do |#{singular_name}| - render #{class_name}View.new(#{singular_name}: #{singular_name}) + render #{class_name}.new(#{singular_name}: #{singular_name}) p do link_to "Show this #{singular_name.humanize}", #{singular_name} end @@ -67,9 +67,9 @@ def view_template end def create_new_view - create_file File.join(view_folder, "new_view.rb"), <<~RUBY - module #{module_name} - class NewView < ApplicationView + create_file File.join(view_folder, "new.rb"), <<~RUBY + module Views::#{module_name} + class New < Views::Base def initialize(#{singular_name}:) @#{singular_name} = #{singular_name} end @@ -95,9 +95,9 @@ def view_template end def create_edit_view - create_file File.join(view_folder, "edit_view.rb"), <<~RUBY - module #{module_name} - class EditView < ApplicationView + create_file File.join(view_folder, "edit.rb"), <<~RUBY + module Views::#{module_name} + class Edit < Views::Base def initialize(#{singular_name}:) @#{singular_name} = #{singular_name} end @@ -126,8 +126,8 @@ def view_template def create_form_view create_file File.join(view_folder, "form.rb"), <<~RUBY - module #{module_name} - class Form < ApplicationView + module Views::#{module_name} + class Form < Views::Base def initialize(#{singular_name}:) @#{singular_name} = #{singular_name} end @@ -159,9 +159,9 @@ def view_template end def create_show_view - create_file File.join(view_folder, "show_view.rb"), <<~RUBY - module #{module_name} - class ShowView < ApplicationView + create_file File.join(view_folder, "show.rb"), <<~RUBY + module Views::#{module_name} + class Show < Views::Base def initialize(#{singular_name}:) @#{singular_name} = #{singular_name} end @@ -169,7 +169,7 @@ def initialize(#{singular_name}:) def view_template p(style: "color:#008000") { notice } - render #{class_name}View.new(#{singular_name}: #{singular_name}) + render #{class_name}.new(#{singular_name}: #{singular_name}) div do link_to "Edit this #{human_name.downcase}", edit_#{singular_name}_path(#{singular_name}) diff --git a/test/dummy/app/components/base.rb b/test/dummy/app/components/base.rb index 70a6f29b..a53ff48d 100644 --- a/test/dummy/app/components/base.rb +++ b/test/dummy/app/components/base.rb @@ -5,6 +5,28 @@ class Components::Base < Phlex::HTML # Include any helpers you want to be available across all components include Phlex::Rails::Helpers::Routes + # The following are used by the phlex_scaffold generator but can be removed if you don't use that + include Phlex::Rails::Helpers::LinkTo + include Phlex::Rails::Helpers::DOMID + include Phlex::Rails::Helpers::FormWith + include Phlex::Rails::Helpers::ButtonTo + include Phlex::Rails::Helpers::Pluralize + include Phlex::Rails::Helpers::Notice + + # Used by the phlex_scaffold generator + private def format_object(object) + # Name the Classes explicitly otherwise you get unexpected results + # e.g. an empty Array will return '[]' which looks bad on the index page + super || case object + when TrueClass, + FalseClass, + BigDecimal, + ActiveSupport::TimeWithZone, + Date + + object.to_s + end + end if Rails.env.development? def before_template diff --git a/test/dummy/app/views/posts/index_view.rb b/test/dummy/app/views/posts/index_view.rb index fb0cbe42..bce77bda 100644 --- a/test/dummy/app/views/posts/index_view.rb +++ b/test/dummy/app/views/posts/index_view.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Posts::IndexView < Views::Base +class Views::Posts::IndexView < Views::Base def view_template h1 { "Posts::Index" } aside { "Find me in app/views/posts/index_view.rb" } diff --git a/test/dummy/lib/templates/rails/scaffold_controller/controller.rb.tt b/test/dummy/lib/templates/rails/scaffold_controller/controller.rb.tt new file mode 100644 index 00000000..9d63cb08 --- /dev/null +++ b/test/dummy/lib/templates/rails/scaffold_controller/controller.rb.tt @@ -0,0 +1,95 @@ +<% # Based on gems/jbuilder-2.12.0/lib/generators/rails/templates/controller.rb %> +<% puts "#{'*' * 40}" %> +<% puts "Generating controller file from this template #{__FILE__} . Remove in order to restore Rails default." %> +<% puts "#{'*' * 40}" %> +<% 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 ] + + # GET <%= route_url %> or <%= route_url %>.json + def index + @<%= plural_table_name %> = <%= orm_class.all(class_name) %> + + respond_to do |format| + format.html { render Views::<%= class_name.pluralize %>::Index.new(<%= plural_name %>: @<%= plural_table_name %>) } + format.json + end + end + + # GET <%= route_url %>/1 or <%= route_url %>/1.json + def show + respond_to do |format| + format.html { render Views::<%= class_name.pluralize %>::Show.new(<%= singular_name %>: @<%= singular_name %>) } + format.json + end + end + + # GET <%= route_url %>/new + def new + render Views::<%= class_name.pluralize %>::New.new(<%= singular_name %>: <%= orm_class.build(class_name) %>) + end + + # GET <%= route_url %>/1/edit + def edit + render Views::<%= class_name.pluralize %>::Edit.new(<%= singular_name %>: @<%= singular_name %>) + end + + # POST <%= route_url %> or <%= route_url %>.json + def create + @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> + + respond_to do |format| + if @<%= orm_instance.save %> + format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> } + format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> } + else + format.html { render Views::<%= class_name.pluralize %>::New.new(<%= singular_name %>: @<%= singular_table_name %>), status: :unprocessable_entity } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT <%= route_url %>/1 or <%= route_url %>/1.json + def update + respond_to do |format| + if @<%= orm_instance.update("#{singular_table_name}_params") %> + format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> } + format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } + else + format.html { render Views::<%= class_name.pluralize %>::Edit.new(<%= singular_name %>: @<%= singular_table_name %>), + status: :unprocessable_entity } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + end + end + end + + # DELETE <%= route_url %>/1 or <%= route_url %>/1.json + def destroy + @<%= orm_instance.destroy %> + + respond_to do |format| + format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_<%= singular_table_name %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> + end + + # Only allow a list of trusted parameters through. + def <%= "#{singular_table_name}_params" %> + <%- if attributes_names.empty? -%> + params.fetch(:<%= singular_table_name %>, {}) + <%- else -%> + params.require(:<%= singular_table_name %>).permit(<%= permitted_params %>) + <%- end -%> + end +end +<% end -%> \ No newline at end of file