Skip to content

Commit

Permalink
Merge pull request #28479 from kamipo/evaluate_default_block_only_whe…
Browse files Browse the repository at this point in the history
…n_necessary

Evaluate the default block only when necessary
  • Loading branch information
kaspth committed Mar 20, 2017
2 parents 1934e93 + 4c1d221 commit 95911db
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def replace(record)
self.target = record
end

def default(record)
writer(record) if reader.nil?
def default(&block)
writer(instance_exec(&block)) if reader.nil?
end

def reset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def self.add_touch_callbacks(model, reflection)

def self.add_default_callbacks(model, reflection)
model.before_validation lambda { |o|
o.association(reflection.name).default o.instance_exec(&reflection.options[:default])
o.association(reflection.name).default(&reflection.options[:default])
}
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/delegation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def inherited(child_class)
# may vary depending on the klass of a relation, so we create a subclass of Relation
# for each different klass, and the delegations are compiled into that subclass only.

delegate :to_xml, :encode_with, :length, :collect, :map, :each, :all?, :include?, :to_ary, :join,
delegate :to_xml, :encode_with, :length, :collect, :uniq, :each, :all?, :include?, :to_ary, :join,
:[], :&, :|, :+, :-, :sample, :reverse, :compact, :in_groups, :in_groups_of,
:to_sentence, :to_formatted_s, :as_json,
:shuffle, :split, :index, to: :records
Expand Down

0 comments on commit 95911db

Please sign in to comment.