uuid_attribute 0.2.0 → 0.9.0

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
  SHA256:
3
- metadata.gz: a3a31aeee55a99380975bf4cf4fbcfe7c506618bace27420afcb31d31ae06ec2
4
- data.tar.gz: 304c13913329ea077e252deb3fb3f3bd0c4001c335db877650ac2d467b696e77
3
+ metadata.gz: 47ad9521befd5c8fc61d64ac6ce670e55edcbc0d5833405c02a6552900834dd9
4
+ data.tar.gz: 38b608ec56aec54bc152b36addadc3b7a173a35db2ed25ffdf84654e757114bf
5
5
  SHA512:
6
- metadata.gz: eb6fd591641a8adef2c268d319bbd678c6bc930258ed12ec751d3d3be5101b4cf35baf63801ddfe2d39a532dd2db8582d8f10b664dc982bcfe24519893985f99
7
- data.tar.gz: 92036ff31a27d90e635bc6d3fae13a153145531fefe391f43cb0e6e4eab19d54990cc69fb6b1d133017aa3a7e1050edc26103e3c4c7a0c8635297019c3eeb9a7
6
+ metadata.gz: cef667567fc3384b760385c7ce38e75ac43b33ca7a002988d5a799f3b62ddc19b053d49a44a3cd9e55b6a09d051cc92cf6b0f27d210cdc479b0f506c974f9623
7
+ data.tar.gz: debb0b40b887e22cd37c82d211a7b00430abd4612b2d95efe8fccbef2af46e4ee51753d6917c13d4eb6476520405d05a74e1e04e66292d76d20fc45e71b27757
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.9.0 - 2022-08-23
4
+
5
+ * Improved tests, now testing all ruby and supported rails versions
6
+ * Added support for PostgreSQL and SQLite
7
+
3
8
  ## 0.2.0 - 2022-08-20
4
9
 
5
10
  * Added tests
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
- # UUID::Attribute
1
+ # UUidAttribute
2
2
 
3
3
  ## Installation
4
4
 
5
5
  Install the gem and add to the application's Gemfile by executing:
6
6
 
7
- $ bundle add activerecord-uuid-attribute
7
+ $ bundle add uuid_attribute
8
8
 
9
9
  If bundler is not being used to manage dependencies, install the gem by executing:
10
10
 
11
- $ gem install activerecord-uuid-attribute
11
+ $ gem install uuid_attribute
12
12
 
13
13
  ## Usage
14
14
 
@@ -16,13 +16,18 @@ TODO: Write usage instructions here
16
16
 
17
17
  ```
18
18
  class YourModel < ApplicationRecord
19
- # Need to put attribute manually
20
19
  attribute :id, :uuid, default: -> { SecureRandom.uuid }
21
20
  end
22
21
 
22
+ Or if you want to disable auto detect of UUIDs
23
+
23
24
  UuidAttribute.setup do |config|
24
25
  # Configure generators to use UUID as primary key (defaults to true)
25
- config.default_primary_id = true
26
+ config.auto_detect_binary_ids = false
27
+ end
28
+
29
+ class YourModel < ApplicationRecord
30
+ attribute :id, :uuid, default: -> { SecureRandom.uuid }
26
31
  end
27
32
  ```
28
33
 
@@ -9,38 +9,21 @@ module UuidAttribute
9
9
  config.eager_load_namespaces << ::UuidAttribute
10
10
 
11
11
  class << self
12
- def binary16_structure?(field_info)
13
- field_info.type == :binary && field_info.limit == 16
12
+ def binary_structure?(field_info)
13
+ field_info.type == :binary
14
14
  end
15
15
 
16
- def binary16?(klass, field)
16
+ def binary?(klass, field)
17
17
  field_info = klass.attribute_types[field]
18
- binary16_structure?(field_info) && klass.method_defined?("#{field}?")
18
+ binary_structure?(field_info)
19
+ # && klass.method_defined?("#{field}?")
19
20
  end
20
21
 
21
22
  def valid_default_rails_ids?(att)
22
23
  att.eql?("id") || att.end_with?("_id")
23
24
  end
24
- end
25
-
26
- =begin
27
- all_models = ObjectSpace.each_object(Class).select { |c| c < ApplicationRecord}.select(&:name)
28
- all_models.each do |model|
29
- puts model
30
- model.attribute_names.each do |att|
31
- next unless valid_default_rails_ids?(att) && binary16?(att)
32
-
33
- default = nil
34
- default = -> { SecureRandom.uuid } if att.eql? "id"
35
- self.class.define_attribute att, ::UuidAttribute::UUID.new, default: default
36
- end
37
- end
38
- =end
39
-
40
- config.after_initialize do
41
- ActiveRecord::Type.register(:uuid, ::UuidAttribute::UUID)
42
25
 
43
- if UuidAttribute.auto_detect_binary_ids
26
+ def list_models
44
27
  models = []
45
28
  Dir["#{Rails.root}/app/models/*"].each do |file|
46
29
  model = File.basename(file, ".*").classify
@@ -49,10 +32,18 @@ end
49
32
 
50
33
  models -= %w[ActiveRecord Concern]
51
34
 
52
- models.each do |model|
53
- model = model.constantize
35
+ models.each(&:constantize)
36
+ all_active_record_classes
37
+ end
38
+
39
+ def all_active_record_classes
40
+ ObjectSpace.each_object(Class).select { |c| c < ActiveRecord::Base }.select(&:name)
41
+ end
42
+
43
+ def configure_binary_ids
44
+ list_models.each do |model|
54
45
  model.attribute_names.each do |att|
55
- next unless valid_default_rails_ids?(att) && binary16?(model, att)
46
+ next unless valid_default_rails_ids?(att) && binary?(model, att)
56
47
 
57
48
  default = nil
58
49
  default = -> { SecureRandom.uuid } if att.eql? "id"
@@ -60,12 +51,16 @@ end
60
51
  end
61
52
  end
62
53
  end
54
+ end
55
+
56
+ config.after_initialize do
57
+ ActiveRecord::Type.register(:uuid, ::UuidAttribute::UUID)
63
58
 
64
- # ActiveRecord::Base.include ::UuidAttribute::ActiveModel if defined? ActiveRecord::Base
59
+ configure_binary_ids if UuidAttribute.auto_detect_binary_ids
65
60
 
66
61
  if UuidAttribute.default_primary_id
67
62
  # Configure UUID as Default Primary Key
68
- Rails.application.config.generators do |g|
63
+ Rails&.application&.config&.generators do |g|
69
64
  g.orm :active_record, primary_key_type: "binary, limit: 16"
70
65
  end
71
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UuidAttribute
4
- VERSION = "0.2.0"
4
+ VERSION = "0.9.0"
5
5
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require "active_model"
4
4
  require "active_model/type"
5
+ require "rails"
5
6
  require_relative "uuid_attribute/version"
6
7
  require_relative "uuid_attribute/uuid"
7
- require_relative "uuid_attribute/active_model"
8
8
  require_relative "uuid_attribute/utils"
9
9
 
10
10
  # UUID::Attribute is a module that provides a UUID attribute for ActiveRecord
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "pathname"
5
+ # require "active_support/configuration_file"
6
+
7
+ module ARTest
8
+ class << self
9
+ def config
10
+ @config ||= YAML.safe_load(File.read(config_file))
11
+ # ActiveSupport::ConfigurationFile.parse(config_file)
12
+ end
13
+
14
+ private
15
+
16
+ def config_file
17
+ Pathname.new(ENV["ARCONFIG"] || "#{TEST_ROOT}/config/config.yml")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveRecord::Schema.define do
4
+ create_table "samples", id: :binary, limit: 16, force: :cascade do |t|
5
+ t.string "name"
6
+ t.datetime "created_at", null: false
7
+ t.datetime "updated_at", null: false
8
+ end
9
+
10
+ create_table "examples", id: :binary, limit: 16, force: :cascade do |t|
11
+ t.string "name"
12
+ t.references :sample, null: false, foreign_key: false, type: :binary, limit: 16
13
+ t.datetime "created_at", null: false
14
+ t.datetime "updated_at", null: false
15
+ end
16
+ end
data/test/test_helper.rb CHANGED
@@ -8,5 +8,30 @@ SimpleCov.start "rails" do
8
8
  add_filter "lib/uuid_attribute/version"
9
9
  end
10
10
 
11
+ require "rails"
12
+ require "active_record"
11
13
  require "minitest/autorun"
