scope_sql_counter 0.0.1
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 +7 -0
- data/.gitignore +51 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +55 -0
- data/README.md +122 -0
- data/bin/console +18 -0
- data/lib/scope_sql_counter.rb +13 -0
- data/lib/scope_sql_counter/orm/active_record.rb +18 -0
- data/lib/scope_sql_counter/syntax.rb +68 -0
- data/lib/scope_sql_counter/version.rb +3 -0
- data/scope_sql_counter.gemspec +32 -0
- data/spec/lib/syntax_spec.rb +53 -0
- data/spec/spec_helper.rb +103 -0
- data/spec/support/test_models.rb +34 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fae64a9faf210fc38ef1a60f4c51af02f0c953a968e2e2b125caeac3af5bb617
|
4
|
+
data.tar.gz: 823287434bc42de868d69ece9f6ab3302fba14acb2ffc96715e6782757b758af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d1c3562435a050ad97d63854a63d91eabdf58464039e842aaa1477c00a800fceb40d9056c788d598903102f4de26197210f5aad56aac039aeb9e463481f6e6e
|
7
|
+
data.tar.gz: 3e440d456f3f0710a1eff8087dc6f0479db14e36766a3756230d8750ade5d451e54edcb9d609db7b4e05969d4c57ca958c8fd79ef9274c79fdd15851096ec4b6
|
data/.gitignore
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ignore bundler config.
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore all logfiles and tempfiles.
|
11
|
+
/log/*
|
12
|
+
/tmp/*
|
13
|
+
!/log/.keep
|
14
|
+
!/tmp/.keep
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
.rvmrc
|
20
|
+
.rbenv*
|
21
|
+
.ruby-version
|
22
|
+
.ruby-gemset
|
23
|
+
.vimrc
|
24
|
+
.powrc
|
25
|
+
.DS_Store
|
26
|
+
/config/database.yml
|
27
|
+
/config/secrets.yml
|
28
|
+
/public/uploads/
|
29
|
+
/public/system/
|
30
|
+
/public/assets/
|
31
|
+
|
32
|
+
dump.rdb
|
33
|
+
/public/packs
|
34
|
+
/public/packs-test
|
35
|
+
/node_modules
|
36
|
+
yarn-debug.log*
|
37
|
+
.yarn-integrity
|
38
|
+
|
39
|
+
# Ignore local configuration
|
40
|
+
/config/local.rb
|
41
|
+
|
42
|
+
# Ignore editor-specific configuration
|
43
|
+
/.vscode
|
44
|
+
|
45
|
+
# Ignore uploaded files in development.
|
46
|
+
/storage/*
|
47
|
+
!/storage/.keep
|
48
|
+
|
49
|
+
# Ignore master key for decrypting credentials and more.
|
50
|
+
/config/master.key
|
51
|
+
*.gem
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
scope_sql_counter (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (5.2.3)
|
10
|
+
activesupport (= 5.2.3)
|
11
|
+
activerecord (5.2.3)
|
12
|
+
activemodel (= 5.2.3)
|
13
|
+
activesupport (= 5.2.3)
|
14
|
+
arel (>= 9.0)
|
15
|
+
activesupport (5.2.3)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
arel (9.0.0)
|
21
|
+
concurrent-ruby (1.1.5)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
i18n (1.6.0)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
minitest (5.11.3)
|
26
|
+
rake (12.3.3)
|
27
|
+
rspec (3.8.0)
|
28
|
+
rspec-core (~> 3.8.0)
|
29
|
+
rspec-expectations (~> 3.8.0)
|
30
|
+
rspec-mocks (~> 3.8.0)
|
31
|
+
rspec-core (3.8.2)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-expectations (3.8.4)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.8.0)
|
36
|
+
rspec-mocks (3.8.1)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.8.0)
|
39
|
+
rspec-support (3.8.2)
|
40
|
+
thread_safe (0.3.6)
|
41
|
+
tzinfo (1.2.5)
|
42
|
+
thread_safe (~> 0.1)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
activerecord (~> 5.2.3)
|
49
|
+
bundler (~> 2.0.2)
|
50
|
+
rake (~> 12.3.3)
|
51
|
+
rspec (~> 3.8.0)
|
52
|
+
scope_sql_counter!
|
53
|
+
|
54
|
+
BUNDLED WITH
|
55
|
+
2.0.2
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# Scope SQL Counter
|
2
|
+
An ActiveRecord extension that helps you count association using SQL.
|
3
|
+
|
4
|
+
Since the association counting was computed within a single query, it's at least better
|
5
|
+
than doing n+1 queries. The main idea is that you don't need to use counter cache library
|
6
|
+
that migrate new columns, use 3rd party app and stuff.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'scope_sql_counter'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
Unfortunately, this gem heavily relies on ActiveRecord. Well, since most
|
17
|
+
rails app use it, you are probably safe? Also, please make sure that all your
|
18
|
+
associations have indices in order to maximize the speed of your query.
|
19
|
+
|
20
|
+
### Add the scopes
|
21
|
+
So let's say your User model have a `has_many :blogs` association.
|
22
|
+
Use the ActiveRecord extension method to generate the scope:
|
23
|
+
```ruby
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
has_many :blogs, dependent: :destroy
|
26
|
+
|
27
|
+
# scope name # association name
|
28
|
+
scope_sql_counter :with_blog_count, :blogs
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
This will create a scope `User.with_blog_count` on your model. And if you call it:
|
33
|
+
```ruby
|
34
|
+
irb: User.with_blog_count
|
35
|
+
=> User Load (0.8ms)
|
36
|
+
SELECT users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id ) AS blogs_count
|
37
|
+
FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
|
38
|
+
```
|
39
|
+
|
40
|
+
As you can see, when the query executes, it sets an alias `AS blogs_count`.
|
41
|
+
This alias should be available as a readonly attribute on your
|
42
|
+
ActiveRecord model instance. For example:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
users = User.with_blog_count
|
46
|
+
user = users.first
|
47
|
+
|
48
|
+
user.blogs_count # => 8
|
49
|
+
```
|
50
|
+
|
51
|
+
## Multiple scopes
|
52
|
+
There are times you may want to fetch multiple counter on your associations.
|
53
|
+
You can achieve this by doing:
|
54
|
+
```ruby
|
55
|
+
class User < ActiveRecord::Base
|
56
|
+
has_many :blogs
|
57
|
+
has_many :comments
|
58
|
+
|
59
|
+
scope :with_multiple_count, -> {
|
60
|
+
select(ScopeSqlCounter.new(context: self, association_key: :blogs).call)
|
61
|
+
.select(ScopeSqlCounter.new(context: self, association_key: :comments).call)
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
# But.. it doesn't look good? Don't worry! We can make it cleaner:
|
66
|
+
|
67
|
+
class User < ActiveRecord::Base
|
68
|
+
scope_sql_counter :with_blog_count, :blogs
|
69
|
+
scope_sql_counter :with_comment_count, :comments
|
70
|
+
|
71
|
+
scope :with_multiple_count, -> { with_blog_count.with_comment_count }
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
### Additional configurations
|
76
|
+
`count_alias`: Sets the alias name for the counter instead of the default
|
77
|
+
|
78
|
+
`conditions`: Adds more condition on your scope counter instead of plain association call
|
79
|
+
|
80
|
+
1. `count_alias` . For example:
|
81
|
+
```ruby
|
82
|
+
scope_sql_counter :with_blog_count, :blogs, count_alias: :posts_count
|
83
|
+
```
|
84
|
+
```ruby
|
85
|
+
irb: users = User.with_blog_count
|
86
|
+
=> User Load (0.8ms)
|
87
|
+
SELECT users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id ) AS posts_count
|
88
|
+
FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
|
89
|
+
|
90
|
+
irb: users.first.posts_count # => 0
|
91
|
+
```
|
92
|
+
|
93
|
+
2. `conditions` . For example:
|
94
|
+
```ruby
|
95
|
+
scope_sql_counter :with_published_blog_count, :blogs,
|
96
|
+
conditions: 'blogs.published_at IS NOT NULL',
|
97
|
+
count_alias: :published_blog_count
|
98
|
+
```
|
99
|
+
```ruby
|
100
|
+
irb: users = User.with_blog_count
|
101
|
+
=> User Load (0.8ms)
|
102
|
+
SELECT users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id
|
103
|
+
AND blogs.published_at IS NOT NULL) AS published_blog_count
|
104
|
+
FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
|
105
|
+
|
106
|
+
irb: users.first.published_blog_count # => 0
|
107
|
+
```
|
108
|
+
|
109
|
+
### has_many :through and has_and_belongs_to_many
|
110
|
+
Dont' worry! It fully supports many to many relationships just fine!
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
1. Fork it
|
114
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
115
|
+
3. Add unit test
|
116
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
117
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
118
|
+
6. Create new Pull Request
|
119
|
+
|
120
|
+
|
121
|
+
## MIT
|
122
|
+
**scope_sql_counter** © 2019+, Harvey Ico. Released under the [MIT](http://mit-license.org/) License.
|
data/bin/console
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'irb'
|
5
|
+
require 'logger'
|
6
|
+
require 'active_record'
|
7
|
+
require 'scope_sql_counter'
|
8
|
+
|
9
|
+
class SimpleFormatter < Logger::Formatter
|
10
|
+
# This method is invoked when a log event occurs
|
11
|
+
def call(severity, timestamp, progname, msg)
|
12
|
+
"#{String === msg ? msg : msg.inspect}\n"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.logger ||= Logger.new(STDOUT, formatter: SimpleFormatter.new)
|
17
|
+
|
18
|
+
IRB.start
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'scope_sql_counter/syntax'
|
2
|
+
require 'scope_sql_counter/orm/active_record'
|
3
|
+
|
4
|
+
unless defined?(ActiveRecord)
|
5
|
+
class ActiveRecordDoesNotExist < StandardError; end
|
6
|
+
|
7
|
+
raise ActiveRecordDoesNotExist.new('Sorry, this gem only supports active record right now.')
|
8
|
+
end
|
9
|
+
|
10
|
+
module ScopeSqlCounter
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.extend(ScopeSqlCounter::Orm::ActiveRecord)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ScopeSqlCounter
|
2
|
+
module Orm
|
3
|
+
module ActiveRecord
|
4
|
+
def scope_sql_counter(scope_name, association_key, conditions: nil, count_alias: nil)
|
5
|
+
scope scope_name, -> {
|
6
|
+
syntax = ScopeSqlCounter::Syntax.new(
|
7
|
+
context: self,
|
8
|
+
association_key: association_key,
|
9
|
+
conditions: conditions,
|
10
|
+
count_alias: count_alias
|
11
|
+
)
|
12
|
+
|
13
|
+
select(syntax.call)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ScopeSqlCounter
|
4
|
+
class Syntax
|
5
|
+
COUNTER_SQL_QUERY = <<-SQL
|
6
|
+
(
|
7
|
+
SELECT COUNT(:target.id)
|
8
|
+
FROM :target
|
9
|
+
WHERE :target.:foreign_key = :context.id
|
10
|
+
:conditions
|
11
|
+
) AS :alias
|
12
|
+
SQL
|
13
|
+
|
14
|
+
attr_reader :context, :association_key, :conditions, :count_alias
|
15
|
+
|
16
|
+
def initialize(context:, association_key:, conditions: nil, count_alias: nil)
|
17
|
+
@context = context
|
18
|
+
@association_key = association_key
|
19
|
+
@conditions = conditions
|
20
|
+
@count_alias = count_alias
|
21
|
+
end
|
22
|
+
|
23
|
+
def call
|
24
|
+
if !context.respond_to?(:select_values) || context.select_values.blank?
|
25
|
+
["#{context.table_name}.*", query].join(', ')
|
26
|
+
else
|
27
|
+
query
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def association
|
34
|
+
reflection = context.reflect_on_association(association_key)
|
35
|
+
|
36
|
+
reflection.through_reflection || reflection
|
37
|
+
end
|
38
|
+
|
39
|
+
def association_table_name
|
40
|
+
if association.macro == :has_and_belongs_to_many
|
41
|
+
association.join_table
|
42
|
+
else
|
43
|
+
association.table_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def query
|
48
|
+
COUNTER_SQL_QUERY.gsub(':target', association_table_name)
|
49
|
+
.gsub(':foreign_key', association.foreign_key)
|
50
|
+
.gsub(':context', context.table_name)
|
51
|
+
.gsub(':conditions', conditions_sql)
|
52
|
+
.gsub(':alias', count_alias_sql)
|
53
|
+
.squish
|
54
|
+
end
|
55
|
+
|
56
|
+
def count_alias_sql
|
57
|
+
count_alias.to_s.presence || "#{association_key}_count"
|
58
|
+
end
|
59
|
+
|
60
|
+
def conditions_sql
|
61
|
+
String.new.tap do |str|
|
62
|
+
if conditions.present?
|
63
|
+
str << "AND #{conditions}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
|
7
|
+
require 'scope_sql_counter/version'
|
8
|
+
|
9
|
+
Gem::Specification.new do |s|
|
10
|
+
s.name = 'scope_sql_counter'
|
11
|
+
s.version = ScopeSqlCounter::VERSION
|
12
|
+
s.date = '2019-08-02'
|
13
|
+
s.summary = 'Don\'t want to use counter cache anymore? Achieve it with plain SQL query instead!'
|
14
|
+
s.description = <<-DESC
|
15
|
+
A gem that provides helper to achieve association counting without hassle.
|
16
|
+
It provides you readonly data inside your ActiveRecord model without executing n+1 queries!
|
17
|
+
DESC
|
18
|
+
s.authors = ['Harvey Ico']
|
19
|
+
s.email = 'godstrikerharvey@gmail.com'
|
20
|
+
s.files = `git ls-files`.split($/)
|
21
|
+
s.homepage = 'https://github.com/harveyico/scope_sql_counter'
|
22
|
+
s.license = 'MIT'
|
23
|
+
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
|
26
|
+
s.required_ruby_version = '>= 2.1.0'
|
27
|
+
|
28
|
+
s.add_development_dependency 'activerecord', '~> 5.2.3'
|
29
|
+
s.add_development_dependency 'bundler', '~> 2.0.2'
|
30
|
+
s.add_development_dependency 'rake', '~> 12.3.3'
|
31
|
+
s.add_development_dependency 'rspec', '~> 3.8.0'
|
32
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/test_models'
|
3
|
+
|
4
|
+
describe ScopeSqlCounter::Syntax do
|
5
|
+
let!(:count_alias) { nil }
|
6
|
+
let!(:conditions) { nil }
|
7
|
+
let!(:arguments) do
|
8
|
+
{
|
9
|
+
context: UserTest,
|
10
|
+
association_key: :blogs,
|
11
|
+
count_alias: count_alias,
|
12
|
+
conditions: conditions
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
let!(:sql) do
|
17
|
+
ScopeSqlCounter::Syntax.new(arguments)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#call' do
|
21
|
+
context 'when model has no selected_values' do
|
22
|
+
it 'returns all current table attributes and add the SQL counter' do
|
23
|
+
expect(sql.call).to eq('users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id ) AS blogs_count')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when model has select_values' do
|
28
|
+
before do
|
29
|
+
allow(UserTest).to receive(:select_values).and_return('users.*')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'only returns the SQL counter' do
|
33
|
+
expect(sql.call).to eq('( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id ) AS blogs_count')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'if count_alias is passed' do
|
38
|
+
let!(:count_alias) { 'random_count' }
|
39
|
+
|
40
|
+
it 'sets the alias based on the text' do
|
41
|
+
expect(sql.call).to eq('users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id ) AS random_count')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'if conditions is passed' do
|
46
|
+
let!(:conditions) { 'blogs.created_at IS NULL' }
|
47
|
+
|
48
|
+
it 'adds the condition on your scope counter' do
|
49
|
+
expect(sql.call).to eq('users.*, ( SELECT COUNT(blogs.id) FROM blogs WHERE blogs.user_id = users.id AND blogs.created_at IS NULL ) AS blogs_count')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
require 'active_record'
|
17
|
+
require 'scope_sql_counter'
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
44
|
+
# have no way to turn it off -- the option exists only for backwards
|
45
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
46
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
47
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
48
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
49
|
+
|
50
|
+
# The settings below are suggested to provide a good initial experience
|
51
|
+
# with RSpec, but feel free to customize to your heart's content.
|
52
|
+
=begin
|
53
|
+
# This allows you to limit a spec run to individual examples or groups
|
54
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
55
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
56
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
57
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
58
|
+
config.filter_run_when_matching :focus
|
59
|
+
|
60
|
+
# Allows RSpec to persist some state between runs in order to support
|
61
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
62
|
+
# you configure your source control system to ignore this file.
|
63
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
64
|
+
|
65
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
66
|
+
# recommended. For more details, see:
|
67
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
68
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
69
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
70
|
+
config.disable_monkey_patching!
|
71
|
+
|
72
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
73
|
+
# be too noisy due to issues in dependencies.
|
74
|
+
config.warnings = true
|
75
|
+
|
76
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
77
|
+
# file, and it's useful to allow more verbose output when running an
|
78
|
+
# individual spec file.
|
79
|
+
if config.files_to_run.one?
|
80
|
+
# Use the documentation formatter for detailed output,
|
81
|
+
# unless a formatter has already been configured
|
82
|
+
# (e.g. via a command-line flag).
|
83
|
+
config.default_formatter = "doc"
|
84
|
+
end
|
85
|
+
|
86
|
+
# Print the 10 slowest examples and example groups at the
|
87
|
+
# end of the spec run, to help surface which specs are running
|
88
|
+
# particularly slow.
|
89
|
+
config.profile_examples = 10
|
90
|
+
|
91
|
+
# Run specs in random order to surface order dependencies. If you find an
|
92
|
+
# order dependency and want to debug it, you can fix the order by providing
|
93
|
+
# the seed, which is printed after each run.
|
94
|
+
# --seed 1234
|
95
|
+
config.order = :random
|
96
|
+
|
97
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
98
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
99
|
+
# test failures related to randomization by passing the same `--seed` value
|
100
|
+
# as the one that triggered the failure.
|
101
|
+
Kernel.srand config.seed
|
102
|
+
=end
|
103
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class BlogTest
|
2
|
+
class << self
|
3
|
+
def table_name
|
4
|
+
'blogs'
|
5
|
+
end
|
6
|
+
|
7
|
+
def foreign_key
|
8
|
+
'user_id'
|
9
|
+
end
|
10
|
+
|
11
|
+
def macro
|
12
|
+
:has_many
|
13
|
+
end
|
14
|
+
|
15
|
+
def through_reflection
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class UserTest
|
21
|
+
class << self
|
22
|
+
def table_name
|
23
|
+
'users'
|
24
|
+
end
|
25
|
+
|
26
|
+
def reflect_on_association(key)
|
27
|
+
"#{key.to_s.singularize.capitalize}Test".constantize
|
28
|
+
end
|
29
|
+
|
30
|
+
def select_values
|
31
|
+
[]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scope_sql_counter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harvey Ico
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 12.3.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.3.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.8.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.8.0
|
69
|
+
description: |2
|
70
|
+
A gem that provides helper to achieve association counting without hassle.
|
71
|
+
It provides you readonly data inside your ActiveRecord model without executing n+1 queries!
|
72
|
+
email: godstrikerharvey@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- README.md
|
82
|
+
- bin/console
|
83
|
+
- lib/scope_sql_counter.rb
|
84
|
+
- lib/scope_sql_counter/orm/active_record.rb
|
85
|
+
- lib/scope_sql_counter/syntax.rb
|
86
|
+
- lib/scope_sql_counter/version.rb
|
87
|
+
- scope_sql_counter.gemspec
|
88
|
+
- spec/lib/syntax_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/support/test_models.rb
|
91
|
+
homepage: https://github.com/harveyico/scope_sql_counter
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 2.1.0
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubygems_version: 3.0.4
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Don't want to use counter cache anymore? Achieve it with plain SQL query
|
114
|
+
instead!
|
115
|
+
test_files: []
|