requires_approval_rails_2 1.0.8 → 1.0.9

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: 8a4b7bd4c275b86b2b16aa1de9dbdc8ed41a7b7e
4
- data.tar.gz: 27891b68e190c130107655d46277ed09b919a07d
3
+ metadata.gz: b19005354f9fe49d93f36d8439ab9021079f6d08
4
+ data.tar.gz: 8aec5974156e1d41db9bd61939000ebf4ef4c3e0
5
5
  SHA512:
6
- metadata.gz: 89c9161b544d25ded4c4dd9b8829e7fea8d85c7318b3110cfc0beb20b9030179091b720fd0a9b4c9ee27be8b165254db2817ed555dfe1cdf14a3eb5a632331e1
7
- data.tar.gz: 2b933bb51777f10b0cbff0f836fa282b4b7889956b7813c29f81d2ee6cb7352d36f8db4362f8aec26b378fe0e472b70eb1b85283eaa272b17a0c0a586b83e8eb
6
+ metadata.gz: c79319fea80be82f71bcf2f834330051f8aa1b4f608564ce01ac838b80d18dede30399ee12967cd39918285275ef9094eb1b0cb62b7191dd230fc216d05802fe
7
+ data.tar.gz: f6dcfa3b2f198e544c1db4ef20fd334d96bfdd1f1362960937657cc74f6dbf119a9d0e7ad3e203c0e684553bb5f65132dbd2df3f00ee42a20a4dc3aadf005744
data/Gemfile CHANGED
@@ -3,4 +3,21 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
- gemspec :name => "requires_approval"
6
+ gem "activerecord", "~> 2.3"
7
+ gem "activesupport", "~> 2.3"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "bundler"
13
+ gem "guard-rspec"
14
+ gem "guard-bundler"
15
+ gem "guard-spork"
16
+ gem "jeweler", "~> 1.8.3"
17
+ gem "mocha"
18
+ gem "rdoc"
19
+ gem "rspec"
20
+ gem "ruby-debug19", :require => "ruby-debug"
21
+ gem "sqlite3"
22
+ gem "yard"
23
+ end
data/Gemfile.lock CHANGED
@@ -1,55 +1,40 @@
1
- PATH
2
- remote: .
3
- specs:
4
- requires_approval (1.0.8)
5
- activerecord
6
- requires_approval
7
-
8
1
  GEM
9
2
  remote: http://rubygems.org/
10
3
  specs:
11
- activemodel (3.2.14)
12
- activesupport (= 3.2.14)
13
- builder (~> 3.0.0)
14
- activerecord (3.2.14)
15
- activemodel (= 3.2.14)
16
- activesupport (= 3.2.14)
17
- arel (~> 3.0.2)
18
- tzinfo (~> 0.3.29)
19
- activesupport (3.2.14)
20
- i18n (~> 0.6, >= 0.6.4)
21
- multi_json (~> 1.0)
22
- arel (3.0.2)
23
- builder (3.0.4)
4
+ activerecord (2.3.12)
5
+ activesupport (= 2.3.12)
6
+ activesupport (2.3.12)
7
+ archive-tar-minitar (0.5.2)
8
+ columnize (0.3.6)
24
9
  diff-lcs (1.1.3)
25
- ffi (1.1.5)
10
+ ffi (1.1.0)
26
11
  git (1.2.5)
27
- guard (1.3.2)
12
+ guard (1.2.3)
28
13
  listen (>= 0.4.2)
29
14
  thor (>= 0.14.6)
30
15
  guard-bundler (1.0.0)
31
16
  bundler (~> 1.0)
32
17
  guard (~> 1.1)
33
- guard-rspec (1.2.1)
18
+ guard-rspec (1.2.0)
34
19
  guard (>= 1.1)
35
20
  guard-spork (1.1.0)
36
21
  guard (>= 1.1)
37
22
  spork (>= 0.8.4)
38
- i18n (0.6.5)
39
23
  jeweler (1.8.4)
40
24
  bundler (~> 1.0)
41
25
  git (>= 1.2.5)
