Module: Secretary::TracksAssociation::ClassMethods
- Defined in:
- lib/secretary/tracks_association.rb
Instance Method Summary collapse
-
#tracks_association(*associations) ⇒ Object
Track the associations passed-in This will make sure that when you change the association, the saved record will get a new version, with association diffs and everything.
Instance Method Details
#tracks_association(*associations) ⇒ Object
Track the associations passed-in This will make sure that when you change the association, the saved record will get a new version, with association diffs and everything.
Arguments
associations - (Symbols) A variable number of association
names to track
Example
has_secretary
has_many :bylines,
:as => :content,
:class_name => "ContentByline",
:dependent => :destroy
tracks_association :bylines
If you want to control when an association should be left out of the version, define an instance method named ‘should_reject_#name?`. This method takes a hash of the model’s attributes (so you can pass in, for example, form params). This also lets you easily share this method with ‘accepts_nested_attributes_for`.
Example
class Person < ActiveRecord::Base
has_secretary
has_many :animals
tracks_association :animals
accepts_nested_attributes_for :animals,
:reject_if => :should_reject_animals?
private
def should_reject_animals?(attributes)
attributes['name'].blank?
end
end
Returns nothing
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/secretary/tracks_association.rb', line 51 def tracks_association(*associations) if !self.has_secretary? raise NotVersionedError, self.name end include DirtyAssociations associations.each do |name| reflection = self.reflect_on_association(name) if !reflection raise NoAssociationError, name, self.name end # If the environment is loaded, the following line will be # evaluated for any `tracks_association` calls. # `versioned_attributes` calls `self.column_names`, # which requires the table to exist. # # So the problem is that if our database or table doesn't exist, # we can't load the environment, and the environment needs to be # loaded in order to load in the schema. # # So, we rescue! And warn. begin self.versioned_attributes << name.to_s if reflection.macro == :belongs_to self.versioned_attributes << reflection.foreign_key end rescue => e warn "[secretary-rails] Caught an error while loading " \ "#{self.name}. #{e}" end define_dirty_association_methods(name, reflection) if reflection.collection? include DirtyAssociations::Collection add_collection_callbacks(name, reflection) else include DirtyAssociations::Singular define_singular_association_writer(name) end end end |