Skip to content

Commit

Permalink
Merge pull request #11650 from prathamesh-sonpatki/rename
Browse files Browse the repository at this point in the history
Renamed private methods _create_record and _update_record

Conflicts:
	activerecord/lib/active_record/persistence.rb
	activerecord/lib/active_record/relation.rb
	activerecord/test/cases/associations/belongs_to_associations_test.rb
  • Loading branch information
rafaelfranca committed Apr 4, 2014
1 parent c2d84ea commit 1755958
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 17 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Make possible to have an association called `records`.

Fixes #11645.

*prathamesh-sonpatki*

* `to_sql` on an association now matches the query that is actually executed, where it
could previously have incorrectly accrued additional conditions (e.g. as a result of
a previous query). CollectionProxy now always defers to the association scope's
Expand Down
Expand Up @@ -118,11 +118,11 @@ def build(attributes = {}, &block)
end

def create(attributes = {}, &block)
create_record(attributes, &block)
_create_record(attributes, &block)
end

def create!(attributes = {}, &block)
create_record(attributes, true, &block)
_create_record(attributes, true, &block)
end

# Add +records+ to this association. Returns +self+ so method calls may
Expand Down Expand Up @@ -449,13 +449,13 @@ def merge_target_lists(persisted, memory)
persisted + memory
end

def create_record(attributes, raise = false, &block)
def _create_record(attributes, raise = false, &block)
unless owner.persisted?
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end

if attributes.is_a?(Array)
attributes.collect { |attr| create_record(attr, raise, &block) }
attributes.collect { |attr| _create_record(attr, raise, &block) }
else
transaction do
add_to_target(build_record(attributes)) do |record|
Expand Down
Expand Up @@ -18,11 +18,11 @@ def writer(record)
end

def create(attributes = {}, &block)
create_record(attributes, &block)
_create_record(attributes, &block)
end

def create!(attributes = {}, &block)
create_record(attributes, true, &block)
_create_record(attributes, true, &block)
end

def build(attributes = {})
Expand Down Expand Up @@ -51,7 +51,7 @@ def set_new_record(record)
replace(record)
end

def create_record(attributes, raise_error = false)
def _create_record(attributes, raise_error = false)
record = build_record(attributes)
yield(record) if block_given?
saved = record.save
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -70,11 +70,11 @@ def write_attribute(attr, value)
super(attr, value)
end

def update_record(*)
def _update_record(*)
partial_writes? ? super(keys_for_partial_write) : super
end

def create_record(*)
def _create_record(*)
partial_writes? ? super(keys_for_partial_write) : super
end

Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/callbacks.rb
Expand Up @@ -302,11 +302,11 @@ def create_or_update #:nodoc:
run_callbacks(:save) { super }
end

def create_record #:nodoc:
def _create_record #:nodoc:
run_callbacks(:create) { super }
end

def update_record(*) #:nodoc:
def _update_record(*) #:nodoc:
run_callbacks(:update) { super }
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/locking/optimistic.rb
Expand Up @@ -66,7 +66,7 @@ def increment_lock
send(lock_col + '=', previous_lock_value + 1)
end

def update_record(attribute_names = @attributes.keys) #:nodoc:
def _update_record(attribute_names = @attributes.keys) #:nodoc:
return super unless locking_enabled?
return 0 if attribute_names.empty?

Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -477,13 +477,13 @@ def relation_for_destroy

def create_or_update
raise ReadOnlyRecord if readonly?
result = new_record? ? create_record : update_record
result = new_record? ? _create_record : _update_record
result != false
end

# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update_record(attribute_names = @attributes.keys)
def _update_record(attribute_names = @attributes.keys)
attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
if attributes_with_values.empty?
0
Expand All @@ -506,7 +506,7 @@ def update_record(attribute_names = @attributes.keys)

# Creates a record with values matching those of the instance attributes
# and returns its id.
def create_record(attribute_names = @attributes.keys)
def _create_record(attribute_names = @attributes.keys)
attributes_values = arel_attributes_with_values_for_create(attribute_names)

new_id = self.class.unscoped.insert attributes_values
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/timestamp.rb
Expand Up @@ -43,7 +43,7 @@ def initialize_dup(other) # :nodoc:

private

def create_record
def _create_record
if self.record_timestamps
current_time = current_time_from_proper_timezone

Expand All @@ -57,7 +57,7 @@ def create_record
super
end

def update_record(*args)
def _update_record(*args)
if should_record_timestamps?
current_time = current_time_from_proper_timezone

Expand Down
Expand Up @@ -866,4 +866,22 @@ def test_reflect_the_most_recent_change
assert post.save
assert_equal post.author_id, author2.id
end

test 'belongs_to works with model name Record' do
Record = Class.new(ActiveRecord::Base) do
connection.create_table :records
end

Foo = Class.new(ActiveRecord::Base) do
connection.create_table :foos do |t|
t.belongs_to :record
end

belongs_to :record
end

record = Record.create!
Foo.create! record: record
assert_equal 1, Foo.count
end
end

0 comments on commit 1755958

Please sign in to comment.