type_scopes 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +17 -17
- data/README.md +8 -2
- data/lib/type_scopes/string.rb +6 -16
- data/lib/type_scopes/version.rb +1 -1
- data/lib/type_scopes.rb +2 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47e7400e39752e376e84d65854ac9eb4130505a6ee311aad99c7d5c132dceb02
|
4
|
+
data.tar.gz: 7a6d6e7d9dbe5d0c07771f175084c0727d3733fcf3ddafa3cd20b8cc50f3b94a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f8d0cb2bf88623c23ac42f3bfc763fbbc2dfa75a741a7a0e92ffedc479cb0e5181de44397047c85719eef0f4bc831835d06cbf7f048b2f8bcc99200c5ffabb0
|
7
|
+
data.tar.gz: 5ec206a432eaa902c84d2b7ad535e8422ca2394eebd32fd04c17005ba6fb71b9e4097806a112be602ac1408124904293f939bca43370daa2d0ca5d8d60c0ce47
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Changelog of Type scopes
|
2
|
+
|
3
|
+
## 0.6.1 (2024-02-22)
|
4
|
+
|
5
|
+
- Check if table exists to prevent from ActiveRecord::StatementInvalid
|
6
|
+
|
7
|
+
## 0.6 (2021-10-15)
|
8
|
+
|
9
|
+
- Refactor by switching modules into classes.
|
10
|
+
|
11
|
+
New usage is `TypeScopes.inject` instead of `include` :
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Model < ApplicationRecord
|
15
|
+
TypeScopes.inject(self)
|
16
|
+
end
|
17
|
+
```
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
GEM
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (
|
5
|
-
activesupport (=
|
6
|
-
activerecord (
|
7
|
-
activemodel (=
|
8
|
-
activesupport (=
|
9
|
-
activesupport (
|
4
|
+
activemodel (7.0.4.1)
|
5
|
+
activesupport (= 7.0.4.1)
|
6
|
+
activerecord (7.0.4.1)
|
7
|
+
activemodel (= 7.0.4.1)
|
8
|
+
activesupport (= 7.0.4.1)
|
9
|
+
activesupport (7.0.4.1)
|
10
10
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
11
|
i18n (>= 1.6, < 2)
|
12
12
|
minitest (>= 5.1)
|
13
13
|
tzinfo (~> 2.0)
|
14
|
-
|
15
|
-
|
16
|
-
i18n (1.8.10)
|
14
|
+
concurrent-ruby (1.1.10)
|
15
|
+
i18n (1.12.0)
|
17
16
|
concurrent-ruby (~> 1.0)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
mini_portile2 (2.8.1)
|
18
|
+
minitest (5.17.0)
|
19
|
+
pg (1.4.5)
|
20
|
+
rake (13.0.6)
|
21
|
+
sqlite3 (1.6.0)
|
22
|
+
mini_portile2 (~> 2.8.0)
|
23
|
+
tzinfo (2.0.5)
|
23
24
|
concurrent-ruby (~> 1.0)
|
24
|
-
zeitwerk (2.4.2)
|
25
25
|
|
26
26
|
PLATFORMS
|
27
27
|
ruby
|
28
28
|
|
29
29
|
DEPENDENCIES
|
30
|
-
activerecord
|
30
|
+
activerecord
|
31
31
|
pg
|
32
32
|
rake
|
33
33
|
sqlite3
|
34
34
|
|
35
35
|
BUNDLED WITH
|
36
|
-
2.
|
36
|
+
2.2.22
|
data/README.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# Type Scopes
|
2
2
|
|
3
3
|
Type scopes creates useful semantic scopes based on the type of the columns of your models.
|
4
|
-
|
4
|
+
The goal is help to write eloquent code such as:
|
5
5
|
|
6
|
+
```ruby
|
7
|
+
Transaction.paid_after(Date.yesterday).amount_between(100, 200).not_refunded
|
8
|
+
```
|
9
|
+
|
10
|
+
It handles dates, times, strings, numerics and booleans.
|
6
11
|
Here are examples for all the available scopes:
|
7
12
|
|
8
13
|
```ruby
|
@@ -70,7 +75,8 @@ Transaction.description_contains("%foo_") # => where("description LIKE '%[%]foo[
|
|
70
75
|
|
71
76
|
## Install
|
72
77
|
|
73
|
-
Add to your Gemfile `gem "type_scopes"` and run in your terminal `bundle install`.
|
78
|
+
Add to your Gemfile `gem "type_scopes"` and run in your terminal `bundle install`.
|
79
|
+
Then call `TypeScopes.inject self` from your models:
|
74
80
|
|
75
81
|
```ruby
|
76
82
|
# /app/models/transaction.rb
|
data/lib/type_scopes/string.rb
CHANGED
@@ -3,12 +3,6 @@ class TypeScopes::String < TypeScopes
|
|
3
3
|
["character", "text", "varchar"].freeze
|
4
4
|
end
|
5
5
|
|
6
|
-
def self.escape(string)
|
7
|
-
string = string.gsub("%".freeze, "[%]".freeze)
|
8
|
-
string.gsub!("_".freeze, "[_]".freeze)
|
9
|
-
string
|
10
|
-
end
|
11
|
-
|
12
6
|
def self.inject_for_column(model, name)
|
13
7
|
column = model.arel_table[name]
|
14
8
|
append_scope(model, :"#{name}_like", lambda { |str, sensitive: true| where(column.matches(str, nil, sensitive)) })
|
@@ -17,31 +11,27 @@ class TypeScopes::String < TypeScopes
|
|
17
11
|
append_scope(model, :"#{name}_not_ilike", lambda { |str| where(column.does_not_match(str)) })
|
18
12
|
|
19
13
|
append_scope(model, :"#{name}_contains", lambda { |str, sensitive: true|
|
20
|
-
send("#{name}_like", "%#{
|
21
|
-
})
|
22
|
-
|
23
|
-
append_scope(model, :"#{name}_does_not_contain", lambda { |str, sensitive: true|
|
24
|
-
send("#{name}_not_like", "%#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
|
14
|
+
send("#{name}_like", "%#{sanitize_sql_like(str)}%", sensitive: sensitive)
|
25
15
|
})
|
26
16
|
|
27
17
|
append_scope(model, :"#{name}_does_not_contain", lambda { |str, sensitive: true|
|
28
|
-
send("#{name}
|
18
|
+
send("#{name}_not_like", "%#{sanitize_sql_like(str)}%", sensitive: sensitive)
|
29
19
|
})
|
30
20
|
|
31
21
|
append_scope(model, :"#{name}_starts_with", lambda { |str, sensitive: true|
|
32
|
-
send("#{name}_like", "#{
|
22
|
+
send("#{name}_like", "#{sanitize_sql_like(str)}%", sensitive: sensitive)
|
33
23
|
})
|
34
24
|
|
35
25
|
append_scope(model, :"#{name}_does_not_start_with", lambda { |str, sensitive: true|
|
36
|
-
send("#{name}_not_like", "#{
|
26
|
+
send("#{name}_not_like", "#{sanitize_sql_like(str)}%", sensitive: sensitive)
|
37
27
|
})
|
38
28
|
|
39
29
|
append_scope(model, :"#{name}_ends_with", lambda { |str, sensitive: true|
|
40
|
-
send("#{name}_like", "%#{
|
30
|
+
send("#{name}_like", "%#{sanitize_sql_like(str)}", sensitive: sensitive)
|
41
31
|
})
|
42
32
|
|
43
33
|
append_scope(model, :"#{name}_does_not_end_with", lambda { |str, sensitive: true|
|
44
|
-
send("#{name}_not_like", "%#{
|
34
|
+
send("#{name}_not_like", "%#{sanitize_sql_like(str)}", sensitive: sensitive)
|
45
35
|
})
|
46
36
|
|
47
37
|
append_scope(model, :"#{name}_matches", lambda { |str, sensitive: true| where(column.matches_regexp(str, sensitive)) })
|
data/lib/type_scopes/version.rb
CHANGED
data/lib/type_scopes.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
class TypeScopes
|
2
|
-
def self.inject(model, column_names =
|
2
|
+
def self.inject(model, column_names = nil)
|
3
|
+
column_names ||= model.table_exists? ? model.columns.map(&:name) : []
|
3
4
|
for name in column_names
|
4
5
|
if column = model.columns_hash[name]
|
5
6
|
Time.support?(column.sql_type) && Time.inject_for_column(model, name)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Bernard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Automatically create semantic scopes based on columns' types (dates,
|
14
14
|
times, strings and numerics).
|
@@ -18,6 +18,7 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- CHANGELOG.md
|
21
22
|
- Gemfile
|
22
23
|
- Gemfile.lock
|
23
24
|
- README.md
|
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
- !ruby/object:Gem::Version
|
54
55
|
version: '0'
|
55
56
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
57
|
+
rubygems_version: 3.2.22
|
57
58
|
signing_key:
|
58
59
|
specification_version: 4
|
59
60
|
summary: Semantic scopes for your ActiveRecord models.
|