requires_approval_rails_2 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,8 +3,8 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
- gem "activerecord", "~> 2.3"
7
- gem "activesupport", "~> 2.3"
6
+ gem "activerecord", "~> 3"
7
+ gem "activesupport", "~> 3"
8
8
 
9
9
  # Add dependencies to develop your gem here.
10
10
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -1,31 +1,42 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activerecord (2.3.12)
5
- activesupport (= 2.3.12)
6
- activesupport (2.3.12)
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.8)
8
+ activemodel (= 3.2.8)
9
+ activesupport (= 3.2.8)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.8)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
7
15
  archive-tar-minitar (0.5.2)
16
+ arel (3.0.2)
17
+ builder (3.0.0)
8
18
  columnize (0.3.6)
9
19
  diff-lcs (1.1.3)
10
- ffi (1.1.0)
20
+ ffi (1.1.5)
11
21
  git (1.2.5)
12
- guard (1.2.3)
22
+ guard (1.3.2)
13
23
  listen (>= 0.4.2)
14
24
  thor (>= 0.14.6)
15
25
  guard-bundler (1.0.0)
16
26
  bundler (~> 1.0)
17
27
  guard (~> 1.1)
18
- guard-rspec (1.2.0)
28
+ guard-rspec (1.2.1)
19
29
  guard (>= 1.1)
20
30
  guard-spork (1.1.0)
21
31
  guard (>= 1.1)
22
32
  spork (>= 0.8.4)
33
+ i18n (0.6.0)
23
34
  jeweler (1.8.4)
24
35
  bundler (~> 1.0)
25
36
  git (>= 1.2.5)
26
37
  rake
27
38
  rdoc
28
- json (1.7.4)
39
+ json (1.7.5)
29
40
  linecache19 (0.5.12)
30
41
  ruby_core_source (>= 0.1.4)
31
42
  listen (0.4.7)
@@ -33,8 +44,9 @@ GEM
33
44
  rb-fsevent (~> 0.9.1)
34
45
  rb-inotify (~> 0.8.8)
35
46
  metaclass (0.0.1)
36
- mocha (0.12.1)
47
+ mocha (0.12.3)
37
48
  metaclass (~> 0.0.1)
49
+ multi_json (1.3.6)
38
50
  rake (0.9.2.2)
39
51
  rb-fchange (0.0.5)
40
52
  ffi
@@ -48,9 +60,9 @@ GEM
48
60
  rspec-expectations (~> 2.11.0)
49
61
  rspec-mocks (~> 2.11.0)
50
62
  rspec-core (2.11.1)
51
- rspec-expectations (2.11.1)
63
+ rspec-expectations (2.11.2)
52
64
  diff-lcs (~> 1.1.3)
53
- rspec-mocks (2.11.1)
65
+ rspec-mocks (2.11.2)
54
66
  ruby-debug-base19 (0.11.25)
55
67
  columnize (>= 0.3.1)
56
68
  linecache19 (>= 0.5.11)
@@ -63,15 +75,16 @@ GEM
63
75
  archive-tar-minitar (>= 0.5.2)
64
76
  spork (0.9.2)
65
77
  sqlite3 (1.3.6)
66
- thor (0.15.4)
78
+ thor (0.16.0)
79
+ tzinfo (0.3.33)
67
80
  yard (0.8.2.1)
68
81
 
69
82
  PLATFORMS
70
83
  ruby
71
84
 
72
85
  DEPENDENCIES
73
- activerecord (~> 2.3)
74
- activesupport (~> 2.3)
86
+ activerecord (~> 3)
87
+ activesupport (~> 3)
75
88
  bundler
76
89
  guard-bundler
77
90
  guard-rspec
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ require 'rake'
14
14
  require 'jeweler'
15
15
  Jeweler::Tasks.new do |gem|
16
16
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "requires_approval_rails_2"
17
+ gem.name = "requires_approval"
18
18
  gem.homepage = "http://github.com/LifebookerInc/requires_approval"
19
19
  gem.license = "MIT"
20
20
  gem.summary = %Q{Gem to handle versioning and things that require approval}
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.5
@@ -4,8 +4,9 @@ require 'errors'
4
4
 
5
5
  module RequiresApproval
