From 6452319399a908011aceb423279de50d2b39ba9a Mon Sep 17 00:00:00 2001 From: Quinn Daley Date: Thu, 11 Jul 2024 14:54:49 +0100 Subject: [PATCH] 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