Skip to content

Commit

Permalink
Add Model#set_associated_object, used by the many_to_one setter metho…
Browse files Browse the repository at this point in the history
…d, for easier overriding

This is for consistency with the one_to_many and many_to_many
association methods (e.g. add_associated_object).  It allows you
to override behavior for all many_to_one setters by overriding a
single method.
  • Loading branch information
jeremyevans committed Feb 25, 2009
1 parent 455d5a6 commit be8e2a6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== HEAD

* Add Model#set_associated_object, used by the many_to_one setter method, for easier overriding (jeremyevans)

* Allow use of database independent types when casting (jeremyevans)

* Give association datasets knowledge of the model object that created them and the related association reflection (jeremyevans)
Expand Down
16 changes: 1 addition & 15 deletions lib/sequel_model/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,7 @@ def def_many_to_one(opts)
return if opts[:read_only]

association_module_private_def(opts._setter_method){|o| send(:"#{key}=", (o.send(opts.primary_key) if o))}

association_module_def(opts.setter_method) do |o|
raise(Sequel::Error, "model object #{model} does not have a primary key") if o && !o.pk
old_val = send(opts.association_method)
return o if old_val == o
return if old_val and run_association_callbacks(opts, :before_remove, old_val) == false
return if o and run_association_callbacks(opts, :before_add, o) == false
send(opts._setter_method, o)
associations[name] = o
remove_reciprocal_object(opts, old_val) if old_val
add_reciprocal_object(opts, o) if o
run_association_callbacks(opts, :after_add, o) if o
run_association_callbacks(opts, :after_remove, old_val) if old_val
o
end
association_module_def(opts.setter_method){|o| set_associated_object(opts, o)}
end

# Adds one_to_many association instance methods
Expand Down
18 changes: 18 additions & 0 deletions lib/sequel_model/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,24 @@ def save_failure(type)
end
end

# Set the given object as the associated object for the given association
def set_associated_object(opts, o)
raise(Sequel::Error, "model object #{model} does not have a primary key") if o && !o.pk
old_val = send(opts.association_method)
return o if old_val == o
return if old_val and run_association_callbacks(opts, :before_remove, old_val) == false
return if o and run_association_callbacks(opts, :before_add, o) == false
send(opts._setter_method, o)
associations[opts[:name]] = o
remove_reciprocal_object(opts, old_val) if old_val
if o
add_reciprocal_object(opts, o)
run_association_callbacks(opts, :after_add, o)
end
run_association_callbacks(opts, :after_remove, old_val) if old_val
o
end

# Set the columns, filtered by the only and except arrays.
def set_restricted(hash, only, except)
columns_not_set = model.instance_variable_get(:@columns).blank?
Expand Down

0 comments on commit be8e2a6

Please sign in to comment.