Sha256: 0d5e1249e9b6fce28081a6af341003ad7e3123670892a7074da570592e4edda3

Contents?: true

Size: 973 Bytes

Versions: 3

Compression:

Stored size: 973 Bytes

Contents

class Policy
  module DefaultPolicy
    def index
      false
    end

    def new?
      false
    end

    def create?
      new?
    end

    def show?
      false
    end

    def edit?
      false
    end

    def update?
      edit?
    end

    def destroy?
      edit?
    end
  end

  include DefaultPolicy

  attr_reader :user, :record

  def initialize(user, record = nil)
    @user = user
    @record = record
  end

  class << self
    def for(user, object)
      if object.is_a?(Class)
        "#{object}Policy".constantize.collection(user)
      else
        "#{object.class}Policy".constantize.member(user, object)
      end
    end

    def collection(user)
      policy = new(user)
      const_defined?(:Collection) &&
        policy.extend(const_get(:Collection))
      policy
    end

    def member(user, record)
      policy = new(user, record)
      const_defined?(:Member) &&
        policy.extend(const_get(:Member))
      policy
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/policies/policy.rb
pages_core-3.5.1 app/policies/policy.rb
pages_core-3.4.2 app/policies/policy.rb