uuid_parameter 0.1.0 → 0.2.0.pre.alpha.1

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
  SHA256:
3
- metadata.gz: '090d5a9e80813acdeed189b4946249c92072ef14b9519c253d60710c86405132'
4
- data.tar.gz: 55aefd1612e65362d133972bc56271ddab529ef4739cddf359898a1c7640bbf0
3
+ metadata.gz: f7718b5d84a19a25b9ce316d23919ef34a13a31a50aa46993578f18f59a81eed
4
+ data.tar.gz: d7be3d70a50ecda0d8fe7db471ab36f948618b56f09acba4af076e2f6da62f15
5
5
  SHA512:
6
- metadata.gz: 4c8972c83b056bfa1b024949ecbef4957b1e4e9314447995f2455306ca64f59850f75dc3798accde471530281e8dd2a6607d6a85b4076a0803af862740530069
7
- data.tar.gz: '079c228088fc5d79467457cbbe0c444178e85765210f3ff372459958b59b93e0a45f1861e11f3d03490fbd2f4b4398e206678bd5ca4b6037f533b7981b4fe99b'
6
+ metadata.gz: 7906c3a3529293eabb7c51a0c6bc97e4d6e2eb8a81ab19f3d717e2c67fb2caec9fbb38c8e5cfd9ca05d56ddfd73c02352338f7618995e8de7adfc32faafa8279
7
+ data.tar.gz: 28ae550b9f0288cdaf292045f1c4f924167a3d2108dc5babdb03e5ba3db0b70590920feb8e875da937abe5a95e4fca127955d0081c3159b4a0313ad114141e15
data/README.md CHANGED
@@ -11,7 +11,8 @@ Models including the `UUIDParameter` module will:
11
11
 
12
12
  ### Features
13
13
 
14
- - Can be used with existing models (simply add a `uuid` column).
14
+ - Can be used with existing models (simply add a `uuid` column): saving the
15
+ model with `nil` values will provide a new UUID.
15
16
  - Does not affect existing primary key.
16
17
  - Can accept any valid random UUID (version 4) provided externally.
17
18
  - Automatically generates a UUID on `:create` if one is not set.
@@ -20,6 +21,17 @@ Models including the `UUIDParameter` module will:
20
21
  - Silently ignores any attempt at changing a set UUID.
21
22
  - Overrides `:to_param` to provide the UUID instead of the primary key.
22
23
 
