validation_errors 0.2.0 → 0.3.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 +4 -4
 - data/CHANGELOG.md +5 -4
 - data/Gemfile.lock +4 -2
 - data/README.md +28 -0
 - data/lib/validation_error.rb +29 -1
 - data/lib/validation_errors.rb +15 -2
 - metadata +3 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: a477abd871a7de8b45962e84d301fa87507f75f91c63e722668c3151fa808358
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 9546453a776ce4579cf4c32cf23fefe42cea5282331c30dc3460961a266401a6
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 5b248a94703c3bd0145794e06776baee1723664201b255fb47251fb8e41528c8c938f315bd48b07e23cb1d298929c3229b6dc8b1257dfc9782764fd1e2e22162
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 64ae712dbac662fcb2458e5096e7c03ba668126cacd9a8257eb6acbd2c2afa1b90b9d9a5683b2744be6dfe2a3451cb836d9718e077f10168415502a0137cd216
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    | 
         @@ -1,11 +1,12 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            ##  
     | 
| 
      
 1 
     | 
    
         
            +
            ## [0.3.0] - 2024-11-05
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            -  
     | 
| 
      
 3 
     | 
    
         
            +
            - Fix persistance in case of rolled back transactions.
         
     | 
| 
      
 4 
     | 
    
         
            +
            - Filter `Rails.application.config.filter_parameters` by default. Does not support procs. 
         
     | 
| 
       4 
5 
     | 
    
         | 
| 
       5 
6 
     | 
    
         
             
            ## [0.2.0] - 2022-12-29
         
     | 
| 
       6 
7 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
            - Allow to use a custom action name when tracking errors
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Allow to use a custom action name when tracking errors.
         
     | 
| 
       8 
9 
     | 
    
         | 
| 
       9 
10 
     | 
    
         
             
            ## [0.1.0] - 2022-09-19
         
     | 
| 
       10 
11 
     | 
    
         | 
| 
       11 
     | 
    
         
            -
            - Initial release
         
     | 
| 
      
 12 
     | 
    
         
            +
            - Initial release.
         
     | 
    
        data/Gemfile.lock
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            PATH
         
     | 
| 
       2 
2 
     | 
    
         
             
              remote: .
         
     | 
| 
       3 
3 
     | 
    
         
             
              specs:
         
     | 
