serialized_attr_accessors 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/serialized_attr_accessors.rb +28 -1
  2. metadata +2 -2
@@ -1,4 +1,5 @@
1
1
  module SerializedAttrAccessors
2
+
2
3
  def self.included(base)
3
4
  base.extend(ClassMethods)
4
5
 
@@ -13,7 +14,13 @@ module SerializedAttrAccessors
13
14
  end
14
15
  end
15
16
 
17
+ #Re-sets sattr_change_set
18
+ def reset_sattr_change_set
19
+ self.sattr_change_set.clear
20
+ end
21
+
16
22
  base.send(:before_validation, :populate_serialized_attributes)
23
+ base.send(:after_save, :reset_sattr_change_set)
17
24
  end
18
25
 
19
26
  module ClassMethods
@@ -57,6 +64,11 @@ module SerializedAttrAccessors
57
64
  #If attributes are not serialized then here is serialization done
58
65
  self.serialize(current_serialized_attr) unless self.serialized_attributes.keys.include?(current_serialized_attr.to_s)
59
66
 
67
+ #Book keeping of any value changed for any sattr
68
+ define_method :sattr_change_set do
69
+ @attr_change_set ||= {}
70
+ end
71
+
60
72
  #Defining method to fetch serialzed parent attribute (gives last found)
61
73
  define_method :fetch_parent_attribute do |filed_name|
62
74
  parent_attr = nil
@@ -69,7 +81,11 @@ module SerializedAttrAccessors
69
81
 
70
82
  define_method field_name do
71
83
  field_value = unserialized_options(fetch_parent_attribute(field_name))[field_name]
72
- (field_value.nil? ? default_value : field_value)
84
+ final_value = (field_value.nil? ? default_value : field_value)
85
+
86
+ #Updates sattr_change_set if field_name is not already present in it
87
+ send(:sattr_change_set)[field_name.to_s] = final_value unless send(:sattr_change_set).include?(field_name.to_s)
88
+ final_value
73
89
  end
74
90
 
75
91
  define_method "#{field_name.to_s}=" do |field_value|
@@ -101,6 +117,17 @@ module SerializedAttrAccessors
101
117
  end
102
118
  end
103
119
  end
120
+
121
+ #Recording sattr value if it is able to respond to ActiveModel::Dirty's changed_attributes
122
+ if send("respond_to?", "changed_attributes")
123
+ send(field_name)
124
+ if send(:sattr_change_set)[field_name.to_s] == field_value
125
+ changed_attributes.delete(field_name.to_s)
126
+ else
127
+ changed_attributes[field_name.to_s] = send(:sattr_change_set)[field_name.to_s]
128
+ end
129
+ end
130
+
104
131
  unserialized_options(fetch_parent_attribute(field_name)).merge!(field_name => field_value)
105
132
  field_value
106
133
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialized_attr_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-07 00:00:00.000000000Z
12
+ date: 2013-05-12 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: attribute accessor generator using sattr_accessor (with default field
15
15
  :serialized_options) or a different serialized field using for_serialized_field