Skip to content

Commit

Permalink
Check that ActiveRecord exists before overriding methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryckbost committed Oct 17, 2011
1 parent eebddcb commit b71cc2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
12 changes: 7 additions & 5 deletions lib/delayed/psych_ext.rb
@@ -1,8 +1,10 @@
class ActiveRecord::Base
# serialize to YAML
def encode_with(coder)
coder["attributes"] = @attributes
coder.tag = ['!ruby/ActiveRecord', self.class.name].join(':')
if defined?(ActiveRecord)
class ActiveRecord::Base
# serialize to YAML
def encode_with(coder)
coder["attributes"] = @attributes
coder.tag = ['!ruby/ActiveRecord', self.class.name].join(':')
end
end
end

Expand Down
28 changes: 15 additions & 13 deletions lib/delayed/serialization/active_record.rb
@@ -1,17 +1,19 @@
class ActiveRecord::Base
yaml_as "tag:ruby.yaml.org,2002:ActiveRecord"
if defined?(ActiveRecord)
class ActiveRecord::Base
yaml_as "tag:ruby.yaml.org,2002:ActiveRecord"

def self.yaml_new(klass, tag, val)
if ActiveRecord::VERSION::MAJOR == 3
klass.unscoped.find(val['attributes'][klass.primary_key])
else # Rails 2
klass.with_exclusive_scope { klass.find(val['attributes'][klass.primary_key]) }
def self.yaml_new(klass, tag, val)
if ActiveRecord::VERSION::MAJOR == 3
klass.unscoped.find(val['attributes'][klass.primary_key])
else # Rails 2
klass.with_exclusive_scope { klass.find(val['attributes'][klass.primary_key]) }
end
rescue ActiveRecord::RecordNotFound
raise Delayed::DeserializationError
end
rescue ActiveRecord::RecordNotFound
raise Delayed::DeserializationError
end

def to_yaml_properties
['@attributes']
def to_yaml_properties
['@attributes']
end
end
end
end

0 comments on commit b71cc2a

Please sign in to comment.