Skip to content

Commit

Permalink
Add support for DataMapper 0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Aug 13, 2009
1 parent c1cab6f commit d40b214
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
== master

* Add support for DataMapper 0.10.0
* Always interpet nil return values from actions as failed attempts
* Fix loopbacks not causing records to save in ORM integrations if no other fields were changed
* Fix events not failing with useful errors when an object's state is invalid
Expand Down
13 changes: 10 additions & 3 deletions lib/state_machine/integrations/data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,21 @@ def self.matches?(klass)

# Loads additional files specific to DataMapper
def self.extended(base) #:nodoc:
require 'dm-core/version' unless ::DataMapper.const_defined?('VERSION')
require 'state_machine/integrations/data_mapper/observer' if ::DataMapper.const_defined?('Observer')
end

# Forces the change in state to be recognized regardless of whether the
# state value actually changed
def write(object, attribute, value)
result = super
object.original_values[self.attribute] = "#{value}-ignored" if attribute == :state && owner_class.properties.has_property?(self.attribute)
if attribute == :state && owner_class.properties.detect {|property| property.name == self.attribute}
if ::DataMapper::VERSION =~ /^(0\.\d\.)/ # Match anything < 0.10
object.original_values[self.attribute] = "#{value}-ignored"
else
object.original_attributes[owner_class.properties[self.attribute]] = "#{value}-ignored"
end
end
result
end

Expand All @@ -265,7 +272,7 @@ def invalidate(object, attribute, message, values = [])

# Resets any errors previously added when invalidating the given object
def reset(object)
object.errors.clear if object.respond_to?(:errors)
object.errors.clear if supports_validations?
end

protected
Expand All @@ -276,7 +283,7 @@ def supports_validations?

# Skips defining reader/writer methods since this is done automatically
def define_state_accessor
owner_class.property(attribute, String) unless owner_class.properties.has_property?(attribute)
owner_class.property(attribute, String) unless owner_class.properties.detect {|property| property.name == attribute}

if supports_validations?
name = self.name
Expand Down
2 changes: 1 addition & 1 deletion test/unit/integrations/data_mapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def new_resource(auto_migrate = true, &block)
storage_names[:default] = 'foo'
def self.name; 'DataMapperTest::Foo'; end

property :id, Integer, :serial => true
property :id, DataMapper::Types::Serial
property :state, String

auto_migrate! if auto_migrate
Expand Down

0 comments on commit d40b214

Please sign in to comment.