rails_scopy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8eba4ca1306ea319ae7a383f2ab5d94253b86efa80e5856b91f171e14a9ad33b
4
+ data.tar.gz: 0ef250f4f325879b03b4262007d68587d5ccf2d0bfb50efd4a78ecf95692c906
5
+ SHA512:
6
+ metadata.gz: 04e594bbd1b428bb53b81f01bca1c71475d2138b7186cd287a0f442a79d608b1185f743926f94ba58f820db6cff119ad8df92a83ad7f59cb50e6c6498a932c4c
7
+ data.tar.gz: e5823686f0831ef31920cea5586f4cbcbc53346ce93598bd1cc64e0b27c33aec9508fcb3cb443f3bb1910c2ee7e073cf0cc7d9041b562e31c3bf538b2640c123
data/.gitignore ADDED
@@ -0,0 +1,24 @@
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
+ # IDE
14
+ /.idea
15
+ .idea/workspace.xml
16
+ .rakeTasks
17
+ .generators
18
+
19
+ # Gem
20
+ *.gem
21
+
22
+ # Logs
23
+ spec/dummy/log/*.log
24
+ spec/dummy/tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ Documentation:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ IgnoredPatterns: ['(\A|\s)#']
5
+ Max: 120
6
+ Layout/EmptyLineAfterGuardClause:
7
+ Enabled: false
8
+ Layout/EmptyLinesAroundBlockBody:
9
+ Enabled: false
10
+ Style/DoubleNegation:
11
+ Enabled: false
12
+ Metrics/MethodLength:
13
+ Max: 30
14
+ Metrics/BlockLength:
15
+ Max: 30
16
+ Metrics/AbcSize:
17
+ Max: 30
18
+ Metrics/CylocmaticComplexity:
19
+ Max: 10
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ ---
2
+ os: linux
3
+ language: ruby
4
+ cache: bundler
5
+ services:
6
+ - postgresql
7
+ rvm:
8
+ - 2.5.5
9
+ - 2.6.3
10
+ gemfile:
11
+ - gemfiles/Gemfile-4-2
12
+ - gemfiles/Gemfile-5-2
13
+ - gemfiles/Gemfile-6-0
14
+ before_install: gem install bundler -v 2.1.0
15
+ before_script:
16
+ - bundle install
17
+ - psql -c 'create database rails_scopy;' -U postgres
18
+ script: RAILS_ENV=test bundle exec rspec
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 rails_scopy.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_scopy (0.1.0)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.2.1)
11
+ activesupport (= 6.0.2.1)
12
+ activerecord (6.0.2.1)
13
+ activemodel (= 6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ activesupport (6.0.2.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
+ concurrent-ruby (1.1.6)
22
+ coveralls (0.8.23)
23
+ json (>= 1.8, < 3)
24
+ simplecov (~> 0.16.1)
25
+ term-ansicolor (~> 1.3)
26
+ thor (>= 0.19.4, < 2.0)
27
+ tins (~> 1.6)
28
+ diff-lcs (1.3)
29
+ docile (1.3.2)
30
+ faker (2.10.2)
31
+ i18n (>= 1.6, < 2)
32
+ i18n (1.8.2)
33
+ concurrent-ruby (~> 1.0)
34
+ json (2.3.0)
35
+ minitest (5.14.0)
36
+ pg (1.2.2)
37
+ rake (10.5.0)
38
+ rspec (3.9.0)
39
+ rspec-core (~> 3.9.0)
40
+ rspec-expectations (~> 3.9.0)
41
+ rspec-mocks (~> 3.9.0)
42
+ rspec-core (3.9.1)
43
+ rspec-support (~> 3.9.1)
44
+ rspec-expectations (3.9.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.9.0)
47
+ rspec-mocks (3.9.1)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.9.0)
50
+ rspec-support (3.9.2)
51
+ simplecov (0.16.1)
52
+ docile (~> 1.1)
53
+ json (>= 1.8, < 3)
54
+ simplecov-html (~> 0.10.0)
55
+ simplecov-html (0.10.2)
56
+ sync (0.5.0)
57
+ term-ansicolor (1.7.1)
58
+ tins (~> 1.0)
59
+ thor (1.0.1)
60
+ thread_safe (0.3.6)
61
+ tins (1.24.1)
62
+ sync
63
+ tzinfo (1.2.6)
64
+ thread_safe (~> 0.1)
65
+ zeitwerk (2.2.2)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 2.0)
72
+ coveralls (~> 0.8)
73
+ faker (~> 2.10)
74
+ pg (~> 1.2)
75
+ rails_scopy!
76
+ rake (~> 10.0)
77
+ rspec (~> 3.0)
78
+ simplecov (~> 0.16)
79
+
80
+ BUNDLED WITH
81
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 OpenGems
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,31 @@
1
+ # RailsScopy
2
+
3
+ Generate automatically scopes in your model (PostgreSQL, SQLite, MySQL, etc...)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails_scopy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails_scopy
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OpenGems/rails_scopy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
28
+
29
+ ## License
30
+
31
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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 'rails_scopy'
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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsScopy
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ module RailsScopy
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ base.perform
9
+ end
10
+
11
+ module ClassMethods
12
+ def perform
13
+ return if abstract_class?
14
+ columns.each do |column|
15
+ name = column.name
16
+ perform_order(name)
17
+ perform_base(name)
18
+ method = "perform_#{column.type}"
19
+ valid = respond_to?(method, :include_private)
20
+ send(method, name) if valid
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def perform_order(name)
27
+ scope :"ascend_by_#{name}", -> { order(name => :asc) }
28
+ scope :"descend_by_#{name}", -> { order(name => :desc) }
29
+ end
30
+
31
+ def perform_base(name)
32
+ scope("#{name}_eq", ->(value) { where(name => value) })
33
+ scope("#{name}_not_eq", ->(value) { where.not(name => value) })
34
+ scope("#{name}_null", -> { where(name => nil) })
35
+ scope("#{name}_not_null", -> { where.not(name => nil) })
36
+ scope("#{name}_present", -> { where("#{name} IS NOT NULL AND #{name} != ''") })
37
+ scope("#{name}_blank", -> { where("#{name} IS NULL OR #{name} = ''") })
38
+ end
39
+
40
+ def perform_numeric(name)
41
+ scope :"#{name}_to", ->(value) { where("#{name} <= ?", value) }
42
+ scope :"#{name}_from", ->(value) { where("#{name} >= ?", value) }
43
+ scope :"#{name}_above", ->(value) { where("#{name} > ?", value) }
44
+ scope :"#{name}_below", ->(value) { where("#{name} < ?", value) }
45
+ scope :"#{name}_between", ->(from, to) { where("#{name} BETWEEN ? AND ?", from, to) }
46
+ end
47
+
48
+ def perform_integer(name)
49
+ perform_numeric(name)
50
+ end
51
+
52
+ def perform_float(name)
53
+ perform_numeric(name)
54
+ scope :"#{name}_scale", ->(value) { where("SCALE(#{name}) = ?", value) }
55
+ end
56
+
57
+ def perform_string(name)
58
+ scope :"#{name}_contains", ->(value) { where("#{name} LIKE ?", "%#{sanitize_sql_like(value)}%") }
59
+ scope :"#{name}_not_contains", ->(value) { where("#{name} NOT LIKE ?", "%#{sanitize_sql_like(value)}%") }
60
+ scope :"#{name}_starts_with", ->(value) { where("#{name} LIKE ?", "#{sanitize_sql_like(value)}%") }
61
+ scope :"#{name}_not_starts_with", ->(value) { where("#{name} NOT LIKE ?", "#{sanitize_sql_like(value)}%") }
62
+ scope :"#{name}_ends_with", ->(value) { where("#{name} LIKE ?", "%#{sanitize_sql_like(value)}") }
63
+ scope :"#{name}_not_ends_with", ->(value) { where("#{name} NOT LIKE ?", "%#{sanitize_sql_like(value)}") }
64
+ scope :"#{name}_length", ->(value) { where("LENGTH(#{name}) = ?", value) }
65
+ scope :"#{name}_between_length", ->(from, to) { where("LENGTH(#{name}) BETWEEN ? AND ?", from, to) }
66
+ end
67
+ alias_method :perform_text, :perform_string
68
+
69
+ def perform_datetime(name)
70
+ scope :"#{name}_to", ->(value) { where("#{name} <= ?", value) }
71
+ scope :"#{name}_from", ->(value) { where("#{name} >= ?", value) }
72
+ scope :"#{name}_after", ->(value) { where("#{name} > ?", value) }
73
+ scope :"#{name}_before", ->(value) { where("#{name} < ?", value) }
74
+ scope :"#{name}_between", ->(from, to) { where("#{name} BETWEEN ? AND ?", from, to) }
75
+ end
76
+ alias_method :perform_date, :perform_datetime
77
+ alias_method :perform_time, :perform_datetime
78
+
79
+ def perform_boolean(name)
80
+ scope :"not_#{name}", -> { where(name => false) }
81
+ scope :"#{name}", -> { where(name => true) }
82
+ end
83
+
84
+ def perform_json(name)
85
+ scope :"#{name}_has_key", ->(value) { where("(#{name} #>'{#{value}}') IS NOT NULL") }
86
+ scope :"#{name}_has_not_key", ->(value) { where("(#{name} #>'{#{value}}') IS NULL") }
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,38 @@
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 'rails_scopy/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rails_scopy'
9
+ spec.version = RailsScopy::VERSION
10
+ spec.authors = ['Boris BRESCIANI']
11
+ spec.email = ['boris2bresciani@gmail.com']
12
+
13
+ spec.summary = 'Generate automatically scopes in your model'
14
+ spec.description = 'Generate automatically scopes in your model (PostgreSQL, SQLite, MySQL, etc...)'
15
+ spec.homepage = 'https://github.com/OpenGems/rails_scopy'
16
+ spec.license = 'MIT'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0")
23
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activerecord', '>= 4.2', '< 7'
30
+
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
+ spec.add_development_dependency 'coveralls', '~> 0.8'
33
+ spec.add_development_dependency 'faker', '~> 2.10'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'simplecov', '~> 0.16'
37
+ spec.add_development_dependency 'pg', '~> 1.2'
38
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_scopy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Boris BRESCIANI
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-27 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: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: coveralls
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.8'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.8'
61
+ - !ruby/object:Gem::Dependency
62
+ name: faker
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.10'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.10'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.16'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.16'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pg
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '1.2'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '1.2'
131
+ description: Generate automatically scopes in your model (PostgreSQL, SQLite, MySQL,
132
+ etc...)
133
+ email:
134
+ - boris2bresciani@gmail.com
135
+ executables: []
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - ".gitignore"
140
+ - ".rspec"
141
+ - ".rubocop.yml"
142
+ - ".travis.yml"
143
+ - Gemfile
144
+ - Gemfile.lock
145
+ - LICENSE.txt
146
+ - README.md
147
+ - Rakefile
148
+ - bin/console
149
+ - bin/setup
150
+ - lib/rails_scopy.rb
151
+ - lib/rails_scopy/version.rb
152
+ - rails_scopy.gemspec
153
+ homepage: https://github.com/OpenGems/rails_scopy
154
+ licenses:
155
+ - MIT
156
+ metadata:
157
+ homepage_uri: https://github.com/OpenGems/rails_scopy
158
+ source_code_uri: https://github.com/OpenGems/rails_scopy
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubygems_version: 3.1.2
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Generate automatically scopes in your model
178
+ test_files: []