Sha256: ae1a5b4318c1813a7a3b0cd0d6fc793a08932d08061c58bf77ae44441597dee0

Contents?: true

Size: 1.64 KB

Versions: 13

Compression:

Stored size: 1.64 KB

Contents

require 'mongoid'

module Mongoid
  module Document
    module ClassMethods
      include OrmAdapter::ToAdapter
    end

    class OrmAdapter < ::OrmAdapter::Base
      # get a list of column names for a given class
      def column_names
        klass.fields.keys
      end

      # @see OrmAdapter::Base#get!
      def get!(id)
        klass.find(wrap_key(id))
      end

      # @see OrmAdapter::Base#get
      def get(id)
        klass.where(:_id => wrap_key(id)).first
      end

      # @see OrmAdapter::Base#find_first
      def find_first(options = {})
        conditions, order = extract_conditions!(options)
        klass.limit(1).where(conditions_to_fields(conditions)).order_by(order).first
      end

      # @see OrmAdapter::Base#find_all
      def find_all(options = {})
        conditions, order, limit, offset = extract_conditions!(options)
        klass.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
      end

      # @see OrmAdapter::Base#create!
      def create!(attributes = {})
        klass.create!(attributes)
      end

      # @see OrmAdapter::Base#destroy
      def destroy(object)
        object.destroy if valid_object?(object)
      end

    protected

      # converts and documents to ids
      def conditions_to_fields(conditions)
        conditions.inject({}) do |fields, (key, value)|
          if value.is_a?(Mongoid::Document) && klass.fields.keys.include?("#{key}_id")
            fields.merge("#{key}_id" => value.id)
          elsif key.to_s == 'id'
            fields.merge('_id' => value)
          else
            fields.merge(key => value)
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 12 versions & 9 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/mongoid.rb
orm_adapter-0.5.0 lib/orm_adapter/adapters/mongoid.rb
orm_adapter-0.4.1 lib/orm_adapter/adapters/mongoid.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.4.0/lib/orm_adapter/adapters/mongoid.rb
orm_adapter_rails4_fix-0.4.1 lib/orm_adapter/adapters/mongoid.rb
orm_adapter-0.4.0 lib/orm_adapter/adapters/mongoid.rb