Created
December 6, 2010 18:19
-
-
Save masatomo/730677 to your computer and use it in GitHub Desktop.
adds a feature to set sequence number to Mongoid::Document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
module Mongoid #:nodoc: | |
# Include this module to add automatic sequence feature | |
# usage: | |
# Class KlassName | |
# include Mongoid::Document | |
# include Mongoid::Sequence | |
# ... | |
# field :number, :type=>Integer | |
# sequence :number | |
# ... | |
module Sequence | |
extend ActiveSupport::Concern | |
class Holder | |
include Document | |
field :seq, :type => Integer | |
end | |
included do | |
set_callback :validate, :before, :set_sequence | |
end | |
module ClassMethods | |
attr_accessor :sequence_fields | |
def sequence(field) | |
self.sequence_fields ||= [] | |
self.sequence_fields << field | |
end | |
end | |
def set_sequence | |
if self.class.sequence_fields.is_a?(Array) | |
self.class.sequence_fields.each do |field| | |
next if self[field] | |
# load schema info (TODO: find better way) | |
Holder.first unless Holder._collection | |
# TODO: should be Holder._collection.find_and_modify. | |
# But as it seems the currnet MongoId::Collection doesn't support find_and_modify, this logic calls Mongo::Collection directly for now | |
next_sequence = Holder._collection.master.collection.find_and_modify(:query => {"_id" => "#{self.class.name.underscore}_#{field}"}, | |
:update=> {"$inc"=> {"seq"=>1}}, | |
:new => true, | |
:upsert => true) | |
self[field] = next_sequence["seq"] | |
end | |
end | |
end | |
end | |
end |
That's great. thanks :)
Well done! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just packaged something similar to this into a gem: https://github.com/goncalossilva/mongoid-sequence