Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@masatomo
Created December 6, 2010 18:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save masatomo/730677 to your computer and use it in GitHub Desktop.
Save masatomo/730677 to your computer and use it in GitHub Desktop.
adds a feature to set sequence number to Mongoid::Document
# 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
@ShogunPanda
Copy link

I've just found a quicker way to achieve the same result (but your code was fundamental to gave me inspiration :) ).
You can see it there: https://gist.github.com/1086265
Best regards!

@goncalossilva
Copy link

Just packaged something similar to this into a gem: https://github.com/goncalossilva/mongoid-sequence

@masatomo
Copy link
Author

That's great. thanks :)

@ShogunPanda
Copy link

Well done! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment