trackzor 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/trackzor.rb +55 -21
  3. data/trackzor.gemspec +2 -2
  4. metadata +2 -2
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('trackzor', '0.1.2') do |gem|
5
+ Echoe.new('trackzor', '0.1.3') do |gem|
6
6
  gem.description = "Track ATTR_updated_at and ATTR_updated_by"
7
7
  gem.url = "http://github.com/corneldm/trackzor"
8
8
  gem.author = "David Cornelius"
data/lib/trackzor.rb CHANGED
@@ -5,47 +5,81 @@ module Trackzor
5
5
 
6
6
  module ClassMethods
7
7
  def trackzored(options = {})
8
+ class_inheritable_reader :trackzor_exempt_columns
9
+
8
10
  if options[:only]
9
11
  except = self.column_names - options[:only].flatten.map(&:to_s)
10
12
  else
11
13
  except = [self.primary_key, inheritance_column, 'lock_version', 'created_at', 'updated_at']
12
14
  except |= Array(options[:except]).collect(&:to_s) if options[:except]
13
15
  end
14
-
16
+ write_inheritable_attribute :trackzor_exempt_columns, except
15
17
  aaa_present = self.respond_to?(:non_audited_columns)
16
18
 
17
- (self.column_names - except).select{|column| column =~ /_updated_by|_updated_at/ }.each do |col|
18
- if col =~ /_updated_by/
19
- belongs_to "#{col.split('_updated_by')[0]}_source".to_sym, :class_name => 'User', :foreign_key => col
19
+ # create ATTR_source associations
20
+ (self.column_names - self.trackzor_exempt_columns).select{|column| column =~ /(_updated_by|_updated_at)$/ }.each do |col|
21
+ if col =~ /_updated_by$/
22
+ belongs_to col.sub(/_updated_by$/, '_source').to_sym, :class_name => 'User', :foreign_key => col
20
23
  end
21
-
22
24
  self.non_audited_columns << col if aaa_present
23
25
  end
24
26
 
25
- validate do |record|
26
- user = Thread.current[:trackzor_user] || Thread.current[:acts_as_audited_user]
27
+ validate :trackzor_assign_and_validate
27
28
 
28
- record.changes.keys.each do |attr|
29
- unless except.include?(attr)
30
- time_column = "#{attr}_updated_at"
31
- user_association = "#{attr}_source"
29
+ include Trackzor::InstanceMethods
30
+ end
31
+ end # ClassMethods
32
32
 
33
- if record.respond_to?(time_column.to_sym)
34
- record.send("#{time_column}=".to_sym, Time.now)
35
- end
33
+ module InstanceMethods
34
+ def trackzor_assign_and_validate
35
+ user = Thread.current[:trackzor_user] || Thread.current[:acts_as_audited_user]
36
36
 
37
- if record.respond_to?(user_association.to_sym)
38
- if user
39
- record.send("#{user_association}=".to_sym, user)
40
- else
41
- record.errors.add("#{attr}_updated_by", "requires Trackzor user to be set")
42
- end
37
+ self.changes.keys.each do |attr|
38
+ unless self.trackzor_exempt_columns.include?(attr)
39
+ time_column = "#{attr}_updated_at"
40
+ user_association = "#{attr}_source"
41
+
42
+ if self.respond_to?(time_column.to_sym)
43
+ self.send("#{time_column}=".to_sym, Time.now)
44
+ end
45
+
46
+ if self.respond_to?(user_association.to_sym)
47
+ if user
48
+ self.send("#{user_association}=".to_sym, user)
49
+ else
50
+ self.errors.add("#{attr}_updated_by", "requires Trackzor user to be set")
43
51
  end
44
52
  end
45
53
  end
46
54
  end
47
55
  end
48
- end
56
+
57
+ # force update of multiple attributes
58
+ def will_update_attributes!(new_attributes, guard_protected_attributes = true)
59
+ return if new_attributes.nil?
60
+ attributes = new_attributes.dup
61
+ attributes.stringify_keys!
62
+
63
+ multi_parameter_attributes = []
64
+ attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes
65
+
66
+ attributes.each do |k, v|
67
+ if k.include?("(")
68
+ multi_parameter_attributes << [ k, v ]
69
+ else
70
+ if respond_to?("#{k}=")
71
+ send("#{k}=", v)
72
+ send("#{k}_will_change!")
73
+ else
74
+ raise "unknown attribute: #{k}"
75
+ end
76
+ end
77
+ end
78
+
79
+ assign_multiparameter_attributes(multi_parameter_attributes)
80
+ save!
81
+ end
82
+ end # InstanceMethods
49
83
 
50
84
  # All X attribute changes where X_updated_by exists will be recorded as made by +user+.
51
85
  def self.as_user(user, &block)
data/trackzor.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{trackzor}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["David Cornelius"]
9
- s.date = %q{2010-01-18}
9
+ s.date = %q{2010-01-22}
10
10
  s.description = %q{Track ATTR_updated_at and ATTR_updated_by}
11
11
  s.email = %q{david.cornelius@bluefishwireless.net}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/trackzor.rb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trackzor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cornelius
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-18 00:00:00 -05:00
12
+ date: 2010-01-22 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15