ams_lazy_relationships 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 250cb8632ee5305b7f64e9c405d234f4314efd44
4
+ data.tar.gz: 13a9171c97c9404bf6bf999d089f65bafcc0240d
5
+ SHA512:
6
+ metadata.gz: c43418b6e95a06f7e5e1ca453004792b2b42f10a02a97f4f07b48304c02b0792ee706b14a548a4d97c95c32ff459e4884ebda4fec5dc3cb842f6cff05e565862
7
+ data.tar.gz: 1b574d51889e7ba67bce43b433e94d9da706f47d38008bbe81f804844380a8fd2f3231a467edb8705dc534215ce42439abcedc20b983887226122c26f40f2d8f
@@ -0,0 +1,31 @@
1
+ # This is a sample .codeclimate.yml configured for Plugin analysis on Code
2
+ # Climate Platform. For an overview of the Code Climate Platform, see here:
3
+ # https://codeclimate.com/blog/code-climate-platform/
4
+
5
+ # Under the plugins key, you can configure which plugins will analyze your repo.
6
+ # Each key is a plugin name. For each value, you need to specify enabled: true
7
+ # to enable the plugin as well as any other plugins-specific configuration.
8
+
9
+ # For more details, see here:
10
+ # http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
11
+
12
+ version: "2"
13
+
14
+ # For a list of all available plugins, see here:
15
+ # https://docs.codeclimate.com/docs/list-of-engines
16
+
17
+ plugins:
18
+ # to turn on a plugin, add it here and set enabled to `true`
19
+ # to turn off a plugin, set enabled to `false` or remove it
20
+ rubocop:
21
+ enabled: true
22
+ channel: rubocop-0-51
23
+ checks:
24
+ Rubocop/Metrics/LineLength:
25
+ enabled: false
26
+ bundler-audit:
27
+ enabled: true
28
+ duplication:
29
+ enabled: true
30
+ exclude_patterns:
31
+ - spec
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
14
+ gemfiles/*.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,37 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - db/schema.rb
6
+ - Gemfile
7
+ TargetRubyVersion: 2.3
8
+ TargetRailsVersion: 4.2
9
+
10
+ Metrics/ClassLength:
11
+ Max: 300
12
+
13
+ Metrics/MethodLength:
14
+ Enabled: false
15
+
16
+ Metrics/ParameterLists:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Enabled: false
21
+
22
+ Layout/AccessModifierIndentation:
23
+ Enabled: false
24
+
25
+ Style/ClassAndModuleChildren:
26
+ Enabled: false
27
+
28
+ Style/Documentation:
29
+ Enabled: false
30
+
31
+ Layout/DotPosition:
32
+ Enabled: true
33
+ EnforcedStyle: trailing
34
+
35
+ Style/StringLiterals:
36
+ Enabled: true
37
+ EnforcedStyle: double_quotes
@@ -0,0 +1,25 @@
1
+ ---
2
+ sudo: false
3
+ env:
4
+ global:
5
+ - CC_TEST_REPORTER_ID=1da1af59767d8c64ae960d5a0ac69089a0fa90e013aedb09e76843f104bec709
6
+ language: ruby
7
+ gemfile:
8
+ - gemfiles/ams_0.10.0.rc4.gemfile
9
+ - gemfiles/ams_0.10.2.gemfile
10
+ - gemfiles/ams_0.10.8.gemfile
11
+ cache: bundler
12
+ rvm:
13
+ - 2.3.4
14
+ before_install:
15
+ - gem install bundler undercover --no-doc
16
+ before_script:
17
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
18
+ - chmod +x ./cc-test-reporter
19
+ - ./cc-test-reporter before-build
20
+ script:
21
+ - bundle exec rake
22
+ - git pull origin master:master
23
+ - undercover --compare master
24
+ after_script:
25
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -0,0 +1,11 @@
1
+ appraise "ams-0.10.0.rc4" do
2
+ gem "active_model_serializers", "0.10.0.rc4"
3
+ end
4
+
5
+ appraise "ams-0.10.2" do
6
+ gem "active_model_serializers", "0.10.2"
7
+ end
8
+
9
+ appraise "ams-0.10.8" do
10
+ gem "active_model_serializers", "0.10.8"
11
+ end
@@ -0,0 +1,24 @@
1
+ # Change Log
2
+
3
+ ## [0.1.0](https://github.com/Bajena/ams_lazy_relationships/tree/0.1.0) (2018-12-30)
4
+ **Closed issues:**
5
+
6
+ - Add changelog [\#10](https://github.com/Bajena/ams_lazy_relationships/issues/10)
7
+ - Prepare initial version + test suite [\#2](https://github.com/Bajena/ams_lazy_relationships/issues/2)
8
+ - Test multiple AMS versions [\#1](https://github.com/Bajena/ams_lazy_relationships/issues/1)
9
+
10
+ **Merged pull requests:**
11
+
12
+ - Add undercover back [\#12](https://github.com/Bajena/ams_lazy_relationships/pull/12) ([Bajena](https://github.com/Bajena))
13
+ - Add changelog [\#11](https://github.com/Bajena/ams_lazy_relationships/pull/11) ([Bajena](https://github.com/Bajena))
14
+ - Split methods logically, add yard comments and hide unnecessary public methods [\#9](https://github.com/Bajena/ams_lazy_relationships/pull/9) ([Bajena](https://github.com/Bajena))
15
+ - Code cleanup [\#8](https://github.com/Bajena/ams_lazy_relationships/pull/8) ([Bajena](https://github.com/Bajena))
16
+ - Add tests for JSON adapter and improve backwards compatibility [\#7](https://github.com/Bajena/ams_lazy_relationships/pull/7) ([Bajena](https://github.com/Bajena))
17
+ - Use Appraisal to test different versions of AMS [\#6](https://github.com/Bajena/ams_lazy_relationships/pull/6) ([Bajena](https://github.com/Bajena))
18
+ - Add core module [\#5](https://github.com/Bajena/ams_lazy_relationships/pull/5) ([Bajena](https://github.com/Bajena))
19
+ - Add Loader classes [\#4](https://github.com/Bajena/ams_lazy_relationships/pull/4) ([Bajena](https://github.com/Bajena))
20
+ - Initial setup [\#3](https://github.com/Bajena/ams_lazy_relationships/pull/3) ([Bajena](https://github.com/Bajena))
21
+
22
+
23
+
24
+ \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ams_lazy_relationships.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jan Bajena
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jan Bajena
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,108 @@
1
+ [![Build Status](https://travis-ci.org/Bajena/ams_lazy_relationships.svg?branch=master)](https://travis-ci.org/Bajena/ams_lazy_relationships)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/c21b988e09db63396309/maintainability)](https://codeclimate.com/github/Bajena/ams_lazy_relationships/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/c21b988e09db63396309/test_coverage)](https://codeclimate.com/github/Bajena/ams_lazy_relationships/test_coverage)
4
+
5
+ # AmsLazyRelationships
6
+
7
+ #### What does the gem do?
8
+ Eliminates N+1 queries problem in [Active Model Serializers gem](https://github.com/rails-api/active_model_serializers) thanks to batch loading provided by a great [BatchLoader gem](https://github.com/exAspArk/batch-loader).
9
+
10
+ The gem provides a module which defines a set of methods useful for eliminating N+1 query problem
11
+ during the serialization. Serializers will first prepare a tree of "promises"
12
+ for every nested lazy relationship. The relationship promises will be
13
+ evaluated only when they're requested.
14
+ E.g. when including `blog_posts.user`: instead of loading a user for each blog post separately it'll gather the blog posts and load all their users at once when including the users in the response.
15
+
16
+ #### How is it better than Rails' includes/joins methods?
17
+ In many cases it's fine to use [`includes`](https://apidock.com/rails/ActiveRecord/QueryMethods/includes) method provided by Rails.
18
+ There are a few problems with `includes` approach though:
19
+ - It loads all the records provided in the arguments hash. Often you may not need all the nested records to serialize the data you want. `AmsLazyRelationships` will load only the data you need thanks to lazy evaluation.
20
+ - When the app gets bigger and bigger you'd need to update all the `includes` statements across your app to prevent the N+1 queries problem which quickly becomes impossible.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem "ams_lazy_relationships"
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ ## Installation
35
+
36
+ Include `AmsLazyRelationships::Core` module in your base serializer
37
+
38
+ ```ruby
39
+ class BaseSerializer < ActiveModel::Serializer
40
+ include AmsLazyRelationships::Core
41
+ end
42
+ ```
43
+
44
+ **Important:**
45
+ This gem uses `BatchLoader` heavily. I highly recommend to clear the batch loader's cache between HTTP requests.
46
+ To do so add a following middleware:
47
+ `use BatchLoader::Middleware`
48
+
49
+ For more info about the middleware check out BatchLoader gem docs: https://github.com/exAspArk/batch-loader#caching
50
+
51
+ ### Usage
52
+ Adding the `AmsLazyRelationships::Core` module lets you define lazy relationships in your serializers:
53
+ ```ruby
54
+
55
+ class UserSerializer < BaseSerializer
56
+ # Short version - preloads a specified ActiveRecord relationships by default
57
+ lazy_has_many :blog_posts
58
+
59
+ # Works same as the previous one, but the loader option is specified explicitly
60
+ lazy_has_many :blog_posts,
61
+ serializer: BlogPostSerializer,
62
+ loader: AmsLazyRelationships::Loaders::Association.new("User", :blog_posts)
63
+
64
+ # The previous one is a shorthand for the following lines:
65
+ lazy_relationship :blog_posts, loader: AmsLazyRelationships::Loaders::Association.new("User", :blog_posts)
66
+ has_many :blog_posts, serializer: BlogPostSerializer do
67
+ lazy_blog_posts
68
+ end
69
+
70
+ lazy_has_one :poro_model, loader: AmsLazyRelationships::Loaders::Direct.new(:poro_model) { |object| PoroModel.new(object) }
71
+
72
+ lazy_belongs_to :account, loader: AmsLazyRelationships::Loaders::SimpleBelongsTo.new("Account")
73
+
74
+ lazy_has_many :comment, loader: AmsLazyRelationships::Loaders::SimpleHasMany.new("Comment", foreign_key: :user_id)
75
+
76
+ ```
77
+
78
+ As you may have already noticed the gem makes use of various loader classes.
79
+
80
+ I've implemented the following ones for you:
81
+ - `AmsLazyRelationships::Loaders::Association` - Batch loads a ActiveRecord association (has_one/has_many/has_many-through/belongs_to). This is a deafult loader in case you don't specify a `loader` option in your serializer's lazy relationship.
82
+ E.g. in order to lazy load user's blog posts use a following loader: `AmsLazyRelationships::Loaders::Association.new("User", :blog_posts)`.
83
+
84
+ - `AmsLazyRelationships::Loaders::SimpleBelongsTo` - Batch loads ActiveRecord models using a foreign key method called on a serialized object. E.g. `AmsLazyRelationships::Loaders::SimpleBelongsTo.new("Account")` called on users will gather their `account_id`s and fire one query to get all accounts at once instead of loading an account per user separately.
85
+ This loader can be useful e.g. when the serialized object is not an ActiveRecord model.
86
+
87
+ - `AmsLazyRelationships::Loaders::SimpleHasMany` - Batch loads ActiveRecord records belonging to given record by foreign key. E.g. `AmsLazyRelationships::Loaders::SimpleHasMany.new("BlogPosts", foreign_key: :user_id)` called on users will and fire one query to gather all blog posts for the users at once instead of loading an the blog posts per user separately.
88
+ This loader can be useful e.g. when the serialized object is not an ActiveRecord model.
89
+
90
+ - `AmsLazyRelationships::Loaders::Direct` - Lazy loads data in a "dumb" way - just executes the provided block when needed. Useful e.g. when the relationship is just a PORO which then in its own serializer needs to lazy load some relationships.
91
+ You can use it like this: `AmsLazyRelationships::Loaders::Direct.new(:poro_model) { |object| PoroModel.new(object)`.
92
+
93
+ The abovementioned loaders are mostly useful when using ActiveRecord, but there should be no problem building a new loader for different frameworks.
94
+ If you're missing a loader you can create an issue or create your own loader taking the existing ones as an example.
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Bajena/ams_lazy_relationships.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "github_changelog_generator/task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
10
+
11
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
12
+ config.user = "Bajena"
13
+ config.project = "ams_lazy_relationships"
14
+ config.future_release = "0.1.0"
15
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "ams_lazy_relationships/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ams_lazy_relationships"
9
+ spec.version = AmsLazyRelationships::VERSION
10
+ spec.authors = ["Jan Bajena"]
11
+
12
+ spec.summary = "ActiveModel Serializers addon for eliminating N+1 queries problem from the serializers."
13
+ spec.homepage = "https://github.com/Bajena/ams_lazy_relationships"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/Bajena/ams_lazy_relationships"
21
+ spec.metadata["changelog_uri"] = "https://github.com/Bajena/ams_lazy_relationships/CHANGELOG.md"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "active_model_serializers"
37
+ spec.add_dependency "batch-loader", "=1.2.2"
38
+
39
+ spec.add_development_dependency "activerecord"
40
+ # A Ruby library for testing against different versions of dependencies
41
+ spec.add_development_dependency "appraisal"
42
+ spec.add_development_dependency "bundler", "~> 1.17"
43
+ # Rspec matchers for SQL query counts
44
+ spec.add_development_dependency "db-query-matchers"
45
+ spec.add_development_dependency "github_changelog_generator"
46
+ spec.add_development_dependency "pry"
47
+ spec.add_development_dependency "pry-nav"
48
+ spec.add_development_dependency "rake", "~> 10.0"
49
+ spec.add_development_dependency "rspec", "~> 3.0"
50
+ spec.add_development_dependency "rspec-rails", "~> 3.5"
51
+ spec.add_development_dependency "rubocop", "= 0.61.0"
52
+ spec.add_development_dependency "rubocop-rspec", "= 1.20.1"
53
+ spec.add_development_dependency "simplecov"
54
+ spec.add_development_dependency "simplecov-lcov"
55
+ spec.add_development_dependency "sqlite3", "~> 1.3"
56
+ # Detect untested code blocks in recent changes
57
+ spec.add_development_dependency "undercover"
58
+ # Dynamically build an Active Record model (with table) within a test context
59
+ spec.add_development_dependency "with_model", "~> 2.0"
60
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "bundler/setup"
6
+ require "ams_lazy_relationships"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "active_model_serializers", "0.10.0.rc4"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "active_model_serializers", "0.10.2"
6
+
7
+ gemspec path: "../"