14
+
15
+ TEST_ROOT = __dir__
16
+ require "config/config"
17
+ puts "Testing with adapter #{ENV["TEST_ADAPTER"]} - Rails Version: #{Rails.version}"
18
+
19
+ # ActiveRecord.async_query_executor = :global_thread_pool if ActiveRecord.respond_to?(:async_query_executor)
20
+ ActiveRecord::Base.configurations = ARTest.config["connections"]
21
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
22
+ DATABASE_CONFIG = ActiveRecord::DatabaseConfigurations::HashConfig.new(
23
+ "test",
24
+ "test",
25
+ ARTest.config["connections"][ENV["TEST_ADAPTER"]]
26
+ ) if Rails::VERSION::MAJOR >= 7 || (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR > 0)
27
+ DATABASE_CONFIG = ARTest.config["connections"][ENV["TEST_ADAPTER"]] if Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0
28
+ ActiveRecord::Tasks::DatabaseTasks.purge(DATABASE_CONFIG) unless ENV["TEST_ADAPTER"] != "sqlite"
29
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(
30
+ DATABASE_CONFIG,
31
+ :ruby,
32
+ "#{TEST_ROOT}/config/schema.rb"
33
+ )
34
+ ActiveRecord::Base.establish_connection DATABASE_CONFIG
35
+ # load_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :no
36
+
12
37
  require "uuid_attribute"
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class Sample < ActiveRecord::Base
6
+ end
7
+
8
+ class Example < ActiveRecord::Base
9
+ belongs_to :sample
10
+ end
11
+
12
+ module UUID
13
+ class TestActiveRecord < Minitest::Test
14
+ def setup
15
+ UuidAttribute::Railtie.config.after_initialize.each do |block|
16
+ next unless block && block[0]
17
+ next unless block[0].source_location[0].include? "uuid_attribute/railtie.rb"
18
+
19
+ block[0].call
20
+ end
21
+ end
22
+
23
+ def test_uuid_generation
24
+ result = Sample.new
25
+ refute_nil(result.id)
26
+ end
27
+
28
+ def test_new_and_finds
29
+ result = Sample.create(name: "A Name")
30
+ assert(result.name, "A Name")
31
+ refute_nil(result.id)
32
+ assert(Sample.find(result.id).name, "A Name")
33
+ end
34
+
35
+ def test_references
36
+ record_a = Sample.create(name: "Sample")
37
+ record_b = Example.create(name: "Example on Sample", sample: record_a)
38
+ assert(record_b.sample)
39
+ assert(record_b.sample.id, record_a.id)
40
+ end
41
+ end
42
+ end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  require "test_helper"
3
4
  require "active_record"
4
5
 
5
6
  module UUID
6
7
  class TestType < Minitest::Test
7
- UNORMALIZED_UUID = "a0b1c2d3-e4f5-a6b7-c8d9-e0f1a2b3c4d5".freeze
8
- NORMALIZED_UUID = "A0B1C2D3E4F5A6B7C8D9E0F1A2B3C4D5".freeze
9
- BINARY_UUID = "\xA0\xB1\xC2\xD3\xE4\xF5\xA6\xB7\xC8\xD9\xE0\xF1\xA2\xB3\xC4\xD5".freeze
10
- SHORTEN_UUID = "4tE0ZuelqYsF4p2FEnW2qb".freeze
8
+ UNORMALIZED_UUID = "a0b1c2d3-e4f5-a6b7-c8d9-e0f1a2b3c4d5"
9
+ NORMALIZED_UUID = "A0B1C2D3E4F5A6B7C8D9E0F1A2B3C4D5"
10
+ BINARY_UUID = "\xA0\xB1\xC2\xD3\xE4\xF5\xA6\xB7\xC8\xD9\xE0\xF1\xA2\xB3\xC4\xD5"
11
+ SHORTEN_UUID = "4tE0ZuelqYsF4p2FEnW2qb"
11
12
 
12
13
  def test_type
13
- assert_equal(::UuidAttribute::UUID.new.type,:uuid)
14
+ assert_equal(::UuidAttribute::UUID.new.type, :uuid)
14
15
  end
15
16
 
16
17
  def test_deserialize
@@ -26,7 +27,6 @@ module UUID
26
27
  result = ::UuidAttribute::UUID.new.serialize(UNORMALIZED_UUID)
27
28
  assert_equal("ActiveModel::Type::Binary::Data", result.class.name)
28
29
  assert_equal(BINARY_UUID.bytes, result.to_s.bytes)
