unique_identifier 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fba706996f4d605d85e6e8a9364981d7150fc6d
4
- data.tar.gz: ef8444f104bbc9eb045416441e449da3516c8a05
3
+ metadata.gz: 65d39ff883a6392b2533fa103315d13c1575fbe5
4
+ data.tar.gz: 8d3d2a2432d6a3b0a45125142fda0b7d2a140653
5
5
  SHA512:
6
- metadata.gz: 2dc1fb3c4245a81f3cfec364af9553c33fd95619a7877f47bea6e125230d3c8d193e5e311646945708d7cb67a1d1f4140c6eaa12d0441ca3f7757dcf6f833a8c
7
- data.tar.gz: c48adcce59cc91713e0baba12e7c7786ee1a7617769a1b5c181ee997d768c5f4a2bab93407980efc578eca48f8fb60816dcef956ba2b9ff058d9bcf8c9783a4d
6
+ metadata.gz: 01cca76a6c1416116e1b9919c9b8a429d7036330020ab8d0c0d91aec16b6533ee1723be7d0677d42dc7e27adf15b187cc02d360fae2f663578af0aa8f879468c
7
+ data.tar.gz: 40fe3058df5421908a9def579afd41d1f6d5388ca848051c906c7f7587668e9004d3e4e8afb161eaed7492b1fbe6799393d5bd32cb59f38112fd5fcbd48819f7
data/Changes.md ADDED
@@ -0,0 +1,6 @@
1
+ 0.0.4
2
+ --------
3
+ **Fixes**
4
+ - PR #1 - updates `before_create` call back to `before_validation, on: :create`
5
+ - fixes issue where `unique_id` attribute is validated, and `generate_unique_id` wasn't called
6
+ until after validations
data/README.md CHANGED
@@ -23,12 +23,12 @@ Or install it yourself as:
23
23
  class SomeModel < ActiveRecord::Base
24
24
 
25
25
  # name of column to store unique identifier
26
- # |
26
+ # |
27
27
  # -------
28
28
  unique_id :number, -> { Array.new(9) { rand(9) }.join }
29
29
  # ------------------------------------
30
30
  # |
31
- # proc to be run before_create to generate unique identifier
31
+ # proc to be run `before_validation, on: :create` to generate unique identifier
32
32
 
33
33
  end
34
34
 
@@ -45,7 +45,7 @@ The above example is equivalent to this:
45
45
 
46
46
  class SomeModel < ActiveRecord::Base
47
47
 
48
- before_create :generate_unique_id
48
+ before_validation :generate_unique_id, on: :create
49
49
 
50
50
  def generate_unique_id
51
51
  self.number = loop do
@@ -8,10 +8,9 @@ module UniqueIdentifier
8
8
  mattr_accessor :field, :block, :klass
9
9
 
10
10
  def unique_id(field, block)
11
- @klass = self.name.constantize
12
- @klass.const_set('BLOCK', block)
13
- @klass.const_set('FIELD', field)
14
- @klass.set_callback(:create, :before, :generate_unique_id)
11
+ const_set('BLOCK', block)
12
+ const_set('FIELD', field)
13
+ before_validation :generate_unique_id, on: :create
15
14
  end
16
15
 
17
16
  end
@@ -1,3 +1,3 @@
1
1
  module UniqueIdentifier
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -29,7 +29,7 @@ RSpec.configure do |config|
29
29
  end
30
30
 
31
31
 
32
- def build_class(name, options = {})
32
+ def build_dummy_class(unique_attr_name, unique_options = {}, class_options = {})
33
33
  # setup class and include delayed_cron
34
34
 
35
35
  class_name = "DummyModel"
@@ -42,8 +42,11 @@ def build_class(name, options = {})
42
42
 
43
43
  klass.class_eval do
44
44
 
45
- unique_id name, options
45
+ unique_id unique_attr_name, unique_options
46
46
 
47
+ if class_options.fetch(:validate, false)
48
+ validates unique_attr_name, presence: true
49
+ end
47
50
  end
48
51
 
49
52
  klass
@@ -5,17 +5,41 @@ describe UniqueIdentifier do
5
5
 
6
6
  context "callbacks" do
7
7
 
8
- before do
9
- DummyModel = build_class(
10
- :number,
11
- Proc.new { "R#{Array.new(9) { rand(9) }.join}" }
12
- )
8
+ context "when unique_id attribute +is not+ validated" do
9
+ before do
10
+ DummyModel = build_dummy_class(
11
+ :number,
12
+ Proc.new { "R#{Array.new(9) { rand(9) }.join}" }
13
+ )
14
+ end
15
+
16
+ let(:model) { DummyModel.create }
17
+
18
+ it "populates the specified field on create" do
19
+ expect(model.number).to match(/R\d{9}/)
20
+ end
13
21
  end
14
22
 
15
- let(:model) { DummyModel.create }
23
+ context "when unique_id attribute +is+ validated" do
24
+ before do
25
+ DummyModel = build_dummy_class(
26
+ :number,
27
+ Proc.new { "R#{Array.new(9) { rand(9) }.join}" },
28
+ validate: true
29
+ )
30
+ end
31
+
32
+ let(:model) { DummyModel.create }
33
+
34
+ it "populates the specified field before any validations, on create" do
35
+ expect(model.number).to match(/R\d{9}/)
36
+ end
37
+
38
+ it "creates a valid instance" do
39
+ expect(model.valid?).to be_truthy
40
+ expect(model.errors).to be_empty
41
+ end
16
42
 
17
- it "populates the specified field on create" do
18
- expect(model.number).to match(/R\d{9}/)
19
43
  end
20
44
 
21
45
  end
@@ -24,7 +48,7 @@ describe UniqueIdentifier do
24
48
 
25
49
  before do
26
50
  i = -1
27
- DummyModel = build_class(:number, Proc.new { i += 1 })
51
+ DummyModel = build_dummy_class(:number, Proc.new { i += 1 })
28
52
  class InheritedModel < DummyModel
29
53
  x = -1
30
54
  unique_id :number, Proc.new { x += 1 }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unique_identifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Grubbs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-09 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,6 +102,7 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
+ - Changes.md
105
106
  - Gemfile
106
107
  - LICENSE.txt
107
108
  - README.md
@@ -131,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  version: '0'
132
133
  requirements: []
133
134
  rubyforge_project:
134
- rubygems_version: 2.4.6
135
+ rubygems_version: 2.4.8
135
136
  signing_key:
136
137
  specification_version: 4
137
138
  summary: before_create helper to create unique idenfication fields