mongoid-history 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc676c3c474a938c28348873980b310b13a23755
4
- data.tar.gz: cf12dbab2b4462f7697f0c98e6c15004718df721
3
+ metadata.gz: 1fa9bab26aeaf58f5e6f047b910f5d7e09c68e6d
4
+ data.tar.gz: 9da6ba97971fe87d296fd995ec73cf6562e3d49e
5
5
  SHA512:
6
- metadata.gz: 8f3a8201b62c8a378a6f5707f047c58c6dc161000289fa0a835b05f732ab3e15b64fb10cc7be8d55ff434d680f72df5ed6e9a79ebf8a4f85067027e32666852a
7
- data.tar.gz: 41e023a44672b5091e791ac365b88e3ea71b46db42ebfeb4b50a647f51a260579492c07b18897713472a494b994c783b7e21028f29fa000507d1425e71050ab8
6
+ metadata.gz: 56c010855bf25272c2cc7af7589077a2903afb2d3f3e899e3d6a3dfd7c795cce513e79e9aae0ebc857d09a42c2f13261aa55b250b88f5331f7ad593ffa1765ed
7
+ data.tar.gz: 6b8eedd5c6138854a8b8e0c9de1c0fc8a846efc90e03cd6ea3a934ed91c8437cb9a4fbf029a4e60acf9ddd2b2bf9ae4866748e0c7dc3149a24ba569a79b71a81
@@ -3,74 +3,4 @@ AllCops:
3
3
  - vendor/**/*
4
4
  - bin/**/*
5
5
 
6
- LineLength:
7
- Enabled: false
8
-
9
- MethodLength:
10
- Enabled: false
11
-
12
- ClassLength:
13
- Enabled: false
14
-
15
- Documentation:
16
- # don't require classes to be documented
17
- Enabled: false
18
-
19
- CollectionMethods:
20
- # don't prefer map to collect, recuce to inject
21
- Enabled: false
22
-
23
- Encoding:
24
- # no need to always specify encoding
25
- Enabled: false
26
-
27
- StringLiterals:
28
- # use single or double-quoted strings, as you please
29
- Enabled: false
30
-
31
- Void:
32
- # == operator used in void context in specs
33
- Enabled: false
34
-
35
- SignalException:
36
- # prefer raise to fail
37
- EnforcedStyle: only_raise
38
-
39
- RaiseArgs:
40
- # don't care for what kind of raise
41
- Enabled: false
42
-
43
- PerlBackrefs:
44
- # TODO: regular expression matching with $1, $2, etc.
45
- Enabled: false
46
-
47
- BlockNesting:
48
- # TODO: fix too much nesting
49
- Max: 4
50
-
51
- Lambda:
52
- # TODO: replace all lambda with -> or Proc
53
- Enabled: false
54
-
55
- Blocks:
56
- # allow multi-line blocks like expect { }
57
- Enabled: false
58
-
59
- WordArray:
60
- # %w vs. [ '', ... ]
61
- Enabled: false
62
-
63
- CyclomaticComplexity:
64
- Enabled: false
65
-
66
- HandleExceptions:
67
- Enabled: false
68
-
69
- MultilineBlockChain:
70
- Enabled: false
71
-
72
- FileName:
73
- Enabled: false
74
-
75
- RegexpLiteral:
76
- Enabled: false
6
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,61 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-12-09 07:20:55 -0500 using RuboCop version 0.27.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 3
9
+ Lint/HandleExceptions:
10
+ Enabled: false
11
+
12
+ # Offense count: 27
13
+ Lint/Void:
14
+ Enabled: false
15
+
16
+ # Offense count: 12
17
+ Metrics/AbcSize:
18
+ Max: 51
19
+
20
+ # Offense count: 4
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 10
23
+
24
+ # Offense count: 181
25
+ # Configuration parameters: AllowURI, URISchemes.
26
+ Metrics/LineLength:
27
+ Max: 688
28
+
29
+ # Offense count: 7
30
+ # Configuration parameters: CountComments.
31
+ Metrics/MethodLength:
32
+ Max: 32
33
+
34
+ # Offense count: 4
35
+ Metrics/PerceivedComplexity:
36
+ Max: 12
37
+
38
+ # Offense count: 37
39
+ Style/Documentation:
40
+ Enabled: false
41
+
42
+ # Offense count: 6
43
+ Style/EachWithObject:
44
+ Enabled: false
45
+
46
+ # Offense count: 1
47
+ # Configuration parameters: Exclude.
48
+ Style/FileName:
49
+ Enabled: false
50
+
51
+ # Offense count: 1
52
+ Style/Lambda:
53
+ Enabled: false
54
+
55
+ # Offense count: 3
56
+ Style/MultilineBlockChain:
57
+ Enabled: false
58
+
59
+ # Offense count: 2
60
+ Style/RegexpLiteral:
61
+ MaxSlashes: 0
@@ -1,5 +1,7 @@
1
1
  services: mongodb
