reference_tracking 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/array/grouping'
2
+
1
3
  module ReferenceTracking
2
4
  module ActionController
3
5
  module ActMacro
@@ -59,12 +61,40 @@ module ReferenceTracking
59
61
  end
60
62
 
61
63
  module Purging
64
+ SKIP_ATTRIBUTES = %w(id created_at updated_at)
65
+
62
66
  def purge(objects)
63
- tags = objects.map do |object|
64
- methods = (object.previous_changes.keys & object.attributes.keys) - %w(updated_at)
67
+ tags = objects.map { |object| purge_tags_for(object) }.flatten
68
+ response.headers[purging_options[:header]] = tags
69
+ end
70
+
71
+ def purge_tags_for(object)
72
+ case params[:action]
73
+ when 'create', 'destroy'
74
+ tags = purge_associations_for(object).map do |owner, method|
75
+ ReferenceTracking.to_tag(owner, method)
76
+ end
77
+ tags << ReferenceTracking.to_tag(object) if params[:action] == 'destroy'
78
+ tags
79
+ when 'update'
80
+ methods = object.previous_changes.keys & object.attributes.keys - SKIP_ATTRIBUTES
65
81
  methods.map { |method| ReferenceTracking.to_tag(object, method) }
66
82
  end
67
- response.headers[purging_options[:header]] = tags.flatten
83
+ end
84
+
85
+ def purge_associations_for(object)
86
+ superclasses = purge_super_classes_for(object)
87
+ object.class.reflect_on_all_associations(:belongs_to).map do |association|
88
+ if owner = object.send(association.name)
89
+ owner.class.reflect_on_all_associations.map do |association|
90
+ [owner, association.name] if superclasses.include?(association.class_name)
91
+ end
92
+ end
93
+ end.flatten.compact.in_groups_of(2)
94
+ end
95
+
96
+ def purge_super_classes_for(object)
97
+ object.class.ancestors.select { |klass| klass < ActiveRecord::Base }.map(&:name)
68
98
  end
69
99
  end
70
100
  end
@@ -1,3 +1,3 @@
1
1
  module ReferenceTracking
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reference_tracking
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-03 00:00:00 +02:00
18
+ date: 2010-10-10 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21