Sha256: b2867583ba094b087e2bf80bec0c5836be3a0c3dcafe593c9b1240e34be138bb

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 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.
#
module Phlexible
  module Rails
    module ControllerAttributes
      extend ActiveSupport::Concern

      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]
            alias_method kwargs[:alias], name if kwargs[:alias]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phlexible-0.4.1 lib/phlexible/rails/controller_attributes.rb
phlexible-0.4.0 lib/phlexible/rails/controller_attributes.rb