activerecord-databasevalidations 1.0.2 → 1.0.3
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/active_record/database_validations/version.rb +1 -1
- data/lib/active_record/validations/adapter_helper.rb +9 -0
- data/lib/active_record/validations/database_constraints.rb +5 -8
- data/lib/active_record/validations/string_truncator.rb +4 -0
- data/test/string_truncator_test.rb +23 -0
- metadata +4 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 19a316a681935a58dfcc28e3b1f61533b939f238c33b5e59168815b659fd46e9
         | 
| 4 | 
            +
              data.tar.gz: 652292e8f015dd4a68ed58e3bda8875dc9cd692a073f62690dc25dacb6bd27b5
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 1a3e18729c91ed650c349a68daf2b2187453d8516fac073ca6f518469d775c9180e70e9f775236e8ca2ab5b2e4395ba7a9d8f2d14c0f5ffd142b4fad7a7713f9
         | 
| 7 | 
            +
              data.tar.gz: '0659a85b324c4e939b7321e62c651c27a51f61f32fa42dc2f65abdb0c21f2db18f69b078c39864d632125290d144bec6fc5632db736a7c11d394680a4f17d59a'
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
| @@ -1,9 +1,12 @@ | |
| 1 1 | 
             
            require 'active_model/validations/bytesize'
         | 
| 2 2 | 
             
            require 'active_model/validations/not_null'
         | 
| 3 | 
            +
            require 'active_record/validations/adapter_helper'
         | 
| 3 4 |  | 
| 4 5 | 
             
            module ActiveRecord
         | 
| 5 6 | 
             
              module Validations
         | 
| 6 7 | 
             
                class DatabaseConstraintsValidator < ActiveModel::EachValidator
         | 
| 8 | 
            +
                  include Validations::AdapterHelper
         | 
| 9 | 
            +
             | 
| 7 10 | 
             
                  attr_reader :constraints
         | 
| 8 11 |  | 
| 9 12 | 
             
                  VALID_CONSTRAINTS = Set[:size, :not_null, :range]
         | 
| @@ -35,7 +38,7 @@ module ActiveRecord | |
| 35 38 |  | 
| 36 39 | 
             
                  def size_validator(klass, column)
         | 
| 37 40 | 
             
                    return unless constraints.include?(:size)
         | 
| 38 | 
            -
                    return unless mysql_adapter?(klass)
         | 
| 41 | 
            +
                    return unless mysql_adapter?(klass.connection)
         | 
| 39 42 | 
             
                    return unless column.text? || column.binary?
         | 
| 40 43 |  | 
| 41 44 | 
             
                    maximum, type, encoding = ActiveRecord::DatabaseValidations::MySQL.column_size_limit(column)
         | 
| @@ -48,7 +51,7 @@ module ActiveRecord | |
| 48 51 |  | 
| 49 52 | 
             
                  def range_validator(klass, column)
         | 
| 50 53 | 
             
                    return unless constraints.include?(:range)
         | 
| 51 | 
            -
                    return unless mysql_adapter?(klass)
         | 
| 54 | 
            +
                    return unless mysql_adapter?(klass.connection)
         | 
| 52 55 | 
             
                    return unless column.number?
         | 
| 53 56 |  | 
| 54 57 | 
             
                    args = { attributes: [column.name.to_sym], class: klass, allow_nil: true }
         | 
| @@ -79,12 +82,6 @@ module ActiveRecord | |
| 79 82 | 
             
                      validator.validate(record)
         | 
| 80 83 | 
             
                    end
         | 
| 81 84 | 
             
                  end
         | 
| 82 | 
            -
             | 
| 83 | 
            -
                  private
         | 
| 84 | 
            -
             | 
| 85 | 
            -
                  def mysql_adapter?(klass)
         | 
| 86 | 
            -
                    klass.connection.class < ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
         | 
| 87 | 
            -
                  end
         | 
| 88 85 | 
             
                end
         | 
| 89 86 | 
             
              end
         | 
| 90 87 | 
             
            end
         | 
| @@ -1,9 +1,13 @@ | |
| 1 | 
            +
            require 'active_record/validations/adapter_helper'
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            module ActiveRecord
         | 
| 2 4 | 
             
              module DatabaseValidations
         | 
| 3 5 | 
             
                module StringTruncator
         | 
| 4 6 | 
             
                  extend ActiveSupport::Concern
         | 
