-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This moves the install generator and scaffold generator under a superglue namespace. With the install generator, now we have `rails g superglue:install`, making use of rails generators also gives us the ability to add options to the install. With the scaffold generator, now we have `rails g superglue:scaffold`. This replaces the previous clunky scaffold that had to disable templates.
- Loading branch information
Showing
30 changed files
with
185 additions
and
172 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
superglue_rails/lib/generators/rails/scaffold_controller_generator.rb
This file was deleted.
Oops, something went wrong.
68 changes: 0 additions & 68 deletions
68
superglue_rails/lib/generators/rails/templates/controller.rb.tt
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
superglue_rails/lib/generators/superglue/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "rails/generators/named_base" | ||
require "rails/generators/resource_helpers" | ||
|
||
module Superglue | ||
module Generators | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def create_files | ||
say "Copying application.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/application.js", "#{app_js_path}/application.js" | ||
|
||
say "Copying page_to_page_mapping.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/page_to_page_mapping.js", "#{app_js_path}/page_to_page_mapping.js" | ||
|
||
say "Copying flash.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/flash.js", "#{app_js_path}/slices/flash.js" | ||
|
||
say "Copying pages.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/pages.js", "#{app_js_path}/slices/pages.js" | ||
|
||
say "Copying store.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/store.js", "#{app_js_path}/store.js" | ||
|
||
say "Copying application_visit.js file to #{app_js_path}" | ||
copy_file "#{__dir__}/templates/js/application_visit.js", "#{app_js_path}/application_visit.js" | ||
|
||
say "Copying Superglue initializer" | ||
copy_file "#{__dir__}/templates/initializer.rb", "config/initializers/superglue.rb" | ||
|
||
say "Copying application.json.props" | ||
copy_file "#{__dir__}/templates/application.json.props", "app/views/layouts/application.json.props" | ||
|
||
say "Adding required member methods to ApplicationRecord" | ||
add_member_methods | ||
|
||
say "Installing FormProps" | ||
run "bundle add form_props" | ||
|
||
say "Installing Superglue and friends" | ||
run "yarn add history react react-dom @reduxjs/toolkit react-redux @thoughtbot/superglue --save" | ||
|
||
say "Superglue is Installed! 🎉", :green | ||
end | ||
|
||
private | ||
|
||
def add_member_methods | ||
inject_into_file "app/models/application_record.rb", after: "class ApplicationRecord < ActiveRecord::Base\n" do | ||
<<-RUBY | ||
def self.member_at(index) | ||
offset(index).limit(1).first | ||
end | ||
def self.member_by(attr, value) | ||
find_by(Hash[attr, value]) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
def app_js_path | ||
"app/javascript/" | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
superglue_rails/lib/generators/superglue/scaffold/scaffold_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails/generators/rails/resource/resource_generator" | ||
|
||
module Superglue | ||
module Generators | ||
class ScaffoldGenerator < Rails::Generators::ResourceGenerator # :nodoc: | ||
remove_hook_for :resource_controller | ||
remove_class_option :actions | ||
|
||
class_option :resource_route, type: :boolean | ||
|
||
hook_for :scaffold_controller, required: true | ||
end | ||
end | ||
end |
61 changes: 61 additions & 0 deletions
61
...rglue_rails/lib/generators/superglue/scaffold_controller/scaffold_controller_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
# This file was copied over from Rails land and slightly modified to account | ||
# for Superglue templates | ||
|
||
require "rails/generators/resource_helpers" | ||
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator" | ||
|
||
module Superglue | ||
module Generators | ||
class ScaffoldControllerGenerator < Rails::Generators::NamedBase # :nodoc: | ||
include Rails::Generators::ResourceHelpers | ||
|
||
# Superglue uses the out-of-the-box controller generated by Rails. | ||
source_root Rails::Generators::ScaffoldControllerGenerator.source_root | ||
|
||
check_class_collision suffix: "Controller" | ||
|
||
class_option :helper, type: :boolean | ||
class_option :orm, banner: "NAME", type: :string, required: true, | ||
desc: "ORM to generate the controller for" | ||
|
||
class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb." | ||
|
||
argument :attributes, type: :array, default: [], banner: "field:type field:type" | ||
|
||
def create_controller_files | ||
template "controller.rb", File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb") | ||
end | ||
|
||
# Replaces template_engine (and its default erb), with view_collection | ||
# defaulting to superglue:view_collection | ||
hook_for :view_collection, required: true, default: "view_collection" | ||
|
||
hook_for :resource_route, in: :rails, required: true do |route| | ||
invoke route unless options.skip_routes? | ||
end | ||
|
||
hook_for :test_framework, in: :rails, as: :scaffold | ||
|
||
# Invoke the helper using the controller name (pluralized) | ||
hook_for :helper, in: :rails, as: :scaffold do |invoked| | ||
invoke invoked, [controller_name] | ||
end | ||
|
||
private | ||
|
||
def permitted_params | ||
attachments, others = attributes_names.partition { |name| attachments?(name) } | ||
params = others.map { |name| ":#{name}" } | ||
params += attachments.map { |name| "#{name}: []" } | ||
params.join(", ") | ||
end | ||
|
||
def attachments?(name) | ||
attribute = attributes.find { |attr| attr.name == name } | ||
attribute&.attachments? | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.