active_record_better_dependent_error_messages 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 505842e6cb841ae76e2e4954fde49e8706d99bae
4
+ data.tar.gz: 3c61dcc6fdbe1c8fd7fcd75d2d06993ac34550f8
5
+ SHA512:
6
+ metadata.gz: 9fd9c9f00dd1a054eea940da731fcd190c3b3d3f9364b5f566fa3bbfd1457a1f1653ddb11f1cfceb775e454f9c2494b154f7fd1fbf971c6d9e718d69d518fd04
7
+ data.tar.gz: 430de6c63eb4c5fe81af967f4432f86535b25ed8aa2488c3f885a27c5628bdca98cd5c95340860e2e540076dbd3b76f5bd80c8be92f2cd15f596ca86700dd83f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 kaspernj
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # ActiveRecordBetterDependentErrorMessages
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "active_record_better_dependent_error_messages"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install active_record_better_dependent_error_messages
22
+ ```
23
+
24
+ Include it in the models where you want it to inspect the relationships upon destroy like this:
25
+ ```ruby
26
+ class MyModel < ApplicationRecord
27
+ include ActiveRecordBetterDependentErrorMessages::DestroyModule
28
+ end
29
+ ```
30
+
31
+ ## Contributing
32
+ Contribution directions go here.
33
+
34
+ ## License
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiveRecordBetterDependentErrorMessages'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/active_record_better_dependent_error_messages .js
2
+ //= link_directory ../stylesheets/active_record_better_dependent_error_messages .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Active record better dependent error messages</title>
5
+ <%= stylesheet_link_tag "active_record_better_dependent_error_messages/application", media: "all" %>
6
+ <%= javascript_include_tag "active_record_better_dependent_error_messages/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ActiveRecordBetterDependentErrorMessages::Engine.routes.draw do
2
+ end
@@ -0,0 +1,8 @@
1
+ require "active_record_better_dependent_error_messages/engine"
2
+
3
+ module ActiveRecordBetterDependentErrorMessages
4
+ path = "#{File.dirname(__FILE__)}/active_record_better_dependent_error_messages"
5
+
6
+ autoload :DestroyModule, "#{path}/destroy_module"
7
+ autoload :DestroyValidator, "#{path}/destroy_validator"
8
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecordBetterDependentErrorMessages::DestroyModule
2
+ def destroy
3
+ ActiveRecordBetterDependentErrorMessages::DestroyValidator.(model: self)
4
+ return false if errors.any?
5
+ super
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ class ActiveRecordBetterDependentErrorMessages::DestroyValidator
2
+ attr_reader :args, :root_model, :model, :trace
3
+
4
+ def initialize(root_model:, model:, trace:, args:)
5
+ @args = args
6
+ @root_model = root_model
7
+ @model = model
8
+ @trace = trace
9
+ end
10
+
11
+ def self.call(root_model: nil, model:, trace: nil, **args)
12
+ root_model ||= model
13
+ trace ||= [model]
14
+
15
+ ActiveRecordBetterDependentErrorMessages::DestroyValidator.new(root_model: root_model, model: model, trace: trace, args: args).()
16
+ end
17
+
18
+ def call
19
+ model.class.reflect_on_all_associations.each do |association|
20
+ if association.options[:dependent] == :restrict_with_error || association.options[:dependent] == :restrict_with_exception
21
+ check_restrict_association(association)
22
+ elsif association.options[:dependent] == :destroy
23
+ check_sub_destroy_association(association)
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def check_restrict_association(association)
31
+ if association.macro == :has_one
32
+ has_one_model = model.__send__(association.name)
33
+
34
+ if has_one_model
35
+ root_model.errors.add(:base, "Cannot delete because of #{trace_as_string} has dependent record: #{association.name} with ID: #{has_one_model.id}")
36
+ end
37
+ elsif association.macro == :has_many
38
+ ids = model.__send__(association.name).pluck(:id)
39
+ if ids.any?
40
+ root_model.errors.add(:base, "Cannot delete because of #{trace_as_string} has dependent records: #{association.name} with IDs: #{ids.join(", ")}")
41
+ end
42
+ end
43
+ end
44
+
45
+ def check_sub_destroy_association(association)
46
+ if association.macro == :has_one
47
+ sub_model = model.__send__(association.name)
48
+ ActiveRecordBetterDependentErrorMessages::DestroyValidator.(root_model: root_model, model: sub_model, trace: trace + [sub_model]) if sub_model
49
+ elsif association.macro == :has_many
50
+ sub_models = model.__send__(association.name)
51
+ sub_models.find_each do |sub_model_i|
52
+ ActiveRecordBetterDependentErrorMessages::DestroyValidator.(root_model: root_model, model: sub_model_i, trace: trace + [sub_model_i])
53
+ end
54
+ end
55
+ end
56
+
57
+ def trace_as_string
58
+ trace.map do |model|
59
+ "#{model.class.name}(#{model.id})"
60
+ end.join(" -> ")
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ActiveRecordBetterDependentErrorMessages
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordBetterDependentErrorMessages
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :active_record_better_dependent_error_messages do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_better_dependent_error_messages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kaspernj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A gem that makes better dependent error messages telling exactly which
42
+ relationship tree failed with which ID's.
43
+ email:
44
+ - kaspernj@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/assets/config/active_record_better_dependent_error_messages_manifest.js
53
+ - app/assets/javascripts/active_record_better_dependent_error_messages/application.js
54
+ - app/assets/stylesheets/active_record_better_dependent_error_messages/application.css
55
+ - app/controllers/active_record_better_dependent_error_messages/application_controller.rb
56
+ - app/helpers/active_record_better_dependent_error_messages/application_helper.rb
57
+ - app/jobs/active_record_better_dependent_error_messages/application_job.rb
58
+ - app/mailers/active_record_better_dependent_error_messages/application_mailer.rb
59
+ - app/models/active_record_better_dependent_error_messages/application_record.rb
60
+ - app/views/layouts/active_record_better_dependent_error_messages/application.html.erb
61
+ - config/routes.rb
62
+ - lib/active_record_better_dependent_error_messages.rb
63
+ - lib/active_record_better_dependent_error_messages/destroy_module.rb
64
+ - lib/active_record_better_dependent_error_messages/destroy_validator.rb
65
+ - lib/active_record_better_dependent_error_messages/engine.rb
66
+ - lib/active_record_better_dependent_error_messages/version.rb
67
+ - lib/tasks/active_record_better_dependent_error_messages_tasks.rake
68
+ homepage: https://github.com/kaspernj/active-record-better-dependent-error-messages
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.6.8
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A gem that makes better dependent error messages telling exactly which relationship
92
+ tree failed with which ID's.
93
+ test_files: []