42
26
  rake
43
27
  rdoc
44
- json (1.7.5)
28
+ json (1.7.4)
29
+ linecache19 (0.5.12)
30
+ ruby_core_source (>= 0.1.4)
45
31
  listen (0.4.7)
46
32
  rb-fchange (~> 0.0.5)
47
33
  rb-fsevent (~> 0.9.1)
48
34
  rb-inotify (~> 0.8.8)
49
35
  metaclass (0.0.1)
50
- mocha (0.12.3)
36
+ mocha (0.12.1)
51
37
  metaclass (~> 0.0.1)
52
- multi_json (1.8.0)
53
38
  rake (0.9.2.2)
54
39
  rb-fchange (0.0.5)
55
40
  ffi
@@ -63,19 +48,30 @@ GEM
63
48
  rspec-expectations (~> 2.11.0)
64
49
  rspec-mocks (~> 2.11.0)
65
50
  rspec-core (2.11.1)
66
- rspec-expectations (2.11.2)
51
+ rspec-expectations (2.11.1)
67
52
  diff-lcs (~> 1.1.3)
68
- rspec-mocks (2.11.2)
53
+ rspec-mocks (2.11.1)
54
+ ruby-debug-base19 (0.11.25)
55
+ columnize (>= 0.3.1)
56
+ linecache19 (>= 0.5.11)
57
+ ruby_core_source (>= 0.1.4)
58
+ ruby-debug19 (0.11.6)
59
+ columnize (>= 0.3.1)
60
+ linecache19 (>= 0.5.11)
61
+ ruby-debug-base19 (>= 0.11.19)
62
+ ruby_core_source (0.1.5)
63
+ archive-tar-minitar (>= 0.5.2)
69
64
  spork (0.9.2)
70
65
  sqlite3 (1.3.6)
71
- thor (0.16.0)
72
- tzinfo (0.3.37)
66
+ thor (0.15.4)
73
67
  yard (0.8.2.1)
74
68
 
75
69
  PLATFORMS
76
70
  ruby
77
71
 
78
72
  DEPENDENCIES
73
+ activerecord (~> 2.3)
74
+ activesupport (~> 2.3)
79
75
  bundler
80
76
  guard-bundler
81
77
  guard-rspec
@@ -83,7 +79,7 @@ DEPENDENCIES
83
79
  jeweler (~> 1.8.3)
84
80
  mocha
85
81
  rdoc
86
- requires_approval!
87
82
  rspec
83
+ ruby-debug19
88
84
  sqlite3
89
85
  yard
data/Rakefile CHANGED
@@ -1,8 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- Dir.glob('lib/tasks/*.rake').each {|r| import r}
4
- require 'rake'
5
-
6
3
  require 'rubygems'
7
4
  require 'bundler'
8
5
  begin
@@ -12,12 +9,12 @@ rescue Bundler::BundlerError => e
12
9
  $stderr.puts "Run `bundle install` to install missing gems"
13
10
  exit e.status_code
14
11
  end
15
-
12
+ require 'rake'
16
13
 
17
14
  require 'jeweler'
18
15
  Jeweler::Tasks.new do |gem|
19
16
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
20
- gem.name = "requires_approval"
17
+ gem.name = "requires_approval_rails_2"
21
18
  gem.homepage = "http://github.com/LifebookerInc/requires_approval"
22
19
  gem.license = "MIT"
23
20
  gem.summary = %Q{Gem to handle versioning and things that require approval}
@@ -28,5 +25,18 @@ Jeweler::Tasks.new do |gem|
28
25
  end
29
26
  Jeweler::RubygemsDotOrgTasks.new
30
27
 
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
31
41
  require 'yard'
32
42
  YARD::Rake::YardocTask.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.8
1
+ 1.0.9
@@ -4,9 +4,8 @@ require 'errors'
4
4
 
5
5
  module RequiresApproval
6
6
 
7
- extend ActiveSupport::Concern
8
-
9
- included do |klass|
7
+
8
+ def self.included(klass)
10
9
  klass.send(:extend, ClassMethods)