| 7 | 
            +
                  include Validations::AdapterHelper
         | 
| 5 8 |  | 
| 6 9 | 
             
                  def truncate_value_to_field_limit(field, value)
         | 
| 10 | 
            +
                    return value unless mysql_adapter?(self.class.connection)
         | 
| 7 11 | 
             
                    return if value.nil?
         | 
| 8 12 |  | 
| 9 13 | 
             
                    column = self.class.columns_hash[field.to_s]
         | 
| @@ -23,6 +23,19 @@ class MagicalCreature < ActiveRecord::Base | |
| 23 23 | 
             
              truncate_to_field_limit :another_string
         | 
| 24 24 | 
             
            end
         | 
| 25 25 |  | 
| 26 | 
            +
            class MagicalSqliteCreature < ActiveRecord::Base
         | 
| 27 | 
            +
              establish_connection(adapter: "sqlite3", database: ":memory:")
         | 
| 28 | 
            +
              connection.create_table("magical_sqlite_creatures", force: true) do |t|
         | 
| 29 | 
            +
                t.string :string, limit: 255
         | 
| 30 | 
            +
                t.string :another_string, limit: 255
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              include ActiveRecord::DatabaseValidations::StringTruncator
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              before_validation truncate_string(:string)
         | 
| 36 | 
            +
              truncate_to_field_limit :another_string
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
             | 
| 26 39 | 
             
            class StringTruncatorTest < Minitest::Test
         | 
| 27 40 | 
             
              def test_handles_nil_gracefully
         | 
| 28 41 | 
             
                u_nil = MagicalCreature.create!(string: 'present', tinytext: 'present')
         | 
| @@ -72,4 +85,14 @@ class StringTruncatorTest < Minitest::Test | |
| 72 85 | 
             
                u6 = MagicalCreature.new(another_string: 'a' * 256)
         | 
| 73 86 | 
             
                assert_equal 'a' * 255, u6.another_string
         | 
| 74 87 | 
             
              end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
              def test_skips_truncate_for_non_mysql_adapter
         | 
| 90 | 
            +
                record = MagicalSqliteCreature.new(string: 'a' * 256)
         | 
| 91 | 
            +
                assert(record.valid?)
         | 
| 92 | 
            +
                assert_equal 'a' * 256, record.string
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                record.another_string = 'a' * 256
         | 
| 95 | 
            +
                assert(record.valid?)
         | 
| 96 | 
            +
                assert_equal 'a' * 256, record.another_string
         | 
| 97 | 
            +
              end
         | 
| 75 98 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: activerecord-databasevalidations
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0. | 
| 4 | 
            +
              version: 1.0.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Willem van Bergen
         | 
| 8 8 | 
             
            bindir: bin
         | 
| 9 9 | 
             
            cert_chain: []
         | 
| 10 | 
            -
            date: 2025- | 
| 10 | 
            +
            date: 2025-03-06 00:00:00.000000000 Z
         | 
| 11 11 | 
             
            dependencies:
         | 
| 12 12 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 13 13 | 
             
              name: activerecord
         | 
| @@ -121,6 +121,7 @@ files: | |
| 121 121 | 
             
            - lib/active_record/database_validations/varchar_191.rb
         | 
| 122 122 | 
             
            - lib/active_record/database_validations/varchar_255.rb
         | 
| 123 123 | 
             
            - lib/active_record/database_validations/version.rb
         | 
| 124 | 
            +
            - lib/active_record/validations/adapter_helper.rb
         | 
| 124 125 | 
             
            - lib/active_record/validations/database_constraints.rb
         | 
| 125 126 | 
             
            - lib/active_record/validations/string_truncator.rb
         | 
| 126 127 | 
             
            - lib/active_record/validations/typed_column.rb
         | 
| @@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 153 154 | 
             
                - !ruby/object:Gem::Version
         | 
| 154 155 | 
             
                  version: '0'
         | 
| 155 156 | 
             
            requirements: []
         | 
| 156 | 
            -
            rubygems_version: 3.6. | 
| 157 | 
            +
            rubygems_version: 3.6.5
         | 
| 157 158 | 
             
            specification_version: 4
         | 
| 158 159 | 
             
            summary: Add validations to your ActiveRecord models based on MySQL database constraints.
         | 
| 159 160 | 
             
            test_files:
         |