serialist 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 1.6.0
@@ -9,10 +9,18 @@ module Serialist
9
9
  attr_accessor :serialist_field
10
10
 
11
11
  def serialist(serialist_field, serialist_options = [])
12
- @serialist_field ||= serialist_field
13
- @serialist_options ||= []
14
- @serialist_options = (@serialist_options + serialist_options).uniq
12
+ @serialist_field = serialist_field
13
+ @serialist_options = serialist_options
15
14
  serialize(@serialist_field, Hash)
15
+ include Serialist::DirtyAdditions
16
+ class_eval do
17
+ unless method_defined?(:changes)
18
+ def changes(meth, *args, &block); super; end
19
+ end
20
+ alias_method :old_changes, :changes
21
+ alias_method :changes, :serialist_changes
22
+ end
23
+
16
24
  if serialist_options.empty?
17
25
  # catch-all mode
18
26
  include Serialist::InstanceMethods
@@ -55,8 +63,10 @@ module Serialist
55
63
  when "="
56
64
  define_method method do |param|
57
65
  self.send(serialist_field.to_s + "=", Hash.new) unless self.send(serialist_field)
58
- self.send(serialist_field)[method[0..-2].to_sym] = param
59
- write_attribute(serialist_field.to_s, self.send(serialist_field)) # needed to get dirty
66
+ # needed to get dirty
67
+ slug = self.send(serialist_field).clone
68
+ slug[method[0..-2].to_sym] = param
69
+ write_attribute(serialist_field.to_s, slug)
60
70
  end
61
71
  else
62
72
  define_method method do
@@ -98,6 +108,19 @@ module Serialist
98
108
  end
99
109
  end
100
110
  end
111
+
112
+ module DirtyAdditions
113
+ def serialist_changes
114
+ full_changes = self.old_changes
115
+ unless (slug = full_changes.delete(self.class.serialist_field.to_s)).blank?
116
+ ((slug[0].try(:keys) || []) + (slug[1].try(:keys) || [])).uniq.each do |serialized_key|
117
+ attr_change = [(slug[0].try(:fetch, serialized_key) rescue nil), (slug[1].try(:fetch, serialized_key) rescue nil)]
118
+ full_changes[serialized_key.to_s] = attr_change if attr_change[0] != attr_change[1]
119
+ end
120
+ end
121
+ full_changes
122
+ end
123
+ end
101
124
  end
102
125
 
103
126
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{serialist}
8
- s.version = "1.5.0"
8
+ s.version = "1.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benoit B\303\251n\303\251zech"]
@@ -87,6 +87,7 @@ class SerialistTest < Test::Unit::TestCase
87
87
  @serialisted.reload
88
88
  assert_equal @serialisted.foo, "foo2"
89
89
  @serialisted.baz = "baz"
90
+ assert_equal @serialisted.changes, {"baz"=>[nil, "baz"]}
90
91
  @serialisted.save
91
92
  @serialisted.reload
92
93
  assert_equal @serialisted.baz, "baz"
@@ -106,9 +107,11 @@ class SerialistTest < Test::Unit::TestCase
106
107
  assert_equal @serialisted.foo, "foo"
107
108
  assert_equal @serialisted.foo?, true
108
109
  @serialisted.foo = "foo2"
110
+ assert_equal @serialisted.changes, {"foo"=>["foo", "foo2"]}
109
111
  @serialisted.save
110
112
  @serialisted.reload
111
113
  assert_equal @serialisted.foo, "foo2"
114
+ assert_equal @serialisted.changes, {}
112
115
  @serialisted.bar = nil
113
116
  @serialisted.save
114
117
  @serialisted.reload
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 5
7
+ - 6
8
8
  - 0
9
- version: 1.5.0
9
+ version: 1.6.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Benoit B\xC3\xA9n\xC3\xA9zech"