Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 generators with kits and module name spacing #222

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ AllCops:
TargetRubyVersion: 3.3.3
Exclude:
- "lib/phlex-rails.rb"
- "lib/generators/**/*"
SuggestExtensions: false

Style/MissingRespondToMissing:
Enabled: false

Style/RescueStandardError:
Enabled: false
4 changes: 2 additions & 2 deletions lib/generators/phlex/component/USAGE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Description:
Generates a Phlex component with the given name
Generates a new Phlex component

Example:
rails generate phlex:component Sidebar

This will create:
app/views/components/sidebar_component.rb
app/components/sidebar.rb
2 changes: 1 addition & 1 deletion lib/generators/phlex/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)

def create_view
@path = File.join("app/views/components", class_path, "#{file_name}_component.rb")
@path = File.join("app/components", class_path, "#{file_name}.rb")
template "component.rb.erb", @path
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/generators/phlex/component/templates/component.rb.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true

<% module_namespacing do -%>
class <%= class_name %>Component < ApplicationComponent
class Components::<%= class_name %> < Components::Base
def view_template
h1 { "<%= class_name %>" }
p { "Find me in <%= @path %>" }
end
end<% end %>
end
4 changes: 3 additions & 1 deletion lib/generators/phlex/install/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ Example:
rails generate phlex:install

This will create:
app/views/application_view.rb
app/views/base.rb
app/components/base.rb
app/initializers/phlex.rb
47 changes: 18 additions & 29 deletions lib/generators/phlex/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,38 @@ class InstallGenerator < ::Rails::Generators::Base

APPLICATION_CONFIGURATION_PATH = Rails.root.join("config/application.rb")

ADD_EXTRA_AUTOLOAD_PATHS_CODE = <<-ADD_EXTRA_AUTOLOAD_PATHS_CODE
config.autoload_paths.push(
"\#{root}/app/views/components",
"\#{root}/app/views",
"\#{root}/app/views/layouts"
)
ADD_EXTRA_AUTOLOAD_PATHS_CODE

def autoload_components_layouts_views
return unless APPLICATION_CONFIGURATION_PATH.exist?

inject_into_class(
APPLICATION_CONFIGURATION_PATH,
"Application",
ADD_EXTRA_AUTOLOAD_PATHS_CODE,
)
end

def configure_tailwind
return unless tailwind_configuration_path.exist?

insert_into_file tailwind_configuration_path, after: "content: [" do
"\n './app/views/**/*.rb'," \
if tailwind_configuration_path
insert_into_file tailwind_configuration_path, after: "content: [" do
'\n "./app/views/**/*.rb",'
end
end
end

def create_application_component
template "application_component.rb", Rails.root.join("app/views/components/application_component.rb")
template "base_component.rb.erb", Rails.root.join("app/components/base.rb")
end

def create_application_layout
template "application_layout.rb", Rails.root.join("app/views/layouts/application_layout.rb")
def create_application_view
template "base_view.rb.erb", Rails.root.join("app/views/base.rb")
end

def create_application_view
template "application_view.rb", Rails.root.join("app/views/application_view.rb")
def create_hello_component
template "hello_component.rb.erb", Rails.root.join("app/components/hello.rb")
end

def create_initializer
template "phlex.rb.erb", Rails.root.join("config/initializers/phlex.rb")
end

private

def tailwind_configuration_path
@_tailwind_configuration_path ||=
Pathname.new(tailwind_configuration_files.first)
if tailwind_configuration_files.any?
Pathname.new(
tailwind_configuration_files.first,
)
end
end

def tailwind_configuration_files
Expand Down
12 changes: 0 additions & 12 deletions lib/generators/phlex/install/templates/application_component.rb

This file was deleted.

24 changes: 0 additions & 24 deletions lib/generators/phlex/install/templates/application_layout.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/generators/phlex/install/templates/application_view.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/generators/phlex/install/templates/base_component.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class Components::Base < Phlex::HTML
include Components

# Include any helpers you want to be available across all components
include Phlex::Rails::Helpers::Routes

if Rails.env.development?
def before_template
comment { "Before #{self.class.name}" }
super
end
end
end
9 changes: 9 additions & 0 deletions lib/generators/phlex/install/templates/base_view.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Views::Base < Components::Base
# The ApplicationView is an abstract class for all your views.

# By default, it inherits from `ApplicationComponent`, but you
# can change that to `Phlex::HTML` if you want to keep views and
# components independent.
end
11 changes: 11 additions & 0 deletions lib/generators/phlex/install/templates/hello_component.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Components::Hello < Components::Base
def initialize(name:)
@name = name
end

def view_template
h1 { "Hello #{@name}" }
end
end
9 changes: 9 additions & 0 deletions lib/generators/phlex/install/templates/phlex.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Components
extend Phlex::Kit
end

module Views
end

Rails.autoloaders.main.push_dir("#{Rails.root}/app/views", namespace: Views)
Rails.autoloaders.main.push_dir("#{Rails.root}/app/components", namespace: Components)
2 changes: 1 addition & 1 deletion lib/generators/phlex/view/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Example:
rails generate phlex:view Articles::Index

This will create:
app/views/articles/index_view.rb
app/views/articles/index.rb
5 changes: 2 additions & 3 deletions lib/generators/phlex/view/templates/view.rb.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true

<% module_namespacing do -%>
class <%= class_name %>View < ApplicationView
class Views::<%= "#{namespace}::" if namespaced? %><%= class_name %> < Views::Base
def view_template
h1 { "<%= class_name %>" }
p { "Find me in <%= @path %>" }
end
end<% end %>
end
2 changes: 1 addition & 1 deletion lib/generators/phlex/view/view_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ViewGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)

def create_view
@path = File.join("app/views", class_path, "#{file_name}_view.rb")
@path = File.join("app/views", class_path, "#{file_name}.rb")
template "view.rb.erb", @path
end
end
Expand Down