Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6542: more_injection_points.diff

File more_injection_points.diff, 4.1 kB (added by ymendel, 2 years ago)
  • base.rb

    old new  
    444444          attributes.collect { |attr| create(attr) } 
    445445        else 
    446446          object = new(attributes) 
    447           scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create
     447          create_scope_to_object(object
    448448          object.save 
    449449          object 
    450450        end 
    451451      end 
    452452 
     453      def create_scope_to_object(object) 
     454        scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create) 
     455      end 
     456 
    453457      # Finds the record from the passed +id+, instantly saves it with the passed +attributes+ (if the validation permits it), 
    454458      # and returns it. If the save fails under validations, the unsaved object is still returned. 
    455459      # 
     
    15421546      # be made (since they can't be persisted). 
    15431547      def destroy 
    15441548        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") 
    15491550        end 
    15501551 
    15511552        freeze 
    15521553      end 
    15531554 
     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 
    15541563      # Returns a clone of the record that hasn't been assigned an id yet and 
    15551564      # is treated as a new record.  Note that this is a "shallow" clone: 
    15561565      # it copies the object's attributes only, not its associations. 
     
    17801789      # Updates the associated record with values matching those of the instance attributes. 
    17811790      # Returns the number of affected rows. 
    17821791      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") 
    17891793      end 
    17901794 
     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 
    17911811      # Creates a record with values matching those of the instance attributes 
    17921812      # and returns its id. 
    17931813      def create 
     
    17951815          self.id = connection.next_sequence_value(self.class.sequence_name) 
    17961816        end 
    17971817 
    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", 
    18031819          self.class.primary_key, self.id, self.class.sequence_name 
    18041820        ) 
    18051821 
     
    18071823        id 
    18081824      end 
    18091825 
     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 
    18101835      # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord descendent. 
    18111836      # Considering the hierarchy Reply < Message < ActiveRecord, this makes it possible to do Reply.new without having to 
    18121837      # set Reply[Reply.inheritance_column] = "Reply" yourself. No such attribute would be set for objects of the