Ticket #6542: more_injection_points.diff
| File more_injection_points.diff, 4.1 kB (added by ymendel, 2 years ago) |
|---|
-
base.rb
old new 444 444 attributes.collect { |attr| create(attr) } 445 445 else 446 446 object = new(attributes) 447 scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create)447 create_scope_to_object(object) 448 448 object.save 449 449 object 450 450 end 451 451 end 452 452 453 def create_scope_to_object(object) 454 scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create) 455 end 456 453 457 # Finds the record from the passed +id+, instantly saves it with the passed +attributes+ (if the validation permits it), 454 458 # and returns it. If the save fails under validations, the unsaved object is still returned. 455 459 # … … 1542 1546 # be made (since they can't be persisted). 1543 1547 def destroy 1544 1548 unless new_record? 1545 connection.delete <<-end_sql, "#{self.class.name} Destroy" 1546 DELETE FROM #{self.class.table_name} 1547 WHERE #{self.class.primary_key} = #{quoted_id} 1548 end_sql 1549 connection.delete(sql_for_destroy, "#{self.class.name} Destroy") 1549 1550 end 1550 1551 1551 1552 freeze 1552 1553 end 1553 1554 1555 def sql_for_destroy 1556 "DELETE FROM #{self.class.table_name}" + ' ' + sql_for_destroy_conditions 1557 end 1558 1559 def sql_for_destroy_conditions 1560 "WHERE #{self.class.primary_key} = #{quoted_id}" 1561 end 1562 1554 1563 # Returns a clone of the record that hasn't been assigned an id yet and 1555 1564 # is treated as a new record. Note that this is a "shallow" clone: 1556 1565 # it copies the object's attributes only, not its associations. … … 1780 1789 # Updates the associated record with values matching those of the instance attributes. 1781 1790 # Returns the number of affected rows. 1782 1791 def update 1783 connection.update( 1784 "UPDATE #{self.class.table_name} " + 1785 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + 1786 "WHERE #{self.class.primary_key} = #{quote_value(id)}", 1787 "#{self.class.name} Update" 1788 ) 1792 connection.update(sql_for_update, "#{self.class.name} Update") 1789 1793 end 1790 1794 1795 def sql_for_update 1796 "UPDATE #{self.class.table_name}" + ' ' + sql_for_update_values + ' ' + sql_for_update_conditions 1797 end 1798 1799 def sql_for_update_values 1800 "SET #{quoted_comma_pair_list(connection, attributes_for_update)}" 1801 end 1802 1803 def attributes_for_update 1804 attributes_with_quotes(false) 1805 end 1806 1807 def sql_for_update_conditions 1808 "WHERE #{self.class.primary_key} = #{quote_value(id)}" 1809 end 1810 1791 1811 # Creates a record with values matching those of the instance attributes 1792 1812 # and returns its id. 1793 1813 def create … … 1795 1815 self.id = connection.next_sequence_value(self.class.sequence_name) 1796 1816 end 1797 1817 1798 self.id = connection.insert( 1799 "INSERT INTO #{self.class.table_name} " + 1800 "(#{quoted_column_names.join(', ')}) " + 1801 "VALUES(#{attributes_with_quotes.values.join(', ')})", 1802 "#{self.class.name} Create", 1818 self.id = connection.insert(sql_for_create, "#{self.class.name} Create", 1803 1819 self.class.primary_key, self.id, self.class.sequence_name 1804 1820 ) 1805 1821 … … 1807 1823 id 1808 1824 end 1809 1825 1826 def sql_for_create 1827 "INSERT INTO #{self.class.table_name} " + 1828 "(#{columns_for_create.join(', ')}) " + 1829 "VALUES(#{attributes_for_create.values.join(', ')})" 1830 end 1831 1832 alias_method :columns_for_create, :quoted_column_names 1833 alias_method :attributes_for_create, :attributes_with_quotes 1834 1810 1835 # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord descendent. 1811 1836 # Considering the hierarchy Reply < Message < ActiveRecord, this makes it possible to do Reply.new without having to 1812 1837 # set Reply[Reply.inheritance_column] = "Reply" yourself. No such attribute would be set for objects of the