Sha256: c09e4d91f92546b47e869b3cbb3513bce4808f8aa092d8c46fcea4eca879e0c6

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# Include this module in your Phlex views to get access to the controller's instance variables. It
# provides an explicit interface for accessing controller instance variables from the view. Simply
# call `controller_attribute` with the name of any controller instance variable you want to access
# in your view.
#
# @example
#   class Views::Users::Index < Views::Base
#     controller_attribute :user_name
#
#     def template
#       h1 { @user_name }
#     end
#   end
#
# Options
#   - `attr_reader:` - If set to `true`, an `attr_reader` will be defined for the given attributes.
#   - `alias:` - If set, the given attribute will be aliased to the given alias value.
#
# NOTE: Phlexible::Rails::ActionController::ImplicitRender is required for this to work.
#
module Phlexible
  module Rails
    module ControllerAttributes
      extend ActiveSupport::Concern

      class UndefinedVariable < NameError
        def initialize(name)
          @variable_name = name
          super "Attempted to expose controller attribute `#{@variable_name}`, but instance " \
                'variable is not defined in the controller.'
        end
      end

      included do
        class_attribute :__controller_attributes__, instance_predicate: false, default: Set.new
      end

      class_methods do
        def controller_attribute(*names, **kwargs)
          self.__controller_attributes__ += names

          return if kwargs.empty?

          names.each do |name|
            attr_reader name if kwargs[:attr_reader]

            if kwargs[:alias]
              if kwargs[:attr_reader]
                alias_method kwargs[:alias], name
              else
                define_method(kwargs[:alias]) { instance_variable_get :"@#{name}" }
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phlexible-0.7.0 lib/phlexible/rails/controller_attributes.rb
phlexible-0.6.2 lib/phlexible/rails/controller_attributes.rb
phlexible-0.6.1 lib/phlexible/rails/controller_attributes.rb
phlexible-0.6.0 lib/phlexible/rails/controller_attributes.rb
phlexible-0.5.0 lib/phlexible/rails/controller_attributes.rb