uuid_parameter 0.2.0.pre.alpha.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locale/en.yml +18 -0
- data/{lib → config}/locale/fr.yml +0 -0
- data/lib/uuid_parameter.rb +3 -40
- data/lib/uuid_parameter/#error.rb# +6 -0
- data/lib/uuid_parameter/concern.rb +50 -0
- data/lib/uuid_parameter/railtie.rb +23 -4
- data/lib/uuid_parameter/version.rb +1 -1
- metadata +8 -6
- data/lib/locale/en.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 276b01d703d6a657be6b878402fd58c598a7f297442c6976b5b124fa206f15e4
|
4
|
+
data.tar.gz: 7ef3b621857adfe47db0f06673dadc5866f531cfce25c3aba60aabad389d08c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc73c09cdb1b543f95fdc487b6b969d4edd5875cf58bd072e22a1f9e3348a7029ec95007e8bcea799925d44ef7e2bf42556c2b467611c86d6b01b1052d533848
|
7
|
+
data.tar.gz: 84fcc1bd03aead0c609cde7d90e8aa8bcdd302fa38416ce71834c9c22824b0769a9e6ce7dd5b13d4dbc7cea8846dcba8af5bbef641ed7db93392769fd4560fe7
|
@@ -0,0 +1,18 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
not_a_uuid_v4: "must be a random UUID (v4)"
|
5
|
+
activerecord:
|
6
|
+
errors:
|
7
|
+
models:
|
8
|
+
attributes:
|
9
|
+
uuid:
|
10
|
+
not_a_uuid_v4: 'not a UUID v4'
|
11
|
+
attributes:
|
12
|
+
user:
|
13
|
+
uuid: 'UUIDv4'
|
14
|
+
errors:
|
15
|
+
user:
|
16
|
+
attributes:
|
17
|
+
uuid:
|
18
|
+
not_a_uuid_v4: 'LAAAAAAAAAAAAAAAAAA in uuid_parameter/locale'
|
File without changes
|
data/lib/uuid_parameter.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# TODO: move this to the right place... But where?
|
4
|
-
I18n.load_path
|
4
|
+
I18n.load_path += Dir[File.join(__dir__, '../config/locale/*.yml')]
|
5
5
|
|
6
6
|
# == UUIDParameter
|
7
7
|
#
|
@@ -28,43 +28,6 @@ I18n.load_path << File.expand_path("locale/en.yml", __dir__)
|
|
28
28
|
# user.reload.uuid # => '8bb96d58-2efd-45df-833b-119971a19fea' (unchanged)
|
29
29
|
# user.to_param # => '8bb96d58-2efd-45df-833b-119971a19fea'
|
30
30
|
#
|
31
|
-
module UUIDParameter
|
32
|
-
extend ActiveSupport::Concern
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
included do
|
38
|
-
validates :uuid,
|
39
|
-
presence: true,
|
40
|
-
uniqueness: true,
|
41
|
-
format: { with: UUID_V4_REGEX, message: :not_a_uuid_v4 }
|
42
|
-
|
43
|
-
before_validation :assign_uuid
|
44
|
-
before_save :recover_uuid
|
45
|
-
|
46
|
-
def to_param
|
47
|
-
uuid.to_s
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def assign_uuid
|
53
|
-
self.uuid ||= SecureRandom.uuid
|
54
|
-
end
|
55
|
-
|
56
|
-
def existing_uuid_changed?
|
57
|
-
!new_record? && !uuid_was.nil? && uuid_changed?
|
58
|
-
end
|
59
|
-
|
60
|
-
def recover_uuid
|
61
|
-
self.uuid = uuid_was if existing_uuid_changed?
|
62
|
-
reset_uuid! unless UUID_V4_REGEX.match?(self.uuid)
|
63
|
-
end
|
64
|
-
|
65
|
-
def reset_uuid!
|
66
|
-
self.uuid = nil
|
67
|
-
assign_uuid
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
32
|
+
require 'uuid_parameter/railtie'
|
33
|
+
require 'uuid_parameter/concern'
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UUIDParameter
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Note the static '4' in the third group: that's the UUID version.
|
7
|
+
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]
|
8
|
+
|
9
|
+
class InvalidRandomUUIDError < ActiveModel::Errors; end
|
10
|
+
|
11
|
+
included do
|
12
|
+
validates :uuid,
|
13
|
+
presence: true,
|
14
|
+
uniqueness: true,
|
15
|
+
# format: { with: UUID_V4_REGEX, message: :not_a_uuid_v4 }
|
16
|
+
with: :uuid4_validator
|
17
|
+
|
18
|
+
|
19
|
+
before_validation :assign_uuid
|
20
|
+
before_save :recover_uuid
|
21
|
+
|
22
|
+
def to_param
|
23
|
+
uuid.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def assign_uuid
|
29
|
+
self.uuid ||= SecureRandom.uuid
|
30
|
+
end
|
31
|
+
|
32
|
+
def existing_uuid_changed?
|
33
|
+
!new_record? && !uuid_was.nil? && uuid_changed?
|
34
|
+
end
|
35
|
+
|
36
|
+
def recover_uuid
|
37
|
+
self.uuid = uuid_was if existing_uuid_changed?
|
38
|
+
reset_uuid! unless UUID_V4_REGEX.match?(self.uuid)
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset_uuid!
|
42
|
+
self.uuid = nil
|
43
|
+
assign_uuid
|
44
|
+
end
|
45
|
+
|
46
|
+
def uuid4_validator
|
47
|
+
errors.add(:uuid, :invalid_random_uuid, message: :not_a_uuid_v4) unless uuid =~ UUID_V4_REGEX
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,12 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rails'
|
4
|
+
|
3
5
|
module UUIDParameter
|
4
|
-
class
|
6
|
+
class Engine < ::Rails::Engine; end
|
7
|
+
|
8
|
+
class Railtie < ::Rails::Railtie # :nodoc:
|
5
9
|
config.uuid_parameter = ActiveSupport::OrderedOptions.new
|
6
10
|
|
7
|
-
initializer 'uuid_parameter
|
8
|
-
|
9
|
-
|
11
|
+
initializer 'uuid_parameter-i18n' do |app|
|
12
|
+
UUIDParameter::Railtie.instance_eval do
|
13
|
+
pattern = pattern_from app.config.i18n.available_locales
|
14
|
+
|
15
|
+
add("config/locale/#{pattern}.yml")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def self.add(pattern)
|
22
|
+
files = Dir[File.join(__dir__, '../..', pattern)]
|
23
|
+
I18n.load_path.concat(files)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.pattern_from(args)
|
27
|
+
array = Array(args || [])
|
28
|
+
array.blank? ? '*' : "#{array.join(',')}"
|
10
29
|
end
|
11
30
|
end
|
12
31
|
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.2.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hellekin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -76,9 +76,11 @@ files:
|
|
76
76
|
- LICENSE
|
77
77
|
- README.md
|
78
78
|
- Rakefile
|
79
|
-
-
|
80
|
-
-
|
79
|
+
- config/locale/en.yml
|
80
|
+
- config/locale/fr.yml
|
81
81
|
- lib/uuid_parameter.rb
|
82
|
+
- lib/uuid_parameter/#error.rb#
|
83
|
+
- lib/uuid_parameter/concern.rb
|
82
84
|
- lib/uuid_parameter/railtie.rb
|
83
85
|
- lib/uuid_parameter/version.rb
|
84
86
|
homepage: https://gitlab.com/incommon.cc/uuid_parameter
|
@@ -96,9 +98,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
98
|
version: '0'
|
97
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
100
|
requirements:
|
99
|
-
- - "
|
101
|
+
- - ">="
|
100
102
|
- !ruby/object:Gem::Version
|
101
|
-
version:
|
103
|
+
version: '0'
|
102
104
|
requirements: []
|
103
105
|
rubyforge_project:
|
104
106
|
rubygems_version: 2.7.6
|
data/lib/locale/en.yml
DELETED