Skip to content

Commit

Permalink
updating
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydreier committed Dec 17, 2009
1 parent 4220267 commit a37041f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions dirty_associations
Submodule dirty_associations added at 4ce062
12 changes: 11 additions & 1 deletion lib/dirty_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def keep_track_of(*reflections)

include InstanceMethods
end


module Settings
ALlOWED_ASSOCIATIONS = [:has_many, :has_and_belongs_to_many]
end

module InstanceMethods

Expand All @@ -75,7 +80,7 @@ def track_association_changes(&block)
def initialize_dirty_associations
self.class.dirty_associations.each do |reflection|
assoc_name = reflection.to_s.singularize
if respond_to?("#{assoc_name}_ids".to_sym)
if is_valid_association?(reflection)
original_associations["#{assoc_name}_original_ids".to_sym] = send("#{assoc_name}_ids".to_sym).dup
instance_eval <<-EOV
def #{assoc_name}_ids_were; (original_associations["#{assoc_name}_original_ids".to_sym] || []).uniq; end;
Expand Down Expand Up @@ -114,6 +119,11 @@ def original_associations
@original_associations ||= {}
end

# Returns boolean if the given association is actually an active association of the current model
def is_valid_association?(association_name)
type = self.class.reflect_on_association(association_name.to_sym) && self.class.reflect_on_association(association_name.to_sym).macro
DirtyAssociations::Settings::ALlOWED_ASSOCIATIONS.include?(type)
end

end

Expand Down
6 changes: 6 additions & 0 deletions test/dirty_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class DirtyAssociationsTest < ActiveSupport::TestCase
assert task.respond_to?(:track_association_changes)
end

test "raise an error if given an invalid association" do
assert_raises ArgumentError do
t = Task.first
end
end

# test "throws an error if no associations are specified" do
# assert_raise ArgumentError do
# class Keyword < ActiveRecord::Base
Expand Down

0 comments on commit a37041f

Please sign in to comment.