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/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 new file mode 100644 index 0000000..4b07f56 --- /dev/null +++ b/lib/phlexible/rails/action_controller/render_action.rb @@ -0,0 +1,22 @@ +# 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) + render view.new(**options.except(:action, :prefixes, :template, :layout)) + else + super + end + end + end + end + end +end \ No newline at end of file