29
- assert_equal(BINARY_UUID.bytes, [result.hex].pack("H*").bytes)
30
30
  end
31
31
 
32
32
  def test_cast
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Negri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-20 00:00:00.000000000 Z
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -49,24 +49,26 @@ files:
49
49
  - LICENSE.md
50
50
  - README.md
51
51
  - lib/uuid_attribute.rb
52
- - lib/uuid_attribute/active_model.rb
53
52
  - lib/uuid_attribute/railtie.rb
54
53
  - lib/uuid_attribute/utils.rb
55
54
  - lib/uuid_attribute/uuid.rb
56
55
  - lib/uuid_attribute/version.rb
56
+ - test/config/config.rb
57
+ - test/config/schema.rb
57
58
  - test/test_helper.rb
59
+ - test/uuid/test_activerecord.rb
58
60
  - test/uuid/test_configuration.rb
59
61
  - test/uuid/test_type.rb
60
62
  - test/uuid/test_utilities.rb
61
- homepage: https://github.com/iugu/activerecord-uuid-attribute
63
+ homepage: https://github.com/iugu/uuid_attribute
62
64
  licenses:
63
65
  - MIT
64
66
  metadata:
65
- homepage_uri: https://github.com/iugu/activerecord-uuid-attribute
66
- documentation_uri: https://rubydoc.info/github/iugu/simple_form/activerecord-uuid-attribute
67
- changelog_uri: https://github.com/iugu/activerecord-uuid-attribute/blob/main/CHANGELOG.md
68
- source_code_uri: https://github.com/iugu/activerecord-uuid-attribute
69
- bug_tracker_uri: https://github.com/iugu/activerecord-uuid-attribute/issues
67
+ homepage_uri: https://github.com/iugu/uuid_attribute
68
+ documentation_uri: https://github.com/iugu/uuid_attribute
69
+ changelog_uri: https://github.com/iugu/uuid_attribute/blob/main/CHANGELOG.md
70
+ source_code_uri: https://github.com/iugu/uuid_attribute
71
+ bug_tracker_uri: https://github.com/iugu/uuid_attribute/issues
70
72
  post_install_message:
71
73
  rdoc_options: []
72
74
  require_paths:
@@ -75,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
77
  requirements:
76
78
  - - ">="
77
79
  - !ruby/object:Gem::Version
78
- version: 2.6.0
80
+ version: 2.5.0
79
81
  required_rubygems_version: !ruby/object:Gem::Requirement
80
82
  requirements:
81
83
  - - ">="
@@ -87,7 +89,10 @@ signing_key:
87
89
  specification_version: 4
88
90
  summary: UUID attribute for ActiveRecord
89
91
  test_files:
92
+ - test/config/config.rb
93
+ - test/config/schema.rb
90
94
  - test/test_helper.rb
95
+ - test/uuid/test_activerecord.rb
91
96
  - test/uuid/test_configuration.rb
92
97
  - test/uuid/test_type.rb
93
98
  - test/uuid/test_utilities.rb
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UuidAttribute
4
- # ActiveModel modifications
5
- module ActiveModel
6
- extend ActiveSupport::Concern
7
-
8
- =begin
9
- included do
10
- alias_method :initialize_without_uuid, :initialize
11
- alias_method :initialize, :uuid_initializer
12
- end
13
-
14
- def uuid_initializer(*args)
15
- auto_detect_uuids
16
- initialize_without_uuid(*args)
17
- end
18
-
19
- def binary16_structure?(field_info)
20
- field_info.type == :binary && field_info.limit == 16
21
- end
22
-
23
- def binary16?(field)
24
- field_info = self.class.attribute_types[field]
25
- binary16_structure?(field_info) && respond_to?("#{field}?")
26
- end
27
-
28
- def valid_default_rails_ids?(att)
29
- att.eql?("id") || att.end_with?("_id")
30
- end
31
-
32
- def auto_detect_uuids
33
- return unless UuidAttribute.auto_detect_binary_ids
34
-
35
- self.class.attribute_names.each do |att|
36
- next unless valid_default_rails_ids?(att) && binary16?(att)
37
-
38
- default = nil
39
- default = -> { SecureRandom.uuid } if att.eql? "id"
40
- self.class.define_attribute att, ::UuidAttribute::UUID.new, default: default
41
- end
42
- end
43
- =end
44
- end
45
- end