11
10
  end
12
11
 
@@ -16,10 +15,10 @@ module RequiresApproval
16
15
 
17
16
  # # approve a list of attributes
18
17
  def approve_attributes(*attributes)
19
-
18
+
20
19
  return true unless self.has_pending_changes?
21
20
 
22
- # validate an normalize our attributes
21
+ # validate an normalize our attributes
23
22
  attributes = self.check_attributes_for_approval(attributes)
24
23
 
25
24
  # make sure that all attributes are provided if we have never
@@ -28,7 +27,7 @@ module RequiresApproval
28
27
 
29
28
  if fields_not_being_approved.present? && self.never_approved?
30
29
  raise PartialApprovalForNewObject.new(
31
- "You must approve #{self.fields_requiring_approval.join(", ")} " +
30
+ "You must approve #{self.fields_requiring_approval.join(", ")} " +
32
31
  "for a new #{self.class.name}"
33
32
  )
34
33
  end
@@ -37,26 +36,24 @@ module RequiresApproval
37
36
  write_attribute(attr, self.latest_unapproved_version.send(attr))
38
37
  end
39
38
 
40
- if self.save
41
- # if we have approved all requested changes, make our latest
42
- # unapproved version approved -
43
- # this is ALWAYS true for a new record even though its pending_changes
44
- # hash is forced to have values
45
- if self.is_first_version? || self.no_pending_changes?
46
- self.latest_unapproved_version.update_attribute(:is_approved, true)
47
- else
48
- # makes our latest_unapproved_version approved and
49
- # creates another unapproved version with any remaining
50
- # attributes
51
- self.create_approval_version_record
52
- end
53
-
54
- self.update_attributes(:is_frozen => false)
55
- self.reload
56
- return true
39
+ # if we have approved all requested changes, make our latest
40
+ # unapproved version approved -
41
+ # this is ALWAYS true for a new record even though its pending_changes
42
+ # hash is forced to have values
43
+ if self.is_first_version? || self.no_pending_changes?
44
+ self.latest_unapproved_version.update_attribute(:is_approved, true)
57
45
  else
58
- return false
46
+ # makes our latest_unapproved_version approved and
47
+ # creates another unapproved version with any remaining
48
+ # attributes
49
+ self.create_approval_version_record
59
50
  end
51
+
52
+ self.is_frozen = false
53
+
54
+ self.save
55
+ self.reload
56
+ true
60
57
  end
61
58
 
62
59
  def deny_attributes(*attributes)
@@ -72,33 +69,20 @@ module RequiresApproval
72
69
  true
73
70
  end
74
71
 
75
- # if we have denied all changes, remove the record and
76
- # make sure it isn't frozen
72
+ # if we have denied all changes, remove the record
77
73
  unless self.has_pending_changes?
78
74
  self.latest_unapproved_version.destroy
79
- self.update_attribute(:is_frozen, false)
80
75
  else
81
76
  self.latest_unapproved_version.save
82
77
  end
83
-
78
+
84
79
  self.reload
85
80
  true
86
81
  end
87
82
 
88
- def reload(*args)
89
- if instance_variable_defined?(:@has_approved_version)
90
- remove_instance_variable(:@has_approved_version)
91
- end
92
- super(*args)
93
- end
94
-
95
83
  # have any of our versions ever been approved?
96
84
  def has_approved_version?
97
- unless instance_variable_defined?(:@has_approved_version)
98
- @has_approved_version = (self.versions.where(:is_approved => true).count > 0)
99
- end
100
-
101
- @has_approved_version
85
+ self.versions.count(:conditions => {:is_approved => true}) > 0
102
86
  end
103
87
 
104
88
  # have we already approved all outstanding changes?
@@ -119,21 +103,21 @@ module RequiresApproval
119
103
  # the changes users have requested since the last approval
120
104
  def pending_changes
121
105
  return {} if self.latest_unapproved_version.blank?
122
-
106
+
123
107
  ret = {}
124
108
  # check each field requiring approval
125
109
  self.fields_requiring_approval.each do |field|