| 
       4 
     | 
    
         
            -
                validation_errors (0. 
     | 
| 
      
 4 
     | 
    
         
            +
                validation_errors (0.3.0)
         
     | 
| 
       5 
5 
     | 
    
         
             
                  activerecord (>= 4.1.0)
         
     | 
| 
       6 
6 
     | 
    
         
             
                  zeitwerk (>= 2.0.0)
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
         @@ -46,6 +46,7 @@ GEM 
     | 
|
| 
       46 
46 
     | 
    
         
             
                  rubocop-ast (>= 0.4.0)
         
     | 
| 
       47 
47 
     | 
    
         
             
                ruby-progressbar (1.11.0)
         
     | 
| 
       48 
48 
     | 
    
         
             
                sqlite3 (1.5.0-arm64-darwin)
         
     | 
| 
      
 49 
     | 
    
         
            +
                sqlite3 (1.5.0-x86_64-linux)
         
     | 
| 
       49 
50 
     | 
    
         
             
                standard (0.13.0)
         
     | 
| 
       50 
51 
     | 
    
         
             
                  rubocop (= 1.10.0)
         
     | 
| 
       51 
52 
     | 
    
         
             
                  rubocop-performance (= 1.9.2)
         
     | 
| 
         @@ -56,6 +57,7 @@ GEM 
     | 
|
| 
       56 
57 
     | 
    
         | 
| 
       57 
58 
     | 
    
         
             
            PLATFORMS
         
     | 
| 
       58 
59 
     | 
    
         
             
              arm64-darwin-21
         
     | 
| 
      
 60 
     | 
    
         
            +
              x86_64-linux
         
     | 
| 
       59 
61 
     | 
    
         | 
| 
       60 
62 
     | 
    
         
             
            DEPENDENCIES
         
     | 
| 
       61 
63 
     | 
    
         
             
              minitest (~> 5.0)
         
     | 
| 
         @@ -65,4 +67,4 @@ DEPENDENCIES 
     | 
|
| 
       65 
67 
     | 
    
         
             
              validation_errors!
         
     | 
| 
       66 
68 
     | 
    
         | 
| 
       67 
69 
     | 
    
         
             
            BUNDLED WITH
         
     | 
| 
       68 
     | 
    
         
            -
               2. 
     | 
| 
      
 70 
     | 
    
         
            +
               2.5.18
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -184,6 +184,34 @@ group by 1, 2 
     | 
|
| 
       184 
184 
     | 
    
         
             
            FlatValidationError.group(:invalid_model_name, :error_column).count
         
     | 
| 
       185 
185 
     | 
    
         
             
            ```
         
     | 
| 
       186 
186 
     | 
    
         | 
| 
      
 187 
     | 
    
         
            +
            ## RailsAdmin integration
         
     | 
| 
      
 188 
     | 
    
         
            +
             
     | 
| 
      
 189 
     | 
    
         
            +
            We provide here some code samples to integrate the models in [RailsAdmin](https://github.com/sferik/rails_admin).
         
     | 
| 
      
 190 
     | 
    
         
            +
             
     | 
| 
      
 191 
     | 
    
         
            +
            This configuration will give you a basic configuration to work with the validation errors efficiently.
         
     | 
| 
      
 192 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 193 
     | 
    
         
            +
            config.model "ValidationError" do
         
     | 
| 
      
 194 
     | 
    
         
            +
              list do
         
     | 
| 
      
 195 
     | 
    
         
            +
                include_fields :invalid_model_name, :invalid_model_id, :action, :created_at
         
     | 
| 
      
 196 
     | 
    
         
            +
             
     | 
| 
      
 197 
     | 
    
         
            +
                field :details, :string do
         
     | 
| 
      
 198 
     | 
    
         
            +
                  visible false
         
     | 
| 
      
 199 
     | 
    
         
            +
                  searchable true
         
     | 
| 
      
 200 
     | 
    
         
            +
                  filterable true
         
     | 
| 
      
 201 
     | 
    
         
            +
                end
         
     | 
| 
      
 202 
     | 
    
         
            +
              end
         
     | 
| 
      
 203 
     | 
    
         
            +
             
     | 
| 
      
 204 
     | 
    
         
            +
              show do
         
     | 
| 
      
 205 
     | 
    
         
            +
                include_fields :invalid_model_name, :invalid_model_id, :action
         
     | 
| 
      
 206 
     | 
    
         
            +
             
     | 
| 
      
 207 
     | 
    
         
            +
                field(:created_at)
         
     | 
| 
      
 208 
     | 
    
         
            +
                field(:details) do
         
     | 
| 
      
 209 
     | 
    
         
            +
                  formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].details)}</pre>".html_safe }
         
     | 
| 
      
 210 
     | 
    
         
            +
                end
         
     | 
| 
      
 211 
     | 
    
         
            +
              end
         
     | 
| 
      
 212 
     | 
    
         
            +
            end
         
     | 
| 
      
 213 
     | 
    
         
            +
            ```
         
     | 
| 
      
 214 
     | 
    
         
            +
             
     | 
| 
       187 
215 
     | 
    
         
             
            ## Development
         
     | 
| 
       188 
216 
     | 
    
         | 
| 
       189 
217 
     | 
    
         
             
            After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. 
         
     | 
    
        data/lib/validation_error.rb
    CHANGED
    
    | 
         @@ -2,9 +2,37 @@ 
     | 
|
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            class ValidationError < ActiveRecord::Base
         
     | 
| 
       4 
4 
     | 
    
         
             
              def self.track(invalid_model, action: invalid_model.persisted? ? "update" : "create")
         
     | 
| 
      
 5 
     | 
    
         
            +
                details = filter_sensible_information(invalid_model.errors.details)
         
     | 
| 
       5 
6 
     | 
    
         
             
                create!(invalid_model_name: invalid_model.class.name,
         
     | 
| 
       6 
7 
     | 
    
         
             
                        invalid_model_id: invalid_model.id,
         
     | 
| 
       7 
     | 
    
         
            -
                        details:  
     | 
| 
      
 8 
     | 
    
         
            +
                        details: details,
         
     | 
| 
       8 
9 
     | 
    
         
             
                        action: action)
         
     | 
| 
       9 
10 
     | 
    
         
             
              end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              def self.filter_sensible_information(details)
         
     | 
| 
      
 13 
     | 
    
         
            +
                filter_parameters = if defined?(Rails) && Rails.respond_to?(:application)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  Rails.application.config.filter_parameters
         
     | 
| 
      
 15 
     | 
    
         
            +
                else
         
     | 
| 
      
 16 
     | 
    
         
            +
                  []
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
                filtered_details = details.dup
         
     | 
| 
      
 19 
     | 
    
         
            +
                filtered_details.each do |column_name, errors|
         
     | 
| 
      
 20 
     | 
    
         
            +
                  filter_parameters.each do |filter|
         
     | 
| 
      
 21 
     | 
    
         
            +
                    must_filter = case filter
         
     | 
| 
      
 22 
     | 
    
         
            +
                                  when Regexp
         
     | 
| 
      
 23 
     | 
    
         
            +
                                    filter.match?(column_name)
         
     | 
| 
      
 24 
     | 
    
         
            +
                                  when String, Symbol
         
     | 
| 
      
 25 
     | 
    
         
            +
                                    filter.to_s == column_name.to_s
         
     | 
| 
      
 26 
     | 
    
         
            +
                    end
         
     | 
| 
      
 27 
     | 
    
         
            +
                    if must_filter
         
     | 
| 
      
 28 
     | 
    
         
            +
                      errors.each do |error|
         
     | 
| 
      
 29 
     | 
    
         
            +
                        if error[:value].present?
         
     | 
| 
      
 30 
     | 
    
         
            +
                          error[:value] = "***"
         
     | 
| 
      
 31 
     | 
    
         
            +
                        end
         
     | 
| 
      
 32 
     | 
    
         
            +
                      end
         
     | 
| 
      
 33 
     | 
    
         
            +
                    end
         
     | 
| 
      
 34 
     | 
    
         
            +
                  end
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
      
 36 
     | 
    
         
            +
                filtered_details
         
     | 
| 
      
 37 
     | 
    
         
            +
              end
         
     | 
| 
       10 
38 
     | 
    
         
             
            end
         
     | 
    
        data/lib/validation_errors.rb
    CHANGED
    
    | 
         @@ -22,18 +22,31 @@ module ValidationErrors 
     | 
|
| 
       22 
22 
     | 
    
         
             
                end
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
                module InstanceMethods
         
     | 
| 
       25 
     | 
    
         
            -
                  def  
     | 
| 
      
 25 
     | 
    
         
            +
                  def update(attributes)
         
     | 
| 
       26 
26 
     | 
    
         
             
                    super.tap do |result|
         
     | 
| 
       27 
27 
     | 
    
         
             
                      ValidationError.track(self) unless result
         
     | 
| 
       28 
28 
     | 
    
         
             
                    end
         
     | 
| 
       29 
29 
     | 
    
         
             
                  end
         
     | 
| 
       30 
30 
     | 
    
         | 
| 
      
 31 
     | 
    
         
            +
                  def update!(attributes)
         
     | 
| 
      
 32 
     | 
    
         
            +
                    super
         
     | 
| 
      
 33 
     | 
    
         
            +
                  rescue ActiveRecord::RecordInvalid
         
     | 
| 
      
 34 
     | 
    
         
            +
                    ValidationError.track(self)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    raise
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  def save(**options)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    super.tap do |result|
         
     | 
| 
      
 40 
     | 
    
         
            +
                      ValidationError.track(self) unless result || persisted?
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
       31 
44 
     | 
    
         
             
                  # Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
         
     | 
| 
       32 
45 
     | 
    
         
             
                  # will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid.
         
     | 
| 
       33 
46 
     | 
    
         
             
                  def save!(**options)
         
     | 
| 
       34 
47 
     | 
    
         
             
                    super
         
     | 
| 
       35 
48 
     | 
    
         
             
                  rescue ActiveRecord::RecordInvalid
         
     | 
| 
       36 
     | 
    
         
            -
                    ValidationError.track(self)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    ValidationError.track(self) unless persisted?
         
     | 
| 
       37 
50 
     | 
    
         
             
                    raise
         
     | 
| 
       38 
51 
     | 
    
         
             
                  end
         
     | 
| 
       39 
52 
     | 
    
         
             
                end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: validation_errors
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.3.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Alessandro Rodi
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2024-11-05 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activerecord
         
     | 
| 
         @@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       144 
144 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       145 
145 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       146 
146 
     | 
    
         
             
            requirements: []
         
     | 
| 
       147 
     | 
    
         
            -
            rubygems_version: 3. 
     | 
| 
      
 147 
     | 
    
         
            +
            rubygems_version: 3.2.22
         
     | 
| 
       148 
148 
     | 
    
         
             
            signing_key:
         
     | 
| 
       149 
149 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       150 
150 
     | 
    
         
             
            summary: Track ActiveRecord validation errors on database
         
     |