Sha256: 5ec2ed137f0344441690d6b5a46b34db38c477d40b9564c3f4c8c3a58e0f2def

Contents?: true

Size: 1.22 KB

Versions: 29

Compression:

Stored size: 1.22 KB

Contents

module Symphonia
  class Role < ApplicationRecord
    self.table_name = 'roles'

    include ModelAttributes

    register_query do
      add_attribute :name, :link
    end

    validates :name, presence: true

    serialize :permissions, Array

    scope :active, -> {}
    scope :sorted, -> { order(:name) }

    # after_save :expire_cache

    def allowed_to?(permission)
      Rails.cache.fetch([self, permission]) do
        !allowed_permissions.detect { |i| permission.to_sym == i.name }.nil?
      end
    end

    def authorize?(controller, action)
      Rails.cache.fetch([self, controller, action]) do
        !allowed_permissions.detect { |p| p.allowed?(controller, action) }.nil?
      end
    end

    def allowed_permissions
      return @allowed_permissions if @allowed_permissions

      @allowed_permissions = permissions.collect do |i|
        i.is_a?(Symphonia::Permissions::Permission) ? i : Symphonia::Permissions.get(i.to_sym)
      end.compact

      @allowed_permissions
    end

    def permission_names
      permissions #.map(&:name)
    end

    # private
    #
    # def expire_cache
    #   Rails.cache.delete_matched("role_#{self.id}_*")
    #   Rails.cache.delete_matched('user_allowed_to*')
    # end

  end

end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
symphonia-4.2.0 app/models/symphonia/role.rb
symphonia-4.1.3 app/models/symphonia/role.rb
symphonia-4.1.2 app/models/symphonia/role.rb
symphonia-4.1.1 app/models/symphonia/role.rb
symphonia-4.1.0 app/models/symphonia/role.rb
symphonia-4.0.1 app/models/symphonia/role.rb
symphonia-4.0.0 app/models/symphonia/role.rb
symphonia-3.4.0 app/models/symphonia/role.rb
symphonia-3.3.4 app/models/symphonia/role.rb
symphonia-3.3.3 app/models/symphonia/role.rb
symphonia-3.3.2 app/models/symphonia/role.rb
symphonia-3.3.0 app/models/symphonia/role.rb
symphonia-3.2.4 app/models/symphonia/role.rb
symphonia-3.2.3 app/models/symphonia/role.rb
symphonia-3.2.2 app/models/symphonia/role.rb
symphonia-3.2.1 app/models/symphonia/role.rb
symphonia-3.1.5 app/models/symphonia/role.rb
symphonia-3.1.4 app/models/symphonia/role.rb
symphonia-3.1.3 app/models/symphonia/role.rb
symphonia-3.1.2 app/models/symphonia/role.rb