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