Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderAction #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions lib/phlexible/rails/action_controller/base.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 6 additions & 11 deletions lib/phlexible/rails/action_controller/implicit_render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/phlexible/rails/action_controller/render_action.rb
Original file line number Diff line number Diff line change
@@ -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
Loading