Sha256: 1ed16df8714e9004b5c2ef1db6b782e5b235e9b8e49d6fdba18694de882f5faa

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 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!(permission_params(whom, 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!(wildcard_permission_params(whom, what))
          end
          self.permissions_as_permissor.reload
        end

        def permission_params(whom, what)
          ActionController::Parameters.new({:relationship_type => whom, :permissible_type => what.class.name, :permissible_id => what.id}).permit!
        end

        def wildcard_permission_params(whom, what)
          ActionController::Parameters.new({:relationship_type => whom, :permissible_type => what}).permit!
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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