trackable 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg/*
2
+ test/debug.log
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -2,13 +2,13 @@ module ActsAsTrackable
2
2
  def self.included(base)
3
3
  base.send :extend, ClassMethods
4
4
  base.instance_eval{
5
- cattr_accessor :eventable_options
5
+ class_inheritable_accessor :eventable_options
6
6
  }
7
7
  end
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def trackable(options)
11
+ def trackable(options={})
12
12
  instance_eval{
13
13
  self.eventable_options = options
14
14
  }
@@ -20,7 +20,7 @@ module ActsAsTrackable
20
20
 
21
21
  module InstanceMethods
22
22
  def record_events
23
- active_keys = changes.keys.reject{ |key| %w{id created_at updated_at}.include?(key)}
23
+ active_keys = changes.keys.reject{ |key| %w{id created_at created_on updated_at updated_on}.include?(key)}
24
24
  active_keys.map do |key|
25
25
  old_val, new_val = changes[key]
26
26
  attrs = Event.attributes_from(self, key, old_val, new_val)
@@ -1,26 +1,25 @@
1
1
  class Event < ActiveRecord::Base
2
2
 
3
3
  def self.attributes_from(model, key, old_val, new_val)
4
- return if old_val.blank? && new_val.blank? #not something we need to track.
4
+ return if old_val.blank? && new_val.blank? # not something we need to track.
5
5
  eventable_options = model.class.eventable_options
6
- return if [eventable_options[:exclude]].flatten.include?(key.to_sym)
7
- msg = if eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][new_val]
8
- eventable_options[:events][key.to_sym][new_val]
9
- elsif eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][:message]
10
- the_proc = eventable_options[:events][key.to_sym][:message]
11
- if key.ends_with?("_id") # hackish, but this is a convention
12
- reference = key[0..-4].to_sym
13
- the_proc.call(model.send(reference).to_s)
14
- else
15
- the_proc.call(new_val)
16
- end
17
- elsif key.ends_with?("_id") # hackish, but this is a convention
18
- reference = key[0..-4].to_sym
6
+ return if Array(eventable_options[:exclude]).include?(key.to_sym)
7
+
8
+ events = eventable_options[:events]
9
+ attr_options = events[key.to_sym] if events
10
+ reference = key.ends_with?("_id") ? key[0..-4].to_sym : key.to_sym
11
+ association = model.class.reflect_on_association(reference)
12
+
13
+ message = if attr_options && attr_options[new_val]
14
+ attr_options[new_val]
15
+ elsif attr_options && msg_proc = attr_options[:message]
16
+ association ? msg_proc.call(model.send(reference).to_s) : msg_proc.call(new_val)
17
+ elsif association
19
18
  "#{key.humanize} changed to #{model.send(reference).to_s}"
20
19
  else
21
20
  "#{key.humanize} changed to #{new_val}"
22
21
  end
23
- {:field_name => key, :message => msg, :whodunnit => Trackable.whodunnit}
22
+ {:field_name => key, :message => message, :whodunnit => Trackable.whodunnit}
24
23
  end
25
24
 
26
25
  end
data/test/schema.rb CHANGED
@@ -7,6 +7,7 @@ ActiveRecord::Schema.define(:version => 0) do
7
7
  t.string :do_not_track
8
8
  t.integer :bar_id
9
9
  t.integer :custom_bar_id
10
+ t.integer :not_association_id
10
11
 
11
12
  t.timestamps
12
13
  end
@@ -134,5 +134,18 @@ class TrackableTest < Test::Unit::TestCase
134
134
  foo.update_attribute(:do_not_track, "Untracked")
135
135
  assert_equal 0, foo.events.size
136
136
  end
137
+
138
+ def test_checks_for_associations_when_creating_message
139
+ foo = Foo.create
140
+ assert_nothing_raised { foo.update_attribute(:not_association_id, 1) }
141
+ assert_equal "Not association changed to 1", foo.events.first.message
142
+ end
143
+
144
+ def test_dropping_trackable_on_model_works
145
+ assert_nothing_raised { Bar.trackable }
146
+ bar = Bar.create
147
+ bar.update_attribute(:name, "Megatron")
148
+ assert_equal "Name changed to Megatron", bar.events.first.message
149
+ end
137
150
 
138
151
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - bigfleet
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-24 00:00:00 -05:00
17
+ date: 2010-05-06 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -38,6 +38,7 @@ extensions: []
38
38
  extra_rdoc_files:
39
39
  - README.textile
40
40
  files:
41
+ - .gitignore
41
42
  - MIT-LICENSE
42
43
  - README.textile
43
44
  - Rakefile