vidibus-versioning 0.1.1 → 0.1.2

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.
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Versioning
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -13,6 +13,7 @@ module Vidibus
13
13
  field :version_updated_at, :type => Time
14
14
 
15
15
  after_initialize :original_attributes
16
+ after_initialize :set_version_updated_at, :unless => :version_updated_at
16
17
  before_update :reset_version_cache
17
18
 
18
19
  mattr_accessor :versioned_attributes, :unversioned_attributes, :versioning_options
@@ -24,11 +25,7 @@ module Vidibus
24
25
  # If no versioned attributes have been defined on class level,
25
26
  # all attributes will be returned except the unversioned ones.
26
27
  def versioned_attributes
27
- if self.class.versioned_attributes.any?
28
- attributes.only(self.class.versioned_attributes)
29
- else
30
- attributes.except(self.class.unversioned_attributes)
31
- end
28
+ filter_versioned_attributes(attributes)
32
29
  end
33
30
  end
34
31
 
@@ -130,11 +127,6 @@ module Vidibus
130
127
  version_obj and version_obj.new_record?
131
128
  end
132
129
 
133
- # Returns the time when versioned attributes have been updated.
134
- def version_updated_at
135
- read_attribute(:version_updated_at) || updated_at
136
- end
137
-
138
130
  protected
139
131
 
140
132
  # Applies version on self. Returns nil
@@ -170,10 +162,21 @@ module Vidibus
170
162
  # TODO: Setting the following line will cause DelayedJob to loop endlessly. The same should happen if an embedded document is defined as versioned_attribute!
171
163
  # original_attributes.merge(version_obj.versioned_attributes).merge(version_cache.wanted_attributes.stringify_keys!)
172
164
 
173
- Hash[*self.class.fields.keys.zip([]).flatten]. # ensure nil fields are included as well
174
- merge(version_obj.versioned_attributes). # add version's attributes
175
- merge(version_cache.wanted_attributes.stringify_keys!). # add options provided with #version call
176
- except(self.class.unversioned_attributes) # remove unversioned attributes; they may have changed
165
+ a = Hash[*self.class.fields.keys.zip([]).flatten]. # ensure nil fields are included as well
166
+ merge(version_obj.versioned_attributes) # add version's attributes
167
+ filter_versioned_attributes(a). # take versioned attributes only
168
+ merge(version_cache.wanted_attributes.stringify_keys!) # add options provided with #version call
169
+ end
170
+
171
+ # Returns versioned attributes from input by
172
+ # including defined attributes only or
173
+ # removing unversioned ones.
174
+ def filter_versioned_attributes(input)
175
+ if self.class.versioned_attributes.any?
176
+ input.only(self.class.versioned_attributes)
177
+ else
178
+ input.except(self.class.unversioned_attributes)
179
+ end
177
180
  end
178
181
 
179
182
  # Sets instance variables used for versioning.
@@ -224,7 +227,7 @@ module Vidibus
224
227
  obj
225
228
  else
226
229
  editing_time = self.class.versioning_options[:editing_time]
227
- if !editing_time or version_updated_at <= (Time.now-editing_time.to_i) or version_updated_at > Time.now # allow future versions
230
+ if !editing_time or version_updated_at <= (Time.now-editing_time.to_i) or updated_at > Time.now # allow future versions
228
231
  versions.build(:created_at => updated_at_was)
229
232
  end
230
233
  end
@@ -290,6 +293,10 @@ module Vidibus
290
293
  @original_attributes = versioned_attributes
291
294
  @version_cache = nil
292
295
  end
296
+
297
+ def set_version_updated_at
298
+ self.version_updated_at ||= updated_at || Time.now
299
+ end
293
300
  end
294
301
  end
295
302
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-versioning
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Pankratz