Sha256: ce7a98c3c6063be264d6acdf9532c6061ea8b98992de6925e4821e29a0fee20f

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# Main class for all the **Faalis** Policy classes.
# It's totally a minimume Policy.
class Faalis::ApplicationPolicy
  attr_reader :user, :record

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

  def authorize?(action)
    return false if @user.nil?
    return true if @user.admin?

    # Check for ownership of the reocrd
    record_class = @record.class

    unless [Class, String, Symbol].include?(record_class)
      if @user.has_ownership?(@record)
        return false if !@user.owned? @record
      end
      @record = @record.class
    end

    user.can? action, @record.to_s
  end

  def method_missing(m, *args, &block_given)
    return authorize? m.to_s[0..-2] if m.to_s =~ /.*\?$/
    super
  end


  def scope
    Pundit.policy_scope!(@user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      if @user.has_ownership?(scope)
        scope.where(user: @user)
      else
        scope
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faalis-2.2.0.pre.rc1 app/policies/faalis/application_policy.rb
faalis-2.2.1 app/policies/faalis/application_policy.rb
faalis-2.2.0 app/policies/faalis/application_policy.rb