Sha256: 7b92696cea60f801a718de50de8c06104224b82cfa79bf486d62ed02211690e6

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module CanCan
  module ModelAdapters
    class MongoidAdapter < AbstractAdapter
      def self.for_class?(model_class)
        model_class <= Mongoid::Document
      end

      def self.override_conditions_hash_matching?(subject, conditions)
        conditions.any? { |k,v| !k.kind_of?(Symbol) }
      end

      def self.matches_conditions_hash?(subject, conditions)
        # To avoid hitting the db, retrieve the raw Mongo selector from
        # the Mongoid Criteria and use Mongoid::Matchers#matches?
        subject.matches?( subject.class.where(conditions).selector )
      end

      def database_records
        @model_class.where(conditions)
      end

      def conditions
        if @rules.size == 0
          false_query
        else
          @rules.first.conditions
        end
      end

      def false_query
        # this query is sure to return no results
        {:_id => {'$exists' => false, '$type' => 7}}  # type 7 is an ObjectID (default for _id)
      end
    end
  end
end

# simplest way to add `accessible_by` to all Mongoid Documents
module Mongoid::Document::ClassMethods
  include CanCan::ModelAdditions::ClassMethods
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cancan-1.5.0 lib/cancan/model_adapters/mongoid_adapter.rb
cancan-1.5.0.beta1 lib/cancan/model_adapters/mongoid_adapter.rb