Sha256: 1e00808ffb03b11f558a2a99f87221eb12c5a7853ee3c5ebc6a110ad24f03296

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module IAmICan
  module Reflection
    extend ActiveSupport::Concern

    class_methods do
      def _reflect_of(key)
        _name = i_am_i_can&.send("#{key}_class")
        reflections.each do |name, reflection|
          return name if reflection.class_name == _name
        end; nil
      end

      %w[ subjects roles role_groups permissions ].each do |k|
        # User.__roles => 'stored_roles'
        define_method "__#{k}" do
          instance_variable_get(:"@__#{k}") or
              instance_variable_set(:"@__#{k}", _reflect_of(k.singularize))
        end

        # User.all._roles == User.all.stored_roles
        define_method "_#{k}" do
          send(send("__#{k}")) rescue (raise NoMethodError)
        end
      end
    end

    included do
      # user._roles => Association CollectionProxy, same as: `user.stored_roles`
      %w[ subjects roles role_groups permissions ].each do |k|
        delegate "__#{k}", to: self

        define_method "_#{k}" do
          send(send("__#{k}")) rescue (raise NoMethodError)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i_am_i_can-4.4.1 lib/i_am_i_can/support/reflection.rb
i_am_i_can-4.4.0 lib/i_am_i_can/support/reflection.rb