Skip to content

Commit

Permalink
Add a new mixin called render_action that allows controllers to rende…
Browse files Browse the repository at this point in the history
…r :action or render "path/to/action"
  • Loading branch information
pedantic-git committed Jul 11, 2024
1 parent 1a004f0 commit 91e876e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
1 change: 1 addition & 0 deletions lib/phlexible/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions lib/phlexible/rails/action_controller/render_action.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 91e876e

Please sign in to comment.