Sha256: 0eb5e74058701082070f3b0408b54e6a31aea8dfd50480d5e0b77871f936588a

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 Bytes

Contents

# frozen_string_literal: true

module Monarchy
  module ActsAsRole
    extend ActiveSupport::Concern

    module ClassMethods
      def acts_as_role
        self.table_name = 'monarchy_roles'

        has_many :members_roles, dependent: :destroy, class_name: 'Monarchy::MembersRole'
        has_many :members, through: :members_roles, class_name: "::#{Monarchy.member_class}"

        belongs_to :inherited_role, class_name: "::#{Monarchy.role_class}"

        after_create :default_inherited_role

        validates :name, presence: true
        validates :level, presence: true

        include Monarchy::ActsAsRole::InstanceMethods
      end
    end

    module InstanceMethods
      private

      def default_inherited_role
        update(inherited_role_id: id) unless inherited_role
      end
    end
  end
end

ActiveRecord::Base.send :include, Monarchy::ActsAsRole

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
monarchy-2.6.0 lib/monarchy/acts_as_role.rb