Browse files

Fix finder

  • Loading branch information...
1 parent e6d0d93 commit 12893cb032d7e19dcaa695d48a6772265fabac26 Bantik committed Mar 12, 2013
Showing with 9 additions and 5 deletions.
  1. +9 −5 lib/faceted/has_object.rb
View
14 lib/faceted/has_object.rb
@@ -51,11 +51,11 @@ def reinitialize_with_object(obj)
end
def save
- return false unless schema_fields
- schema_fields.each{ |k| object.send("#{k}=", self.send(k)) if self.send(:settable_field?, k) }
+ return false unless schema_fields.present?
+ schema_fields.each{ |k| self.send(:object).send("#{k}=", self.send(k)) if self.send(:settable_field?, k) }
self.success = object.save
self.errors = object.errors && object.errors.full_messages
- self.reinitialize_with_object(object)
+ self.reinitialize_with_object(object) if self.success
self.success
end
@@ -76,12 +76,16 @@ def initialize_with_object
def object
return unless self.class.klass
- @object = self.class.klass.where(find_by => self.send(find_by)).first || self.class.klass.new
+ if self.send(find_by).present?
+ @object ||= self.class.klass.where(find_by => self.send(find_by)).first
+ else
+ @object ||= self.class.klass.new
+ end
end
def object=(obj)
@object = obj
- self.id = obj.id
+ self.id = obj.id if obj.id.present?
end
def settable_field?(field_name)

0 comments on commit 12893cb

Please sign in to comment.