2
2
 
3
+ sudo: false
4
+
3
5
  language: ruby
4
6
 
5
7
  cache: bundler
@@ -8,7 +10,7 @@ rvm:
8
10
  - 2.1.1
9
11
  - 2.0.0
10
12
  - 1.9.3
11
- - rbx-2
13
+ - rbx-2.2.10
12
14
  - jruby-19mode
13
15
 
14
16
  env:
@@ -1,3 +1,11 @@
1
+ 0.4.5 (2/9/2015)
2
+ ----------------
3
+
4
+ * [#131](https://github.com/aq1018/mongoid-history/pull/131) - Add `undo` method, that helps to get specific version of an object without saving changes - [@alexkravets](https://github.com/alexkravets).
5
+ * [#127](https://github.com/aq1018/mongoid-history/pull/127) - Fix gem naming per [rubygems](http://guides.rubygems.org/name-your-gem/) specs, now you can `require 'mongoid/history'` - [@nofxx](https://github.com/nofxx).
6
+ * [#129](https://github.com/aq1018/mongoid-history/pull/129) - Support multiple levels of embedded polimorphic documents - [@BrunoChauvet](https://github.com/BrunoChauvet).
7
+ * [#123](https://github.com/aq1018/mongoid-history/pull/123): Use a method compatible with mongoid-observers to determinine the version of Mongoid - [@zeitnot](https://github.com/zeitnot).
8
+
1
9
  0.4.4 (7/29/2014)
2
10
  -----------------
3
11
 
data/README.md CHANGED
@@ -121,6 +121,9 @@ track.undo! user # comment title should be "Test"
121
121
 
122
122
  track.redo! user # comment title should be "Test 2"
123
123
 
124
+ # undo comment to version 1 without save
125
+ comment.undo nil, from: 1, to: comment.version
126
+
124
127
  # undo last change
125
128
  comment.undo! user
126
129
 
@@ -238,6 +241,27 @@ instructions above then run the following command:
238
241
  MyHistoryTracker.all.each{|ht| ht.rename(:modifier_id, :created_by)
239
242
  ```
240
243
 
244
+ **Setting Modifier Class Name**
245
+
246
+ If your app will track history changes to a user, Mongoid History looks for these modifiers in the ``User`` class by default. If you have named your 'user' accounts differently, you will need to add that to your Mongoid History config:
247
+
248
+ The following examples set the modifier class name using a Rails initializer:
249
+
250
+ If your app uses a class ``Author``:
251
+
252
+ ```ruby
253
+ # config/initializers/mongoid-history.rb
254
+ # initializer for mongoid-history
255
+
256
+ Mongoid::History.modifier_class_name = 'Author'
257
+ ```
258
+
259
+ Or perhaps you are namespacing to a module:
260
+
261
+ ```ruby
262
+ Mongoid::History.modifier_class_name = 'CMS::Author'
263
+ ```
264
+
241
265
  **Using an alternate changes method**
242
266
 
243
267
  Sometimes you may wish to provide an alternate method for determining which changes should be tracked. For example, if you are using embedded documents
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ begin
3
3
  Bundler.setup(:default, :development)
4
4
  rescue Bundler::BundlerError => e
5
5
  $stderr.puts e.message
6
- $stderr.puts "Run `bundle install` to install missing gems"
6
+ $stderr.puts 'Run `bundle install` to install missing gems'
7
7
  exit e.status_code
8
8
  end
9
9
 
@@ -13,11 +13,11 @@ require 'rspec/core'
13
13
  require 'rspec/core/rake_task'
14
14
  RSpec::Core::RakeTask.new(:spec) do |spec|
15
15
  spec.pattern = FileList['spec/**/*_spec.rb']
16
- spec.rspec_opts = "--color --format progress"
16
+ spec.rspec_opts = '--color --format progress'
17
17
  end
18
18
 
19
19
  require 'rubocop/rake_task'
20
- Rubocop::RakeTask.new(:rubocop)
20
+ RuboCop::RakeTask.new(:rubocop)
21
21
 
22
22
  task default: [:rubocop, :spec]
23
23
 
@@ -1,6 +1,13 @@
1
+ require 'easy_diff'
2
+
3
+ require 'mongoid/history/version'
4
+ require 'mongoid/history/mongoid'
5
+ require 'mongoid/history/tracker'
6
+ require 'mongoid/history/trackable'
7
+
1
8
  module Mongoid
2
9
  module History
3
- GLOBAL_TRACK_HISTORY_FLAG = "mongoid_history_trackable_enabled"
10
+ GLOBAL_TRACK_HISTORY_FLAG = 'mongoid_history_trackable_enabled'
4
11
 
5
12
  mattr_accessor :tracker_class_name
6
13
  mattr_accessor :trackable_class_options
@@ -23,3 +30,7 @@ module Mongoid
23
30
  end
24
31
  end
25
32
  end
33
+
34
+ Mongoid::History.modifier_class_name = 'User'
35
+ Mongoid::History.trackable_class_options = {}
36
+ Mongoid::History.current_user_method ||= :current_user
@@ -1,7 +1,7 @@
1
1
  module Mongoid
2
2
  module History
3
3
  def self.mongoid3?
4
- ::Mongoid.const_defined? :Observer # deprecated in Mongoid 4.x
4
+ Mongoid::VERSION =~ /^3\./
5
5
  end
6
6
  end
7
7
  end
@@ -74,7 +74,7 @@ module Mongoid
74
74
  # undo :from => 1, :to => 5
75
75
  # undo 4
76
76
  # undo :last => 10
77
- def undo!(modifier = nil, options_or_version = nil)
77
+ def undo(modifier = nil, options_or_version = nil)
78
78
  versions = get_versions_criteria(options_or_version).to_a
79
79
  versions.sort! { |v1, v2| v2.version <=> v1.version }
80
80
 
@@ -82,13 +82,20 @@ module Mongoid
82
82
  undo_attr = v.undo_attr(modifier)
83
83
  if Mongoid::History.mongoid3? # update_attributes! not bypassing rails 3 protected attributes
84
84
  assign_attributes(undo_attr, without_protection: true)
85
- save!
86
85
  else # assign_attributes with 'without_protection' option does not work with rails 4/mongoid 4
87
- update_attributes!(undo_attr)
86
+ self.attributes = undo_attr
88
87
  end
89
88
  end
90
89
  end
91
90
 
91
+ # undo! :from => 1, :to => 5
92
+ # undo! 4
93
+ # undo! :last => 10
94
+ def undo!(modifier = nil, options_or_version = nil)
95
+ undo(modifier, options_or_version)
96
+ save!
97
+ end
98
+
92
99
  def redo!(modifier = nil, options_or_version = nil)
93
100
  versions = get_versions_criteria(options_or_version).to_a
94
101
  versions.sort! { |v1, v2| v1.version <=> v2.version }
@@ -120,11 +127,11 @@ module Mongoid
120
127
  if options[:from] && options[:to]
121
128
  lower = options[:from] >= options[:to] ? options[:to] : options[:from]
122
129
  upper = options[:from] < options[:to] ? options[:to] : options[:from]
123
- versions = history_tracks.where(:version.in => (lower .. upper).to_a)
130
+ versions = history_tracks.where(:version.in => (lower..upper).to_a)
124
131
  elsif options[:last]
125
132
  versions = history_tracks.limit(options[:last])
126
133
  else
127
- raise "Invalid options, please specify (:from / :to) keys or :last key."
134
+ fail 'Invalid options, please specify (:from / :to) keys or :last key.'
128
135
  end
129
136
  else
130
137
  options_or_version = options_or_version.to_a if options_or_version.is_a?(Range)
@@ -138,13 +145,20 @@ module Mongoid
138
145
 
139
146
  def related_scope
140
147
  scope = history_trackable_options[:scope]
141
- scope = _parent.collection_name.to_s.singularize.to_sym if scope.is_a?(Array)
142
148
 
143
- if Mongoid::History.mongoid3?
144
- scope = metadata.inverse_class_name.tableize.singularize.to_sym if metadata.present? && scope == metadata.as
149
+ # Use top level document if its name is specified in the scope
150
+ root_document_name = traverse_association_chain.first['name'].singularize.underscore.gsub('/', '_').to_sym
151
+ if scope.is_a?(Array) && scope.include?(root_document_name)
152
+ scope = root_document_name
145
153
  else
146
- scope = relation_metadata.inverse_class_name.tableize.singularize.to_sym if relation_metadata.present? && scope == relation_metadata.as
154
+ scope = _parent.collection_name.to_s.singularize.to_sym if scope.is_a?(Array)
155
+ if Mongoid::History.mongoid3?
156
+ scope = metadata.inverse_class_name.tableize.singularize.to_sym if metadata.present? && scope == metadata.as
157
+ else
158
+ scope = relation_metadata.inverse_class_name.tableize.singularize.to_sym if relation_metadata.present? && scope == relation_metadata.as
159
+ end
147
160
  end
161
+
148
162
  scope
149
163
  end
150
164
 
@@ -308,7 +322,7 @@ module Mongoid
308
322
  #
309
323
  # @return [ Array < String > ] the list of reserved database field names
310
324
  def reserved_tracked_fields
311
- @reserved_tracked_fields ||= ["_id", history_trackable_options[:version_field].to_s, "#{history_trackable_options[:modifier_field]}_id"]
325
+ @reserved_tracked_fields ||= ['_id', history_trackable_options[:version_field].to_s, "#{history_trackable_options[:modifier_field]}_id"]
312
326
  end
313
327
 
314
328
  def history_trackable_options
@@ -140,10 +140,10 @@ module Mongoid
140
140
  # { field_1: value, field_2: value }
141
141
  def affected
142
142
  target = action.to_sym == :destroy ? :from : :to
143
- @affected ||= tracked_changes.inject(HashWithIndifferentAccess.new) { |h, (k, v)|
143
+ @affected ||= tracked_changes.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
144
144
  h[k] = v[target]
145
145
  h
146
- }
146
+ end
147
147
  end
148
148
 
149
149
  # Returns the class of the trackable, irrespective of whether the trackable object
@@ -151,7 +151,7 @@ module Mongoid
151
151
  #
152
152
  # @return [ Class ] the class of the trackable
153
153
  def trackable_parent_class
154
- association_chain.first["name"].constantize
154
+ association_chain.first['name'].constantize
155
155
  end
156
156
 
157
157
  private
@@ -166,18 +166,18 @@ module Mongoid
166
166
 
167
167
  def create_standalone
168
168
  restored = trackable_parent_class.new(localize_keys(original))
169
- restored.id = original["_id"]
169
+ restored.id = original['_id']
170
170
  restored.save!
171
171
  end
172
172
 
173
173
  def create_on_parent
174
- name = association_chain.last["name"]
174
+ name = association_chain.last['name']
175
175
  if trackable_parent.class.embeds_one?(name)
176
176
  trackable_parent.create_embedded(name, localize_keys(original))
177
177
  elsif trackable_parent.class.embeds_many?(name)
178
178
  trackable_parent.get_embedded(name).create!(localize_keys(original))
179
179
  else
180
- raise "This should never happen. Please report bug!"
180
+ fail 'This should never happen. Please report bug!'
181
181
  end
182
182
  end
183
183
 
@@ -202,7 +202,7 @@ module Mongoid
202
202
  elsif doc.class.embeds_many?(name)
203
203
  doc.get_embedded(name).unscoped.where(_id: node['id']).first
204
204
  else
205
- raise "This should never happen. Please report bug."
205
+ fail 'This should never happen. Please report bug.'
206
206
  end
207
207
  documents << doc
208
208
  break if chain.empty?
@@ -211,7 +211,7 @@ module Mongoid
211
211
  end
212
212
 
213
213
  def localize_keys(hash)
214
- klass = association_chain.first["name"].constantize
214
+ klass = association_chain.first['name'].constantize
215
215
  klass.localized_fields.keys.each do |name|
216
216
  hash["#{name}_translations"] = hash.delete(name) if hash[name].present?
217
217
  end if klass.respond_to?(:localized_fields)
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module History
3
- VERSION = '0.4.4'
3
+ VERSION = '0.4.5'
4
4
  end
5
5
  end
@@ -23,9 +23,9 @@ Gem::Specification.new do |s|
23
23
  s.add_runtime_dependency 'activesupport'
24
24
 
25
25
  s.add_development_dependency 'rake'
26
- s.add_development_dependency 'rspec', '>= 3.0'
26
+ s.add_development_dependency 'rspec', '~> 3.1'
27
27
  s.add_development_dependency 'bundler'
28
- s.add_development_dependency 'rubocop', '0.21.0'
28
+ s.add_development_dependency 'rubocop', '0.27.1'
29
29
  s.add_development_dependency 'yard'
30
30
  s.add_development_dependency 'gem-release'
31
31
  s.add_development_dependency 'coveralls'
@@ -78,13 +78,13 @@ describe Mongoid::History::Tracker do
78
78
  end
79
79
  end
80
80
 
81
- it "tracks history for nested embedded documents with polymorphic relations" do
81
+ it 'tracks history for nested embedded documents with polymorphic relations' do
82
82
  user = User.new
83
83
  user.save!
84
84
 
85
85
  real_state = user.real_states.build(name: 'rs_name')
86
86
  real_state.save!
87
- real_state.build_address(address: "Main Street #123", city: "Highland Park", state: 'IL').save!
87
+ real_state.build_address(address: 'Main Street #123', city: 'Highland Park', state: 'IL').save!
88
88
  expect(real_state.history_tracks.count).to eq(2)
89
89
  expect(real_state.address.history_tracks.count).to eq(1)
90
90
 
@@ -102,7 +102,7 @@ describe Mongoid::History::Tracker do
102
102
 
103
103
  company = user.companies.build(name: 'co_name')
104
104
  company.save!
105
- company.build_address(address: "Main Street #456", city: "Evanston", state: 'IL').save!
105
+ company.build_address(address: 'Main Street #456', city: 'Evanston', state: 'IL').save!
106
106
  expect(company.history_tracks.count).to eq(2)
107
107
  expect(company.address.history_tracks.count).to eq(1)
108
108
 
@@ -112,7 +112,7 @@ describe Mongoid::History::Tracker do
112
112
  expect(company.address.history_tracks.count).to eq(2)
113
113
  expect(company.history_tracks.last.action).to eq('update')
114
114
 
115
- company.build_second_address(address: "Main Street #789", city: "Highland Park", state: 'IL').save!
115
+ company.build_second_address(address: 'Main Street #789', city: 'Highland Park', state: 'IL').save!
116
116
  expect(company.history_tracks.count).to eq(4)
117
117
  expect(company.second_address.history_tracks.count).to eq(1)
118
118
  expect(company.history_tracks.last.action).to eq('create')