Sha256: 00db33ebfce39e4d11950eaa70cc0f1f9a2cab42fc04eb9cfb24f1d30c6f0b6e

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module IAmICan
  module Association_ClassMethods
    def has_many_temporary_roles(name: nil)
      define_method :temporary_roles do
        @temporary_roles ||= [ ]
      end
      #
      alias_method name, :temporary_roles if name

      define_method :valid_temporary_roles do
        i_am_i_can.role_model.where(id: temporary_roles.map(&:id))
      end

      define_method :temporary_role_names do
        temporary_roles.map(&:name).map(&:to_sym)
      end
      #
      alias_method "#{name.to_s.singularize}_names", :temporary_role_names if name

      define_method :_temporary_roles_add do |instances = nil, name: nil|
        roles = instances.present? ? instances : i_am_i_can.role_model.where(name: name - temporary_role_names)
        temporary_roles.concat(roles)
        roles.map(&:name).map(&:to_sym)
      end

      define_method :_temporary_roles_rmv do |instances = nil, name: nil|
        if instances.present?
          self.temporary_roles -= instances
          instances.map(&:name).map(&:to_sym)
        else
          (name & temporary_role_names).each { |n| self.temporary_roles.reject! { |i| i.name.to_sym == n } }
        end
      end

      define_method :_temporary_roles_exec do |action = :assign, instances = nil, name: nil|
        send('_temporary_roles_' + (action == :assign ? 'add' : 'rmv'), instances, name: name)
      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/association_class_methods.rb
i_am_i_can-4.4.0 lib/i_am_i_can/support/association_class_methods.rb