Sha256: 0674d666399fd83eee2eb5b3cd8623de3b1911964530f86a754bebb702efd849

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

# Adds support for default and action_missing rendering of Phlex views. So instead of this:
#
#   class UsersController
#     def index
#       render Views::Users::Index.new
#     end
#   end
#
# You can do this:
#
#   class UsersController
#     include Phlexible::Rails::ActionController::ImplicitRender
#   end
#
module Phlexible
  module Rails
    module ActionController
      module ImplicitRender
        def default_render
          render_plex_view({ action: action_name }) || super
        end

        def assign_phlex_accessors(pview)
          pview.tap do |view|
            view.__controller_attributes__.each do |attr|
              view.instance_variable_set :"@#{attr}", view_assigns[attr.to_s]
            end
          end
        end

        def method_for_action(action_name)
          super || ('default_phlex_render' if phlex_view(action_name))
        end

        def default_phlex_render
          render assign_phlex_accessors(phlex_view(action_name).new)
        end

        # @param options [Hash] At a minimum this may contain an `:action` key, which will be used
        #   as the name of the view to render. If no `:action` key is provided, the current
        #   `action_name` is used.
        def render_plex_view(options)
          options[:action] ||= action_name

          return unless (view = phlex_view(options[:action]))

          render assign_phlex_accessors(view.new)
        end

        def phlex_view(action_name = @_action_name)
          "views/#{controller_path}/#{action_name}".classify.safe_constantize
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlexible-0.4.0 lib/phlexible/rails/action_controller/implicit_render.rb