126
-
110
+
127
111
  # if it is the same in the unapproved as in the parent table
128
112
  # we skip it
129
- if self.is_first_version? ||
113
+ if self.is_first_version? ||
130
114
  self.send(field) != self.latest_unapproved_version.send(field)
131
-
115
+
132
116
  # otherwise we get the change set
133
117
  ret[field] = {
134
- # our first version is always nil, regardless of the
118
+ # our first version is always nil, regardless of the
135
119
  # defaults in that table
136
- "was" => self.is_first_version? ? nil : self.send(field),
120
+ "was" => self.is_first_version? ? nil : self.send(field),
137
121
  "became" => self.latest_unapproved_version.send(field)
138
122
  }
139
123
  end
@@ -171,7 +155,7 @@ module RequiresApproval
171
155
  self.latest_unapproved_version.update_attributes(
172
156
  self.attributes_requiring_approval.merge(:is_approved => true)
173
157
  )
174
- # reload so this unapproved version is out of our cache and will not
158
+ # reload so this unapproved version is out of our cache and will not
175
159
  # get its foreign key unassigned
176
160
  self.latest_unapproved_version(true)
177
161
 
@@ -192,7 +176,7 @@ module RequiresApproval
192
176
  !self.has_approved_version?
193
177
  end
194
178
 
195
- # ActiveRecord-style attribute hash for the
179
+ # ActiveRecord-style attribute hash for the
196
180
  # requested changes
197
181
  def pending_attributes
198
182
  ret = {}
@@ -220,7 +204,7 @@ module RequiresApproval
220
204
  # adds our versions table
221
205
  self.drop_versions_table
222
206
  self.create_versions_table
223
-
207
+
224
208
  end
225
209
 
226
210
  def requires_approval_for(*attrs)
@@ -235,14 +219,13 @@ module RequiresApproval
235
219
 
236
220
  # create a blank version before create to handle if no
237
221
  # attributes were ever set
238
- self.before_validation(
239
- :latest_unapproved_version_with_nil_check,
240
- :on => :create
222
+ self.before_validation_on_create(
223
+ :latest_unapproved_version_with_nil_check
241
224
  )
242
-
225
+
243
226
  # create the versions class
244
227
  self.create_versions_class
245
- self.has_many :versions,
228
+ self.has_many :versions,
246
229
  :class_name => self.versions_class.name,
247
230
  :foreign_key => self.versions_foreign_key
248
231
 
@@ -253,11 +236,10 @@ module RequiresApproval
253
236
  :conditions => [
254
237
  "#{self.versions_table_name}.is_approved = ?", false
255
238
  ]
256
- end
257
239
 
258
- def unapproved
259
- includes(:latest_unapproved_version)
260
- .where("#{self.versions_table_name}.id IS NOT NULL")
240
+ self.set_up_scopes
241
+
242
+
261
243
  end
262
244
 
263
245
  # the class which our versions are
@@ -286,9 +268,9 @@ module RequiresApproval
286
268
  # create a class
287
269
  def create_versions_class
288
270
  versions_table_name = self.versions_table_name
289
-
271
+
290
272
  self.const_set self.versions_class_name, Class.new(ActiveRecord::Base)
291
-
273
+
292
274
  self.versions_class.class_eval do
293
275
  self.table_name = versions_table_name
294
276
  end
@@ -327,18 +309,27 @@ module RequiresApproval
327
309
  @versions_table_name = opts.delete(:versions_table_name)
328
310
  end
329
311
 
312
+ def set_up_scopes
313
+ self.named_scope(:unapproved, {
314
+ :include => [:latest_unapproved_version],
315
+ :conditions => [
316
+ "#{self.versions_table_name}.id IS NOT NULL"
317
+ ]
318
+ })
319
+ end
320
+
330
321
  def set_up_version_delegates
331
322
  self.fields_requiring_approval.each do |f|
332
323
  define_method("#{f}=") do |val|
333
324
  # type cast our val so "0" changes to 'false'
334
325
  type_casted_val = self.column_for_attribute(f).type_cast(val)
335
-
326
+
336
327
  # if we have a latest_unapproved version already, let it handle
337
- # updates - if not, only create one if the type casted value is
328
+ # updates - if not, only create one if the type casted value is
338
329
  # not the same as what is in the parent value
339
- if self.latest_unapproved_version.present? ||
330
+ if self.latest_unapproved_version.present? ||
340
331
  type_casted_val != self.send(f)
341
-
332
+
342
333
  self.send("#{f}_will_change!")
343
334
  self.latest_unapproved_version_with_nil_check.send("#{f}=", val)
344
335
  end
@@ -346,12 +337,6 @@ module RequiresApproval
346
337
  end
347
338
  end
348
339
 
349
- def validates_approved_field(*args)
350
- self.versions_class.class_eval do
351
- validates(*args)
352
- end
353
- end
354
-
355
340
  # class name for our versions
356
341
  def versions_class_name
357
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.8"
8
+ s.version = "1.0.9"
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-05"
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 = [
@@ -235,12 +235,12 @@ describe RequiresApproval do
235
235
  user.reload
236
236
 
237
237
  user.latest_unapproved_version.should be nil
238
-
239
238
  user.update_attributes(
240
239
  :first_name => user.first_name,
241
240
  :last_name => user.last_name
242
241
  )
243
242
  user.reload
243
+
244
244
  user.latest_unapproved_version.should be nil
245
245
 
246
246
 
@@ -266,12 +266,13 @@ describe RequiresApproval do
266
266
 
267
267
  end
268
268
 
269
- context ".validates_approved_field" do
269
+ context "validation" do
270
270
 
271
271
  it "should delegate to the requires_approval field" do
272
272
  User.class_eval do
273
- validates_approved_field :first_name,
274
- :presence => true
273
+ self.versions_class.class_eval do
274
+ validates_presence_of :first_name
275
+ end
275
276
  end
276
277
 
277
278
  user = User.new
@@ -332,7 +333,8 @@ describe RequiresApproval do
332
333
  })
