Permalink
Browse files

Delete create, create! and create_record from HasManyThroughAssociati…

…on in exchange for more generic versions in AssociationCollection
  • Loading branch information...
1 parent 379c022 commit ffa57671bb664a715eb2eebaa7476c747abf0fb1 @jonleighton jonleighton committed with tenderlove Dec 16, 2010
@@ -263,15 +263,15 @@ def create(attrs = {})
else
create_record(attrs) do |record|
yield(record) if block_given?
- record.save
+ insert_record(record, false)
end
end
end
def create!(attrs = {})
create_record(attrs) do |record|
yield(record) if block_given?
- record.save!
+ insert_record(record, true)
end
end
@@ -488,18 +488,20 @@ def save_record(record, force, validate)
end
def create_record(attrs)
- attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
ensure_owner_is_persisted!
- scoped_where = scoped.where_values_hash
- create_scope = scoped_where ? @scope[:create].merge(scoped_where) : @scope[:create]
- record = @reflection.klass.send(:with_scope, :create => create_scope) do
- @reflection.build_association(attrs)
- end
- if block_given?
- add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }
- else
- add_record_to_target_with_callbacks(record)
+ attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
+ create_scope = @scope[:create].merge(scoped.where_values_hash || {})
+
+ transaction do
+ record = with_scope(:create => create_scope) do
+ @reflection.build_association(attrs)
+ end
+ if block_given?
+ add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }
+ else
+ add_record_to_target_with_callbacks(record)
+ end
end
end
@@ -9,14 +9,6 @@ class HasManyThroughAssociation < HasManyAssociation #:nodoc:
alias_method :new, :build
- def create!(attrs = nil)
- create_record(attrs, true)
- end
-
- def create(attrs = nil)
- create_record(attrs, false)
- end
-
def destroy(*records)
transaction do
delete_records(flatten_deeper(records))
@@ -35,16 +27,6 @@ def size
end
protected
- def create_record(attrs, force = true)
- ensure_owner_is_persisted!
-
- transaction do
- object = @reflection.klass.new(attrs)
- add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) }
- object
- end
- end
-
def target_reflection_has_associated_record?
if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank?
false

0 comments on commit ffa5767

Please sign in to comment.