activerecord-autoscope 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 777a569281b2e5c3094260664c157e5dc686f0a0a66b100b01a69934bc3c2c3f
4
+ data.tar.gz: 74f54e9f786b70788e95d8dddece0e7fba1a87ba3bf6defd7fd596f78e31a6a7
5
+ SHA512:
6
+ metadata.gz: d60e542cd7d3ff042c8ca8eda7ce0f81e6d3d1e2fba25f622d228a2e6a6cb811015682ed3ee14c208d4c8fbe9f4dcee76cec796eec1d4deb4245fe57b4ee1898
7
+ data.tar.gz: a7e40c224bdf06159021eb944b67f8e3b002a9d6627f274c3c3cd949275375791779f39894e339d52e568a52a636dc14577f30d92a9bec069e7a023b5b0a774c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
+ AllCops:
12
+ TargetRubyVersion: 2.2
13
+
14
+ Metrics:
15
+ Enabled: false
16
+
17
+ Naming/FileName:
18
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in activerecord-autoscope.gemspec
6
+ gemspec
7
+
8
+ gem 'rubocop'
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-autoscope (0.1.0)
5
+ activerecord (>= 5.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.3)
11
+ activesupport (= 5.2.3)
12
+ activerecord (5.2.3)
13
+ activemodel (= 5.2.3)
14
+ activesupport (= 5.2.3)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.3)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ ast (2.4.0)
23
+ concurrent-ruby (1.1.5)
24
+ diff-lcs (1.3)
25
+ i18n (1.6.0)
26
+ concurrent-ruby (~> 1.0)
27
+ jaro_winkler (1.5.2)
28
+ minitest (5.11.3)
29
+ parallel (1.17.0)
30
+ parser (2.6.3.0)
31
+ ast (~> 2.4.0)
32
+ rainbow (3.0.0)
33
+ rake (10.5.0)
34
+ rspec (3.8.0)
35
+ rspec-core (~> 3.8.0)
36
+ rspec-expectations (~> 3.8.0)
37
+ rspec-mocks (~> 3.8.0)
38
+ rspec-core (3.8.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-expectations (3.8.3)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-mocks (3.8.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-support (3.8.0)
47
+ rubocop (0.68.1)
48
+ jaro_winkler (~> 1.5.1)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.5, != 2.5.1.1)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 1.4.0, < 1.6)
54
+ ruby-progressbar (1.10.0)
55
+ thread_safe (0.3.6)
56
+ tzinfo (1.2.5)
57
+ thread_safe (~> 0.1)
58
+ unicode-display_width (1.5.0)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord-autoscope!
65
+ bundler (~> 2.0)
66
+ rake (~> 10.0)
67
+ rspec (~> 3.0)
68
+ rubocop
69
+
70
+ BUNDLED WITH
71
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Yoshiyuki Hirano
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.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # ActiveRecord::AutoScope
2
+
3
+ This gem automatically defines scope methods which we often implement
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-autoscope'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-autoscope
20
+
21
+ ## Usage
22
+
23
+ This gem automatically defines these scope methods **without configuration**
24
+
25
+ These APIs are similar as [ransack](https://github.com/activerecord-hackery/ransack/)'s one, but these scope methods are simple implementations.
26
+
27
+ Please check its [implementation](https://github.com/yhirano55/activerecord-autoscope/blob/master/lib/activerecord/auto_scope/scope_methods.rb) if you're interested in.
28
+
29
+ | Type | Predicate | Description | Notes |
30
+ | ---- | --------- | ----------- | ----- |
31
+ | all | `*_eq` | equal | |
32
+ | all | `*_not_eq` | not equal | |
33
+ | all | `*_is` | equal | |
34
+ | all | `*_is_not` | not equal | |
35
+ | all | `*_null` | is null | |
36
+ | all | `*_not_null` | is not null | |
37
+ | all | `*_present` | not null and not empty | |
38
+ | all | `*_blank` | null or empty | |
39
+ | `:integer` | `*_gt` | greater than | |
40
+ | `:integer` | `*_gteq` | greater than or equal | |
41
+ | `:integer` | `*_lt` | less than | |
42
+ | `:integer` | `*_lteq` | less than or equal | |
43
+ | `:string`, `:text` | `*_start` | starts with | SQL: `col LIKE 'value%'` |
44
+ | `:string`, `:text` | `*_not_start` | does not start with | |
45
+ | `:string`, `:text` | `*_end` | ends with | SQL: `col LIKE '%value'` |
46
+ | `:string`, `:text` | `*_not_end` | does not end with | |
47
+ | `:string`, `:text` | `*_cont` | contains | SQL: `col LIKE '%value%'` |
48
+ | `:string`, `:text` | `*_not_cont` | does not contain | |
49
+ | `:date`, `:datetime` | `*_after` | after | |
50
+ | `:date`, `:datetime` | `*_on_or_after` | on or after | |
51
+ | `:date`, `:datetime` | `*_before` | before | |
52
+ | `:date`, `:datetime` | `*_on_or_before` | on or before | |
53
+ | `:date`, `:datetime` | `*_between` | between | |
54
+ | `:boolean` | `*` | true | SQL: `col IS true` |
55
+ | `:boolean` | `not_*` | false | |
56
+
57
+ ### Manually settings
58
+
59
+ If you want to define these scope methods manually, you have to add this initializer file:
60
+
61
+ ```ruby
62
+ # config/initializers/activerecord_autoscope.rb
63
+ ActiveRecord::AutoScope.config.auto_define_scopes = false
64
+ ```
65
+
66
+ Then you have to enable inside each models:
67
+
68
+ ```ruby
69
+ class User < ApplicationRecord
70
+ enable_auto_scopes!
71
+ end
72
+ ```
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('./lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'activerecord/auto_scope/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'activerecord-autoscope'
9
+ s.version = ActiveRecord::AutoScope::VERSION
10
+ s.authors = ['Yoshiyuki Hirano']
11
+ s.email = ['yhirano@me.com']
12
+ s.homepage = 'https://github.com/yhirano55/activerecord-autoscope'
13
+ s.summary = %(activerecord-autoscope automatically add useful scopes)
14
+ s.description = s.summary
15
+ s.license = 'MIT'
16
+ s.files = Dir.chdir(File.expand_path('.', __dir__)) do
17
+ `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ end
21
+
22
+ s.bindir = 'exe'
23
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ s.require_paths = ['lib']
25
+
26
+ s.required_ruby_version = '>= 2.2.2'
27
+ s.required_rubygems_version = '>= 1.8.11'
28
+
29
+ s.add_dependency 'activerecord', '>= 5.2'
30
+
31
+ s.add_development_dependency 'bundler', '~> 2.0'
32
+ s.add_development_dependency 'rake', '~> 10.0'
33
+ s.add_development_dependency 'rspec', '~> 3.0'
34
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'activerecord/autoscope'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'activerecord/auto_scope'
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require_relative 'auto_scope/config'
6
+ require_relative 'auto_scope/scope_methods'
7
+ require_relative 'auto_scope/version'
8
+
9
+ begin
10
+ require 'rails'
11
+ require_relative 'auto_scope/railtie'
12
+ rescue LoadError
13
+ nil
14
+ end
15
+
16
+ module ActiveRecord
17
+ module AutoScope # :nodoc:
18
+ def self.configure
19
+ yield config
20
+ end
21
+
22
+ def self.config
23
+ @config ||= Config.new
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module AutoScope
5
+ class Config # :nodoc:
6
+ attr_accessor :auto_define_scopes
7
+
8
+ def initialize
9
+ @auto_define_scopes = true
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module AutoScope
5
+ class Railtie < ::Rails::Railtie # :nodoc:
6
+ config.after_initialize do
7
+ ActiveSupport.on_load :active_record do
8
+ ActiveRecord::Base.extend ::ActiveRecord::AutoScope::ScopeMethods
9
+ end
10
+
11
+ if ::ActiveRecord::AutoScope.config.auto_define_scopes
12
+ TracePoint.trace(:end) do |event|
13
+ next if !event.self.ancestors.include?(ActiveRecord::Base) || event.self.abstract_class?
14
+
15
+ event.self.enable_auto_scopes!
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module AutoScope
5
+ module ScopeMethods # :nodoc:
6
+ def enable_auto_scopes!
7
+ columns.each do |column|
8
+ attr_name = column.name
9
+ table_name = column.table_name
10
+
11
+ scope("#{attr_name}_eq", ->(value) { where(attr_name => value) })
12
+ scope("#{attr_name}_not_eq", ->(value) { where.not(attr_name => value) })
13
+ scope("#{attr_name}_is", ->(value) { where(attr_name => value) })
14
+ scope("#{attr_name}_is_not", ->(value) { where.not(attr_name => value) })
15
+ scope("#{attr_name}_null", -> { where(attr_name => nil) })
16
+ scope("#{attr_name}_not_null", -> { where.not(attr_name => nil) })
17
+ scope("#{attr_name}_present", -> { where("#{table_name}.#{attr_name} IS NOT NULL AND #{table_name}.#{attr_name} != ''") })
18
+ scope("#{attr_name}_blank", -> { where("#{table_name}.#{attr_name} IS NULL OR #{table_name}.#{attr_name} = ''") })
19
+
20
+ case column.type
21
+ when :integer
22
+ scope("#{attr_name}_gt", ->(value) { where("#{table_name}.#{attr_name} > ?", value) })
23
+ scope("#{attr_name}_gteq", ->(value) { where("#{table_name}.#{attr_name} >= ?", value) })
24
+ scope("#{attr_name}_lt", ->(value) { where("#{table_name}.#{attr_name} < ?", value) })
25
+ scope("#{attr_name}_lteq", ->(value) { where("#{table_name}.#{attr_name} <= ?", value) })
26
+ when :string, :text
27
+ scope("#{attr_name}_start", ->(value) { where("#{table_name}.#{attr_name} LIKE ?", "#{sanitize_sql_like(value)}%") })
28
+ scope("#{attr_name}_not_start", ->(value) { where("#{table_name}.#{attr_name} NOT LIKE ?", "#{sanitize_sql_like(value)}%") })
29
+ scope("#{attr_name}_end", ->(value) { where("#{table_name}.#{attr_name} LIKE ?", "%#{sanitize_sql_like(value)}") })
30
+ scope("#{attr_name}_not_end", ->(value) { where("#{table_name}.#{attr_name} NOT LIKE ?", "%#{sanitize_sql_like(value)}") })
31
+ scope("#{attr_name}_cont", ->(value) { where("#{table_name}.#{attr_name} LIKE ?", "%#{sanitize_sql_like(value)}%") })
32
+ scope("#{attr_name}_not_cont", ->(value) { where("#{table_name}.#{attr_name} NOT LIKE ?", "%#{sanitize_sql_like(value)}%") })
33
+ when :date, :datetime
34
+ scope("#{attr_name}_after", ->(value) { where("#{table_name}.#{attr_name} > ?", value) })
35
+ scope("#{attr_name}_on_or_after", ->(value) { where("#{table_name}.#{attr_name} >= ?", value) })
36
+ scope("#{attr_name}_before", ->(value) { where("#{table_name}.#{attr_name} < ?", value) })
37
+ scope("#{attr_name}_on_or_before", ->(value) { where("#{table_name}.#{attr_name} <= ?", value) })
38
+ scope("#{attr_name}_between", ->(_value) { where(attr_name => format_value) })
39
+ when :boolean
40
+ scope(attr_name, -> { where(attr_name => true) })
41
+ scope("not_#{attr_name}", -> { where(attr_name => false) })
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module AutoScope
5
+ VERSION = '0.1.0'.freeze
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-autoscope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshiyuki Hirano
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-03 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
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'
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
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.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.0'
69
+ description: activerecord-autoscope automatically add useful scopes
70
+ email:
71
+ - yhirano@me.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - activerecord-autoscope.gemspec
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/activerecord-autoscope.rb
89
+ - lib/activerecord/auto_scope.rb
90
+ - lib/activerecord/auto_scope/config.rb
91
+ - lib/activerecord/auto_scope/railtie.rb
92
+ - lib/activerecord/auto_scope/scope_methods.rb
93
+ - lib/activerecord/auto_scope/version.rb
94
+ homepage: https://github.com/yhirano55/activerecord-autoscope
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.2.2
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 1.8.11
112
+ requirements: []
113
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: activerecord-autoscope automatically add useful scopes
117
+ test_files: []