eagerbeaver 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c558246984c62a7470ea996b956053150d58ec50
4
+ data.tar.gz: 5d8a81bb64fed4f171dc327604b9c06253bd5323
5
+ SHA512:
6
+ metadata.gz: 646386f43d5f2c9510121878dd95fc7c5b94221ee3337a0ef168d93d5e9e9150eae592bf01d61737a716da45adb622b4905ff4b20ff756bb9668b5ad78fcc4d6
7
+ data.tar.gz: ad9f291388b5b039c0f25d94993e2f178b87a537cbbfea74e5633efdfc851bfb830b2d4abd5673217e65d84cd138cd9550cc5f8c9e09d0e7fadf6d4a174235bb
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Danny Park
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # EagerBeaver
2
+
3
+ Rails `eager_load`, `includes`, `preload` can break when changing table or association names. These breaking changes are often discovered in production due to the tricky nature of testing the existence of all the associations listed in the `includes`. Proper testing is error prone because it requires that all of the listed associations are built during the tests. This gem makes it trivial to test that the associations exist so that you can quickly find the `includes` to fix when refactoring data models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'eagerbeaver'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install eagerbeaver
20
+
21
+ ## Usage
22
+
23
+ The constructor takes the model as the first parameter and the `includes` array as the second parameter:
24
+ `EagerBeaver.new(MyModel, [:association1, :association2, assoc3: { foo: :bar }])`
25
+
26
+ The instance has one public method, `.errors`, which returns an array of error messages describing unknown associations.
27
+
28
+ Imagine we have the following class:
29
+ ```ruby
30
+ class Lease < ActiveRecord::Base
31
+ has_many :lease_terms
32
+ has_many :spaces, through: :lease_terms
33
+ belongs_to :tenant
34
+ end
35
+ ```
36
+
37
+ with the corresponding controller:
38
+ ```ruby
39
+ class LeasesController < ApplicationController
40
+ def index
41
+ leases = Lease.all.includes(self.class.lease_includes)
42
+ # do some stuff
43
+ end
44
+
45
+ def self.lease_includes
46
+ [
47
+ :lease_terms,
48
+ :spaces,
49
+ :tenant,
50
+ :foobar
51
+ ]
52
+ end
53
+ end
54
+ ```
55
+
56
+ We can ensure that our `lease_includes` is valid by testing it:
57
+ ```ruby
58
+ describe LeasesController do
59
+ describe 'includes' do
60
+ it 'is valid' do
61
+ expect(EagerBeaver.new(Lease, LeasesController.lease_includes).errors).to be_empty
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ This expectation would fail, because `.errors` will return `['foobar is not an association of Lease']`.
68
+
69
+ EagerBeaver works with nested `includes` as well. Check out the specs to see more examples.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "eagerbeaver/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eagerbeaver"
8
+ spec.version = Eagerbeaver::VERSION
9
+ spec.authors = ["Danny Park"]
10
+ spec.email = ["dannypark92@gmail.com"]
11
+
12
+ spec.summary = "Validates associations listed in rails includes"
13
+ spec.description = "Validates associations listed in rails includes"
14
+ spec.homepage = "http://github.com/viewthespace/eagerbeaver"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency 'combustion', '~> 0.7.0'
27
+ spec.add_development_dependency 'activerecord'
28
+ spec.add_development_dependency 'pg'
29
+ spec.add_development_dependency "pry-byebug"
30
+ end
@@ -0,0 +1,36 @@
1
+ require "eagerbeaver/version"
2
+
3
+ class EagerBeaver
4
+ def initialize(model, preloads)
5
+ @model = model
6
+ @preloads = Array(preloads)
7
+ end
8
+
9
+ def errors
10
+ trace(model, preloads).flatten.compact
11
+ end
12
+
13
+ private
14
+
15
+ def trace(model, assoc)
16
+ model_string = model.to_s.singularize.classify
17
+
18
+ if assoc.is_a?(Array)
19
+ assoc.map { |a| trace(model, a) }
20
+ elsif assoc.is_a?(Hash)
21
+ val = Array(trace(association_name(model_string, assoc.keys.first), assoc.values.first))
22
+ val << trace(model, assoc.keys.first)
23
+ elsif assoc.is_a?(Symbol) && !model_string.constantize.reflect_on_all_associations.map(&:name).include?(assoc)
24
+ "#{assoc} is not an association of #{model_string}"
25
+ end
26
+ end
27
+
28
+ def association_name(model_string, association_name_or_alias)
29
+ association = model_string.constantize.reflect_on_all_associations.find { |a| a.name == association_name_or_alias }
30
+ association.try(:source_reflection_name) ||
31
+ association.try(:options).try(:[], :class_name) ||
32
+ association_name_or_alias
33
+ end
34
+
35
+ attr_reader :model, :preloads
36
+ end
@@ -0,0 +1,3 @@
1
+ module Eagerbeaver
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eagerbeaver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Park
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: combustion
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Validates associations listed in rails includes
98
+ email:
99
+ - dannypark92@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - eagerbeaver.gemspec
110
+ - lib/eagerbeaver.rb
111
+ - lib/eagerbeaver/version.rb
112
+ homepage: http://github.com/viewthespace/eagerbeaver
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.6.13
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Validates associations listed in rails includes
136
+ test_files: []