24
+ ### Anti-Features
25
+
26
+ - If the database already contains invalid UUIDv4 data, the affected records
27
+ will become **impossible to save**: this is to ensure that you can check your
28
+ referential integrity. You can still force `#reset_uuid!` to bypass this, or
29
+ use SQL directly in the database. (See #1)
30
+
31
+ ### I18n
32
+
33
+ Translations are in progress (See #2): specs are therefore failing (on purpose).
34
+
23
35
  ## Usage
24
36
 
25
37
  Add a `uuid` column to your model if it does not have one already:
@@ -72,6 +84,8 @@ $ gem install uuid_parameter
72
84
  Bug reports and pull requests are welcome on Gitlab at
73
85
  https://gitlab.com/incommon.cc/uuid_parameter.
74
86
 
87
+ See [ChangeLog](../CHANGELOG.md) and commit messages.
88
+
75
89
  The [Github repository] is a mirror to facilitate integration with other Rails
76
90
  development, but I don't like Microsoft, and never will. They may show the face
77
91
  they like, they come from enemity and, as far as I'm concerned, will remain
data/lib/locale/en.yml ADDED
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ not_a_uuid_v4: "must be a random UUID (v4)"
data/lib/locale/fr.yml ADDED
@@ -0,0 +1,4 @@
1
+ fr:
2
+ errors:
3
+ messages:
4
+ not_a_uuid_v4: "doit être un UUID aléatoire (v4)"
@@ -1,4 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module UUIDParameter
2
4
  class Railtie < ::Rails::Railtie
5
+ config.uuid_parameter = ActiveSupport::OrderedOptions.new
6
+
7
+ initializer 'uuid_parameter.set_locales' do |app|
8
+ app.config.i18n.load_paths << File.expand_path('../locale', __DIR__)
9
+ app.config.i18n.available_locales = [:en, :fr].freeze
10
+ end
3
11
  end
4
12
  end
@@ -1,3 +1,3 @@
1
1
  module UUIDParameter
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0-alpha.1'
3
3
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uuid_parameter/uuid_version4_validator'
3
+ # TODO: move this to the right place... But where?
4
+ I18n.load_path << File.expand_path("locale/en.yml", __dir__)
4
5
 
5
6
  # == UUIDParameter
6
7
  #
@@ -10,13 +11,37 @@ require 'uuid_parameter/uuid_version4_validator'
10
11
  # including the UUIDParameter module will use their :uuid rather than their :id
11
12
  # for URLs.
12
13
  #
14
+ # class User < ActiveRecord::Base
15
+ # include UUIDParameter
16
+ # end
17
+ #
18
+ # user = User.new
19
+ # user.uuid # => nil
20
+ # user.save # => true
21
+ # user.uuid # => some UUID was generated
22
+ # user.uuid = 'foo'
23
+ # user.save # => true
24
+ # user.uuid # => original UUID is preserved
25
+ #
26
+ # user = User.create(uuid: '8bb96d58-2efd-45df-833b-119971a19fea')
27
+ # user.update_attribute(uuid: '00000000-aaaa-45df-cccc-119971a19fea') # => true
28
+ # user.reload.uuid # => '8bb96d58-2efd-45df-833b-119971a19fea' (unchanged)
29
+ # user.to_param # => '8bb96d58-2efd-45df-833b-119971a19fea'
30
+ #
13
31
  module UUIDParameter
14
32
  extend ActiveSupport::Concern
33
+
34
+ # Note the static '4' in the third group: that's the UUID version.
35
+ UUID_V4_REGEX = %r[\A[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}\z]
36
+
15
37
  included do
16
- validates_with UUIDVersion4Validator
38
+ validates :uuid,
39
+ presence: true,
40
+ uniqueness: true,
41
+ format: { with: UUID_V4_REGEX, message: :not_a_uuid_v4 }
17
42
 
18
- before_validation :assign_uuid, on: :create
19
- before_save :keep_existing_uuid
43
+ before_validation :assign_uuid
44
+ before_save :recover_uuid
20
45
 
21
46
  def to_param
22
47
  uuid.to_s
@@ -28,12 +53,18 @@ module UUIDParameter
28
53
  self.uuid ||= SecureRandom.uuid
29
54
  end
30
55
 
31
- def keep_existing_uuid
56
+ def existing_uuid_changed?
57
+ !new_record? && !uuid_was.nil? && uuid_changed?
58
+ end
59
+
60
+ def recover_uuid
32
61
  self.uuid = uuid_was if existing_uuid_changed?
62
+ reset_uuid! unless UUID_V4_REGEX.match?(self.uuid)
33
63
  end
34
64
 
35
- def existing_uuid_changed?
36
- !new_record? && uuid_changed?
65
+ def reset_uuid!
66
+ self.uuid = nil
67
+ assign_uuid
37
68
  end
38
69
  end
39
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid_parameter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0.pre.alpha.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hellekin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-18 00:00:00.000000000 Z
11
+ date: 2018-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -76,9 +76,10 @@ files:
76
76
  - LICENSE
77
77
  - README.md
78
78
  - Rakefile
79
+ - lib/locale/en.yml
80
+ - lib/locale/fr.yml
79
81
  - lib/uuid_parameter.rb
80
82
  - lib/uuid_parameter/railtie.rb
81
- - lib/uuid_parameter/uuid_version4_validator.rb
82
83
  - lib/uuid_parameter/version.rb
83
84
  homepage: https://gitlab.com/incommon.cc/uuid_parameter
84
85
  licenses:
@@ -95,9 +96,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
96
  version: '0'
96
97
  required_rubygems_version: !ruby/object:Gem::Requirement
97
98
  requirements:
98
- - - ">="
99
+ - - ">"
99
100
  - !ruby/object:Gem::Version
100
- version: '0'
101
+ version: 1.3.1
101
102
  requirements: []
102
103
  rubyforge_project:
103
104
  rubygems_version: 2.7.6
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UUIDParameter
4
- # Validate a UUID v4: it must be correctly formatted and cannot change.
5
- # This latter responsibility is defined in the UUIDParameter model concern.
6
- class UUIDVersion4Validator < ActiveModel::Validator
7
- # Note the static '4' in the third group: that's the UUID version.
8
- UUID_V4_REGEX = %r[\A[a-f0-9]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}\z]
9
-
10
- def validate(record)
11
- unless UUID_V4_REGEX.match?(record.uuid)
12
- record.errors.add(:uuid, 'must be a valid random UUID (v4)')
13
- end
14
- end
15
- end
16
- end