Skip to content

Commit

Permalink
Add support for Mongoid versions >= 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alup committed Aug 23, 2012
1 parent 566a3aa commit da62e52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/money-rails/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.init
end

if ::Mongoid::VERSION =~ /^3(.*)/
# Mongoid 3.x stuff here
require 'money-rails/mongoid/three'
end
end

Expand Down
40 changes: 40 additions & 0 deletions lib/money-rails/mongoid/three.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Money

# Converts an object of this instance into a database friendly value.
def mongoize
{
:cents => cents,
:currency_iso => currency.iso_code
}
end

class << self

# Get the object as it was stored in the database, and instantiate
# this custom class from it.
def demongoize(object)
return nil if object.nil?

::Money.new(object[:cents], object[:currency_iso])
end

# Takes any possible object and converts it to how it would be
# stored in the database.
def mongoize(object)
case object
when Money then object.mongoize
when Hash then ::Money.new(object[:cents], object[:currency]).mongoize
else object
end
end

# Converts the object that was supplied to a criteria and converts it
# into a database friendly form.
def evolve(object)
case object
when Money then object.mongoize
else object
end
end
end
end

0 comments on commit da62e52

Please sign in to comment.