Sha256: 4db066df1806bd06bf26c9438b9c88c6b9e167f1aad07c5377dcdc44257aaa24

Contents?: true

Size: 1.27 KB

Versions: 24

Compression:

Stored size: 1.27 KB

Contents

require "active_support/concern"

module RocketJob
  module Plugins
    # Base class for storing models in MongoDB
    module Document
      extend ActiveSupport::Concern
      include ::Mongoid::Document

      included do
        store_in client: "rocketjob"
      end

      # rubocop:disable Style/RedundantSort
      module ClassMethods
        # Mongoid does not apply ordering, add sort
        def first
          all.sort("_id" => 1).first
        end

        # Mongoid does not apply ordering, add sort
        def last
          all.sort("_id" => -1).first
        end
      end
      # rubocop:enable Style/RedundantSort

      private

      # Apply changes to this document returning the updated document from the database.
      # Allows other changes to be made on the server that will be loaded.
      def find_and_update(attrs)
        doc = collection.find(_id: id).find_one_and_update({"$set" => attrs}, return_document: :after)
        raise(::Mongoid::Errors::DocumentNotFound.new(self.class, id)) unless doc

        # Clear out keys that are not returned during the reload from MongoDB
        (fields.keys + embedded_relations.keys - doc.keys).each { |key| send("#{key}=", nil) }
        @attributes = doc
        apply_defaults
        self
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/plugins/document.rb
rocketjob-6.3.0 lib/rocket_job/plugins/document.rb
rocketjob-6.2.0 lib/rocket_job/plugins/document.rb
rocketjob-6.1.1 lib/rocket_job/plugins/document.rb
rocketjob-6.1.0 lib/rocket_job/plugins/document.rb
rocketjob-6.0.3 lib/rocket_job/plugins/document.rb
rocketjob-6.0.2 lib/rocket_job/plugins/document.rb
rocketjob-6.0.1 lib/rocket_job/plugins/document.rb
rocketjob-6.0.0 lib/rocket_job/plugins/document.rb
rocketjob-6.0.0.rc3 lib/rocket_job/plugins/document.rb
rocketjob-6.0.0.rc2 lib/rocket_job/plugins/document.rb
rocketjob-6.0.0.rc1 lib/rocket_job/plugins/document.rb
rocketjob-5.4.1 lib/rocket_job/plugins/document.rb
rocketjob-5.4.0 lib/rocket_job/plugins/document.rb
rocketjob-5.4.0.beta2 lib/rocket_job/plugins/document.rb
rocketjob-5.4.0.beta1 lib/rocket_job/plugins/document.rb
rocketjob-5.3.3 lib/rocket_job/plugins/document.rb
rocketjob-5.3.2 lib/rocket_job/plugins/document.rb
rocketjob-5.3.1 lib/rocket_job/plugins/document.rb
rocketjob-5.3.0 lib/rocket_job/plugins/document.rb