uuid_parameter 0.2.0.pre.alpha.1 → 0.2.0

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: f7718b5d84a19a25b9ce316d23919ef34a13a31a50aa46993578f18f59a81eed
4
- data.tar.gz: d7be3d70a50ecda0d8fe7db471ab36f948618b56f09acba4af076e2f6da62f15
3
+ metadata.gz: 276b01d703d6a657be6b878402fd58c598a7f297442c6976b5b124fa206f15e4
4
+ data.tar.gz: 7ef3b621857adfe47db0f06673dadc5866f531cfce25c3aba60aabad389d08c0
5
5
  SHA512:
6
- metadata.gz: 7906c3a3529293eabb7c51a0c6bc97e4d6e2eb8a81ab19f3d717e2c67fb2caec9fbb38c8e5cfd9ca05d56ddfd73c02352338f7618995e8de7adfc32faafa8279
7
- data.tar.gz: 28ae550b9f0288cdaf292045f1c4f924167a3d2108dc5babdb03e5ba3db0b70590920feb8e875da937abe5a95e4fca127955d0081c3159b4a0313ad114141e15
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
@@ -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 << File.expand_path("locale/en.yml", __dir__)
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
- # 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
-
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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UUIDParameter
4
+
5
+
6
+ end
@@ -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 Railtie < ::Rails::Railtie
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.set_locales' do |app|
8
- app.config.i18n.load_paths << File.expand_path('../locale', __DIR__)
9
- app.config.i18n.available_locales = [:en, :fr].freeze
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
@@ -1,3 +1,3 @@
1
1
  module UUIDParameter
2
- VERSION = '0.2.0-alpha.1'
2
+ VERSION = '0.2.0'
3
3
  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.pre.alpha.1
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-10 00:00:00.000000000 Z
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
- - lib/locale/en.yml
80
- - lib/locale/fr.yml
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: 1.3.1
103
+ version: '0'
102
104
  requirements: []
103
105
  rubyforge_project:
104
106
  rubygems_version: 2.7.6
data/lib/locale/en.yml DELETED
@@ -1,4 +0,0 @@
1
- en:
2
- errors:
3
- messages:
4
- not_a_uuid_v4: "must be a random UUID (v4)"