From 91e876ec4265010e8d27e4d3b3060ec3a7b1c92f Mon Sep 17 00:00:00 2001 From: Quinn Daley Date: Thu, 11 Jul 2024 14:39:41 +0100 Subject: [PATCH 1/2] Add a new mixin called render_action that allows controllers to render :action or render "path/to/action" --- README.md | 27 +++++++++++++++++ lib/phlexible/rails.rb | 1 + .../rails/action_controller/render_action.rb | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/phlexible/rails/action_controller/render_action.rb diff --git a/README.md b/README.md index 3bc7f37..cee7e35 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,33 @@ class UsersController end ``` +#### `ActionController::RenderAction` + +Adds support for calling views by their action name. So instead of this: + +```ruby +class UsersController + def other_index + render Views::Users::Index.new + end +end +``` + +You can do this: + +```ruby +class UsersController + include Phlexible::Rails::ActionController::RenderAction + + def other_index + render :index + # or render "users/index" + end +end +``` + +Any other parameters passed will be passed as named parameters to the view's constructor. + #### `ControllerVariables` > Available in **>= 1.0.0** diff --git a/lib/phlexible/rails.rb b/lib/phlexible/rails.rb index 973cc50..0a56e93 100644 --- a/lib/phlexible/rails.rb +++ b/lib/phlexible/rails.rb @@ -15,6 +15,7 @@ module Rails module ActionController autoload :ImplicitRender, 'phlexible/rails/action_controller/implicit_render' autoload :MetaTags, 'phlexible/rails/action_controller/meta_tags' + autoload :RenderAction, 'phlexible/rails/action_controller/render_action' end end end diff --git a/lib/phlexible/rails/action_controller/render_action.rb b/lib/phlexible/rails/action_controller/render_action.rb new file mode 100644 index 0000000..0301981 --- /dev/null +++ b/lib/phlexible/rails/action_controller/render_action.rb @@ -0,0 +1,30 @@ +module Phlexible + module Rails + module ActionController + module RenderAction + + def render_to_body(options = {}) + if view = phlex_view(options) + render view.new(**options.except(:action, :prefixes, :template, :layout)) + else + super + end + end + + private + + def phlex_view(options) + phlex_view_path(options)&.camelize&.safe_constantize + end + + def phlex_view_path(options) + if options[:action] + "/#{controller_path}/#{options[:action]}_view" + elsif options[:template] + "/#{options[:template]}_view" + end + end + end + end + end +end \ No newline at end of file From 6452319399a908011aceb423279de50d2b39ba9a Mon Sep 17 00:00:00 2001 From: Quinn Daley Date: Thu, 11 Jul 2024 14:54:49 +0100 Subject: [PATCH 2/2] Refactor so both modules can be used together --- lib/phlexible/rails/action_controller/base.rb | 24 +++++++++++++++++++ .../action_controller/implicit_render.rb | 17 +++++-------- .../rails/action_controller/render_action.rb | 20 +++++----------- 3 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 lib/phlexible/rails/action_controller/base.rb diff --git a/lib/phlexible/rails/action_controller/base.rb b/lib/phlexible/rails/action_controller/base.rb new file mode 100644 index 0000000..4dcc94e --- /dev/null +++ b/lib/phlexible/rails/action_controller/base.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Phlexible + module Rails + module ActionController + module Base + + protected + + def phlex_view(options) + phlex_view_path(options)&.camelize&.safe_constantize + end + + def phlex_view_path(options) + if options[:action] + "/#{controller_path}/#{options[:action]}_view" + elsif options[:template] + "/#{options[:template]}_view" + end + end + end + end + end +end diff --git a/lib/phlexible/rails/action_controller/implicit_render.rb b/lib/phlexible/rails/action_controller/implicit_render.rb index 2ae0b73..6ed8686 100644 --- a/lib/phlexible/rails/action_controller/implicit_render.rb +++ b/lib/phlexible/rails/action_controller/implicit_render.rb @@ -18,10 +18,15 @@ # include Phlexible::Rails::ActionController::ImplicitRender # end # + +require_relative "base" + module Phlexible module Rails module ActionController module ImplicitRender + include Base + def default_render render_plex_view({ action: action_name }) || super end @@ -40,20 +45,10 @@ def default_phlex_render def render_plex_view(options) options[:action] ||= action_name - return unless (view = phlex_view(options[:action])) + return unless (view = phlex_view({action: options[:action]})) render view.new, options end - - private - - def phlex_view(action_name = @_action_name) - phlex_view_path(action_name).camelize.safe_constantize - end - - def phlex_view_path(action_name) - "#{controller_path}/#{action_name}_view" - end end end end diff --git a/lib/phlexible/rails/action_controller/render_action.rb b/lib/phlexible/rails/action_controller/render_action.rb index 0301981..4b07f56 100644 --- a/lib/phlexible/rails/action_controller/render_action.rb +++ b/lib/phlexible/rails/action_controller/render_action.rb @@ -1,7 +1,13 @@ +# frozen_string_literal: true + + +require_relative "base" + module Phlexible module Rails module ActionController module RenderAction + include Base def render_to_body(options = {}) if view = phlex_view(options) @@ -10,20 +16,6 @@ def render_to_body(options = {}) super end end - - private - - def phlex_view(options) - phlex_view_path(options)&.camelize&.safe_constantize - end - - def phlex_view_path(options) - if options[:action] - "/#{controller_path}/#{options[:action]}_view" - elsif options[:template] - "/#{options[:template]}_view" - end - end end end end