Sha256: b74301cf595951910c533e6295b4458051adf6ab65ab08b246a7bd2565742eb8

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module PrivatePerson
  module Permissor
    def acts_as_permissor(params = {})
      if params[:of].nil?
        raise 'Called acts_as_permissor, but without an :of parameter.'
      end
      class_attribute :of
      self.of = params[:of]
      class_name = params[:class_name] || params[:of].to_s.classify
      class_name.constantize.acts_as_permitted
      class_eval do
        has_many :permissions_as_permissor, :as => :permissor, :class_name => 'Permission'
        has_many :permissibles, :through => :permissions, :as => :permissor

        def permit!(whom, what)
          existing = self.permissions_as_permissor.by_relationship_type(whom).by_permissible(what)

          if existing.empty?
            self.permissions_as_permissor.create!({:relationship_type => whom, :permissible => what})
          end
          self.permissions_as_permissor.reload
        end

        def wildcard_permit!(whom, what)
          existing = self.permissions_as_permissor.by_relationship_type(whom).where(:permissible_type, what)

          if existing.empty?
            self.permissions_as_permissor.create!({:relationship_type => whom, :permissible_type => what})
          end
          self.permissions_as_permissor.reload
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
private_person-0.2.9 lib/private_person/models/permissor.rb