333
334
 
334
335
  # should create an approved version
335
- user.versions.where(:is_approved => true).count.should be > 0
336
+ count = user.versions.count(:conditions => {:is_approved => true})
337
+ count.should be > 0
336
338
 
337
339
  end
338
340
 
@@ -385,19 +387,6 @@ describe RequiresApproval do
385
387
 
386
388
  end
387
389
 
388
- it "should not approve the latest unapproved version if it is invalid" do
389
-
390
- user = User.new(
391
- :first_name => "Dan",
392
- :last_name => "Langevin",
393
- :birthday => Date.today
394
- )
395
-
396
- user.stubs(:save => false)
397
- user.approve_all_attributes.should eql false
398
-
399
- end
400
-
401
390
  end
402
391
 
403
392
  context "#deny_attributes" do
@@ -475,23 +464,6 @@ describe RequiresApproval do
475
464
  )
476
465
  end
477
466
 
478
- it "should set is_frozen to false when denying all attributes" do
479
-
480
- u = User.create(
481
- :first_name => "Dan",
482
- :last_name => "Langevin",
483
- :birthday => Date.today
484
- )
485
- u.approve_all_attributes
486
-
487
- u.update_attributes(:first_name => "Changed!", :is_frozen => true)
488
- u.is_frozen?.should eql(true)
489
-
490
- u.deny_attributes(:first_name)
491
- u.is_frozen?.should eql(false)
492
-
493
- end
494
-
495
467
  end
496
468
 
497
469
  context "#has_approved_version?" do
data/spec/spec_helper.rb CHANGED
@@ -13,6 +13,9 @@ Spork.prefork do
13
13
  require 'rspec'
14
14
  require 'requires_approval'
15
15
 
16
+ require 'ruby-debug'
17
+ Debugger.start
18
+
16
19
  # Requires supporting files with custom matchers and macros, etc,
17
20
  # in ./support/ and its subdirectories.
18
21
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requires_approval_rails_2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Langevin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2012-08-05 00:00:00.000000000 Z
11
+ date: 2012-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord