Skip to content

Commit

Permalink
make generators add namespacing to components and views as per phlex v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyforbusiness committed Oct 16, 2024
1 parent 594fdbc commit 444b9a1
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 65 deletions.
12 changes: 6 additions & 6 deletions lib/generators/phlex/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ 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

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
Expand All @@ -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
33 changes: 0 additions & 33 deletions lib/generators/phlex/install/templates/application_component.rb

This file was deleted.

22 changes: 22 additions & 0 deletions lib/generators/phlex/install/templates/base_component.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/generators/phlex/install/templates/controller.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ 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

# 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
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions lib/generators/rails/phlex_scaffold/phlex_scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -159,17 +159,17 @@ 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
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})
Expand Down
22 changes: 22 additions & 0 deletions test/dummy/app/components/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/posts/index_view.rb
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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 -%>

0 comments on commit 444b9a1

Please sign in to comment.