6
6
 
7
-
8
- def self.included(klass)
7
+ extend ActiveSupport::Concern
8
+
9
+ included do |klass|
9
10
  klass.send(:extend, ClassMethods)
10
11
  end
11
12
 
@@ -16,6 +17,8 @@ module RequiresApproval
16
17
  # # approve a list of attributes
17
18
  def approve_attributes(*attributes)
18
19
 
20
+ return true unless self.has_pending_changes?
21
+
19
22
  # validate an normalize our attributes
20
23
  attributes = self.check_attributes_for_approval(attributes)
21
24
 
@@ -35,8 +38,10 @@ module RequiresApproval
35
38
  end
36
39
 
37
40
  # if we have approved all requested changes, make our latest
38
- # unapproved version approved
39
- unless self.has_pending_changes?
41
+ # unapproved version approved -
42
+ # this is ALWAYS true for a new record even though its pending_changes
43
+ # hash is forced to have values
44
+ if self.is_first_version? || self.no_pending_changes?
40
45
  self.latest_unapproved_version.update_attribute(:is_approved, true)
41
46
  else
42
47
  # makes our latest_unapproved_version approved and
@@ -78,7 +83,7 @@ module RequiresApproval
78
83
 
79
84
  # have any of our versions ever been approved?
80
85
  def has_approved_version?
81
- self.versions.count(:conditions => {:is_approved => true}) > 0
86
+ self.versions.where(:is_approved => true).count > 0
82
87
  end
83
88
 
84
89
  # have we already approved all outstanding changes?
@@ -86,6 +91,16 @@ module RequiresApproval
86
91
  self.pending_changes.present?
87
92
  end
88
93
 
94
+ # are we the first version?
95
+ def is_first_version?
96
+ !self.has_approved_version?
97
+ end
98
+
99
+ # returns true if there are no changes to approve
100
+ def no_pending_changes?
101
+ !self.has_pending_changes?
102
+ end
103
+
89
104
  # the changes users have requested since the last approval
90
105
  def pending_changes
91
106
  return {} if self.latest_unapproved_version.blank?
@@ -93,12 +108,17 @@ module RequiresApproval
93
108
  ret = {}
94
109
  # check each field requiring approval
95
110
  self.fields_requiring_approval.each do |field|
111
+
96
112
  # if it is the same in the unapproved as in the parent table
97
113
  # we skip it
98
- unless self.send(field) == self.latest_unapproved_version.send(field)
114
+ if self.is_first_version? ||
115
+ self.send(field) != self.latest_unapproved_version.send(field)
116
+
99
117
  # otherwise we get the change set
100
118
  ret[field] = {
101
- "was" => self.send(field),
119
+ # our first version is always nil, regardless of the
120
+ # defaults in that table
121
+ "was" => self.is_first_version? ? nil : self.send(field),
102
122
  "became" => self.latest_unapproved_version.send(field)
103
123
  }
104
124
  end
@@ -218,10 +238,11 @@ module RequiresApproval
218
238
  :conditions => [
219
239
  "#{self.versions_table_name}.is_approved = ?", false
220
240
  ]
241
+ end
221
242
 
222
- self.set_up_scopes
223
-
224
-
243
+ def unapproved
244
+ includes(:latest_unapproved_version)
245
+ .where("#{self.versions_table_name}.id IS NOT NULL")
225
246
  end
226
247
 
227
248
  # the class which our versions are
@@ -291,24 +312,31 @@ module RequiresApproval
291
312
  @versions_table_name = opts.delete(:versions_table_name)
292
313
  end
293
314
 
294
- def set_up_scopes
295
- self.named_scope(:unapproved, {
296
- :include => [:latest_unapproved_version],
297
- :conditions => [
298
- "#{self.versions_table_name}.id IS NOT NULL"
299
- ]
300
- })
301
- end
302
-
303
315
  def set_up_version_delegates
304
316
  self.fields_requiring_approval.each do |f|
305
317
  define_method("#{f}=") do |val|
