Sha256: 65eb5e1a6f81648c65aab8d36182b26f4d7a3e3a11200b37ccb6f0ae64577947

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

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|
        define_method "_#{k}" do
          send(self.class.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.2.0 lib/i_am_i_can/support/reflection.rb
i_am_i_can-4.1.0 lib/i_am_i_can/support/reflection.rb