Skip to content

Commit

Permalink
Removed UPDATE … SET type query executions, as it appears ActiveRec…
Browse files Browse the repository at this point in the history
…ord handles the type fine on it's own. (since Rails 3.1.0?)
  • Loading branch information
DouweM committed Sep 4, 2011
1 parent 6b46253 commit 4a7cc8c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
15 changes: 6 additions & 9 deletions lib/citier/child_instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def save(validate = true)
attributes_for_current = self.attributes.reject { |key,value| self.class.superclass.column_names.include?(key) }
changed_attributes_for_current = self.changed_attributes.reject { |key,value| self.class.superclass.column_names.include?(key) }

citier_debug("Attributes for #{self.class.to_s}: #{attributes_for_current.inspect.to_s}")
citier_debug("Attributes for #{self.class.superclass.to_s}: #{attributes_for_parent.inspect}")
citier_debug("Changed attributes for #{self.class.superclass.to_s}: #{changed_attributes_for_parent.keys.inspect}")
citier_debug("Attributes for #{self.class.to_s}: #{attributes_for_current.inspect}")
citier_debug("Changed attributes for #{self.class.to_s}: #{changed_attributes_for_current.keys.inspect}")

########
#
Expand All @@ -29,7 +32,7 @@ def save(validate = true)
parent = self.class.superclass.new

parent.force_attributes(attributes_for_parent, :merge => true)
changed_attributes_for_parent["id"] = 0 # We need to change at least something so timestamps are updated.
changed_attributes_for_parent["id"] = 0 # We need to change at least something to force a timestamps update.
parent.force_changed_attributes(changed_attributes_for_parent)

parent.id = self.id if id
Expand Down Expand Up @@ -79,19 +82,13 @@ def save(validate = true)
if(!current_saved)
citier_debug("Class (#{self.class.superclass.to_s}) could not be saved")
citier_debug("Errors = #{current.errors.to_s}")

end
end

# Update root class with this 'type'
if parent_saved && current_saved
sql = "UPDATE #{self.class.base_class.table_name} SET #{self.class.inheritance_column} = '#{self.class.to_s}' WHERE id = #{self.id}"
citier_debug("SQL : #{sql}")
self.connection.execute(sql)
self.force_changed_attributes({})
end

self.force_changed_attributes({})

return parent_saved && current_saved
end

Expand Down
2 changes: 0 additions & 2 deletions lib/citier/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def acts_as_citier(options = {})
else
# Root class

after_save :updatetype

citier_debug("Root Class")

set_table_name "#{table_name}"
Expand Down
10 changes: 0 additions & 10 deletions lib/citier/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ def force_changed_attributes(new_changed_attributes, options = {})
@changed_attributes = new_changed_attributes
end
end

def updatetype
# Keeps our types intact when we've retrieved a record through Root.first etc. and save it.
# Without this it would revert back to the root class
type = self.type || self.class.to_s

sql = "UPDATE #{self.class.base_class.table_name} SET #{self.class.inheritance_column} = '#{type}' WHERE id = #{self.id}"
self.connection.execute(sql)
citier_debug("#{sql}")
end

# USAGE validates :attribute, :citier_uniqueness => true
# Needed because validates :attribute, :uniqueness => true Won't work because it tries to call child_class.attribute on parents table
Expand Down

0 comments on commit 4a7cc8c

Please sign in to comment.