306
- self.send("#{f}_will_change!")
307
- self.latest_unapproved_version_with_nil_check.send("#{f}=", val)
318
+ # type cast our val so "0" changes to 'false'
319
+ type_casted_val = self.column_for_attribute(f).type_cast(val)
320
+
321
+ # if we have a latest_unapproved version already, let it handle
322
+ # updates - if not, only create one if the type casted value is
323
+ # not the same as what is in the parent value
324
+ if self.latest_unapproved_version.present? ||
325
+ type_casted_val != self.send(f)
326
+
327
+ self.send("#{f}_will_change!")
328
+ self.latest_unapproved_version_with_nil_check.send("#{f}=", val)
329
+ end
308
330
  end
309
331
  end
310
332
  end
311
333
 
334
+ def validates_approved_field(*args)
335
+ self.versions_class.class_eval do
336
+ validates(*args)
337
+ end
338
+ end
339
+
312
340
  # class name for our versions
313
341
  def versions_class_name
314
342
  @versions_class_name ||= "Version"
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "requires_approval_rails_2"
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dan Langevin"]
12
- s.date = "2012-08-06"
12
+ s.date = "2012-10-12"
13
13
  s.description = "Gem to handle versioning and things that require approval"
14
14
  s.email = "dan.langevin@lifebooker.com"
15
15
  s.extra_rdoc_files = [
@@ -13,17 +13,44 @@ describe RequiresApproval do
13
13
  t.string(:first_name)
14
14
  t.string(:last_name)
15
15
  t.date(:birthday)
16
+ t.boolean(:is_alive, :default => true)
16
17
  t.timestamps
17
18
  end
18
19
 
19
20
  class User < ActiveRecord::Base
20
- requires_approval_for(:first_name, :last_name)
21
+ requires_approval_for(:first_name, :last_name, :is_alive)
21
22
  end
22
23
 
23
24
  User.prepare_tables_for_requires_approval
24
25
 
25
26
  end
26
27
 
28
+ context "#pending_changes" do
29
+
30
+ it "gives back all attributes when it is a new record" do
31
+ user = User.create(
32
+ :first_name => "Dan",
33
+ :last_name => "Test",
34
+ :is_alive => true
35
+ )
36
+ user.pending_changes.should eql({
37
+ "first_name" => {
38
+ "was" => nil,
39
+ "became" => "Dan"
40
+ },
41
+ "last_name" => {
42
+ "was" => nil,
43
+ "became" => "Test"
44
+ },
45
+ "is_alive" => {
46
+ "was" => nil,
47
+ "became" => true
48
+ }
49
+ })
50
+ end
51
+
52
+ end
53
+
27
54
 
28
55
  context "setup" do
29
56
 
@@ -197,15 +224,54 @@ describe RequiresApproval do
197
224
 
198
225
  end
199
226
 
227
+ it "should not create new versions when attributes are not actually
228
+ updated" do
229
+
230
+ user = User.create(
231
+ :first_name => "Dan",
232
+ :last_name => "Langevin"
233
+ )
234
+ user.approve_all_attributes
235
+ user.reload
236
+
237
+ user.latest_unapproved_version.should be nil
238
+
239
+ user.update_attributes(
240
+ :first_name => user.first_name,
241
+ :last_name => user.last_name
242
+ )
243
+ user.reload
244
+ user.latest_unapproved_version.should be nil
245
+
246
+
247
+ end
248
+
249
+ it "should allow you to modify attributes after an unapproved version
250
+ has been created" do
251
+
252
+ user = User.create(
253
+ :first_name => "Dan",
254
+ :last_name => "Langevin"
255
+ )
256
+ user.approve_all_attributes
257
+
258
+ user.update_attributes(:first_name => "X")
259
+ user.update_attributes(:first_name => "Dan")
260
+
261
+ user.approve_all_attributes
262
+ user.reload.first_name.should eql("Dan")
263
+
264
+ end
265
+
266
+
200
267
  end
201
268
 
202
- context "validation" do
269
+ context ".validates_approved_field" do
203
270
 
204
271
  it "should delegate to the requires_approval field" do
205
272
  User.class_eval do
206
- self.versions_class.class_eval do
207
- validates_presence_of :first_name
208
- end
273
+ validates_approved_field :first_name,
274
+ :presence => true
209
275
  end
210
276
 
211
277
  user = User.new
@@ -266,8 +332,7 @@ describe RequiresApproval do
266
332
  })
267
333
 
268
334
  # should create an approved version
269
- count = user.versions.count(:conditions => {:is_approved => true})
270
- count.should be > 0
335
+ user.versions.where(:is_approved => true).count.should be > 0
271
336
 
272
337
  end
273
338
 
@@ -279,7 +344,7 @@ describe RequiresApproval do
279
344
  :last_name => "Langevin",
280
345
  :birthday => Date.today
281
346
  )
282
- user.approve_attributes(:first_name, :last_name)
347
+ user.approve_attributes(:first_name, :last_name, :is_alive)
283
348
  user.update_attributes({
284
349
  :first_name => "New Name",
285
350
  :last_name => "New Last Name"
@@ -306,6 +371,20 @@ describe RequiresApproval do
306
371
 
307
372
  end
308
373
 
374
+ it "should return true if you approve an already approved
375
+ record" do
376
+
377
+ user = User.create(
378
+ :first_name => "Dan",
379
+ :last_name => "Langevin",
380
+ :birthday => Date.today
381
+ )
382
+ user.approve_all_attributes
383
+
384
+ user.approve_all_attributes.should be true
385
+
386
+ end
387
+
309
388
  end
310
389
 
311
390
  context "#deny_attributes" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requires_approval_rails_2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2164816540 !ruby/object:Gem::Requirement
16
+ requirement: &2163753200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2164816540
24
+ version_requirements: *2163753200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2164815680 !ruby/object:Gem::Requirement
27
+ requirement: &2163752640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2164815680
35
+ version_requirements: *2163752640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2164814860 !ruby/object:Gem::Requirement
38
+ requirement: &2163751920 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2164814860
46
+ version_requirements: *2163751920
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-rspec
49
- requirement: &2164813820 !ruby/object:Gem::Requirement
49
+ requirement: &2163751300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2164813820
57
+ version_requirements: *2163751300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard-bundler
60
- requirement: &2164812360 !ruby/object:Gem::Requirement
60
+ requirement: &2163750660 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2164812360
68
+ version_requirements: *2163750660
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-spork
71
- requirement: &2164809020 !ruby/object:Gem::Requirement
71
+ requirement: &2163750040 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2164809020
79
+ version_requirements: *2163750040
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &2164804700 !ruby/object:Gem::Requirement
82
+ requirement: &2163749420 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.8.3
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2164804700
90
+ version_requirements: *2163749420
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: mocha
93
- requirement: &2164802980 !ruby/object:Gem::Requirement
93
+ requirement: &2163748740 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2164802980
101
+ version_requirements: *2163748740
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rdoc
104
- requirement: &2164802080 !ruby/object:Gem::Requirement
104
+ requirement: &2163748060 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2164802080
112
+ version_requirements: *2163748060
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rspec
115
- requirement: &2164801020 !ruby/object:Gem::Requirement
115
+ requirement: &2163747460 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *2164801020
123
+ version_requirements: *2163747460
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: ruby-debug19
126
- requirement: &2164800460 !ruby/object:Gem::Requirement
126
+ requirement: &2163746760 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *2164800460
134
+ version_requirements: *2163746760
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: sqlite3
137
- requirement: &2164799940 !ruby/object:Gem::Requirement
137
+ requirement: &2163746140 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *2164799940
145
+ version_requirements: *2163746140
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: yard
148
- requirement: &2164799380 !ruby/object:Gem::Requirement
148
+ requirement: &2163745620 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,7 +153,7 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *2164799380
156
+ version_requirements: *2163745620
157
157
  description: Gem to handle versioning and things that require approval
158
158
  email: dan.langevin@lifebooker.com
159
159
  executables: []
@@ -190,6 +190,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
190
  - - ! '>='
191
191
  - !ruby/object:Gem::Version
192
192
  version: '0'
193
+ segments:
194
+ - 0
195
+ hash: -2588660493545333348
193
196
  required_rubygems_version: !ruby/object:Gem::Requirement
194
197
  none: false
195
198
  requirements:
@@ -203,4 +206,3 @@ signing_key:
203
206
  specification_version: 3
204
207
  summary: Gem to handle versioning and things that require approval
205
208
  test_files: []
206
- has_rdoc: