type_scopes 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 26fb4bb8237b5e8afa0912bb38895c1afc4475d7
4
- data.tar.gz: d331c4b23670597d42be35954bd0b2ca6a24b3b3
2
+ SHA256:
3
+ metadata.gz: 9e8bdc381d58fe45f3469a665b2f5be91c2dd26a3865152eaf121b01d557faa5
4
+ data.tar.gz: 6d1cba071587a8ede1cdf4689bd5cc5306c63316ebc9dce7b4f5fa29bedfdd6f
5
5
  SHA512:
6
- metadata.gz: 23b399603b333ceb04df3ffefcb6ee65309e3b9e027dc39f24dca9908870d75924a36100e99b33a10e24e1fecd800594a5b999ba75ad2da3a28e9cf34071cc6a
7
- data.tar.gz: 5bb76bf2695722732e17f973fce71cd44d747b599b955669781afbebc142548c0dc1cc74a95d126c12070c52300914a7c1f154e15f69ca932db401ce1d1f1409
6
+ metadata.gz: 779176bcded65c9163253892abc66d5df1d7443bc6f1a2c6d15843cf72af3e968d0a6e922507dfb883484c1faa7dc83c2b18b361c98d21e05e3831b3a5d5f23e
7
+ data.tar.gz: 3772b0545a904a4b6cbe7bd0677bab5f5f341af5b9f5a15ff6857f1ee4a19f6bf88abfa26df041ffa1e74335c6e1c091807febf99f0976b9efbbb19b8deec9cd
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  Type scopes creates useful scopes based on the type of the columns of your models.
4
4
  It handles dates, times, strings and numerics.
5
5
 
6
- Here is an example of all the available scopes:
6
+ Here are examples for all the available scopes:
7
7
 
8
8
  ```ruby
9
9
  # paid_at: datetime
@@ -14,11 +14,14 @@ class Transaction < ActiveRecord::Base
14
14
  end
15
15
 
16
16
  # Time scopes
17
- Transaction.paid_to("2017-09-06") # => where("paid_to <= '2017-09-06'")
18
- Transaction.paid_from("2017-09-06") # => where("paid_to >= '2017-09-06'")
19
- Transaction.paid_after("2017-09-06") # => where("paid_to > '2017-09-06'")
20
- Transaction.paid_before("2017-09-06") #= where("paid_to < '2017-09-06'")
21
- Transaction.paid_between("2017-09-06", "2017-09-07") # => where("paid_to BETWEEN '2017-09-06' AND '2017-09-07'")
17
+ Transaction.paid_to("2017-09-06") # => where("paid_at <= '2017-09-06'")
18
+ Transaction.paid_from("2017-09-06") # => where("paid_at >= '2017-09-06'")
19
+ Transaction.paid_after("2017-09-06") # => where("paid_at > '2017-09-06'")
20
+ Transaction.paid_before("2017-09-06") #= where("paid_at < '2017-09-06'")
21
+ Transaction.paid_between("2017-09-06", "2017-09-07") # => where("paid_at BETWEEN '2017-09-06' AND '2017-09-07'")
22
+ Transaction.paid_not_between("2017-09-06", "2017-09-07") # => where("paid_at NOT BETWEEN '2017-09-06' AND '2017-09-07'")
23
+ Transaction.paid_within("2017-09-06", "2017-09-07") # => where("paid_at > '2017-09-06' AND paid_at < '2017-09-07'")
24
+ Transaction.paid_not_within("2017-09-06", "2017-09-07") # => where("paid_at <= '2017-09-06' OR paid_at >= '2017-09-07'")
22
25
 
23
26
  # Numeric scopes
24
27
  Transaction.amount_to(100) # => where("amount <= 100")
@@ -26,6 +29,9 @@ Transaction.amount_from(100) # => where("amount >= 100")
26
29
  Transaction.amount_above(100) # => where("amount > 100")
27
30
  Transaction.amount_below(100) # => where("amount < 100")
28
31
  Transaction.amount_between(100, 200) # => where("amount BETWEEN 100 AND 200")
32
+ Transaction.amount_not_between(100, 200) # => where("amount NOT BETWEEN 100 AND 200")
33
+ Transaction.amount_within(100, 200) # => where("amount > 100 AND amount < 200")
34
+ Transaction.amount_not_within(100, 200) # => where("amount <= 100 OR amount >= 200")
29
35
 
30
36
  # String scopes
31
37
  Transaction.description_contains("foo") # => where("description LIKE '%foo%'")
@@ -1,5 +1,5 @@
1
1
  module NumericScopes
2
- TYPES = ["integer", "double precision", "numeric"].freeze
2
+ TYPES = ["integer", "double precision", "numeric", "bigint"].freeze
3
3
 
4
4
  def self.included(model)
5
5
  model.extend(ClassMethods)
@@ -9,18 +9,22 @@ module NumericScopes
9
9
  module ClassMethods
10
10
  def create_numeric_scopes
11
11
  for column in columns
12
- if TYPES.include?(column.sql_type)
12
+ if TYPES.any? { |type| column.sql_type.include?(type) }
13
13
  create_numeric_scopes_for_column(column.name)
14
14
  end
15
15
  end
16
16
  end
17
17
 
18
18
  def create_numeric_scopes_for_column(name)
19
- scope :"#{name}_to", lambda { |value| where("#{quoted_table_name}.#{name} <= ?", value) }
20
- scope :"#{name}_from", lambda { |value| where("#{quoted_table_name}.#{name} >= ?", value) }
21
- scope :"#{name}_above", lambda { |value| where("#{quoted_table_name}.#{name} > ?", value) }
22
- scope :"#{name}_below", lambda { |value| where("#{quoted_table_name}.#{name} < ?", value) }
23
- scope :"#{name}_between", lambda { |from, to| where("#{quoted_table_name}.#{name} BETWEEN ? AND ?", from, to) }
19
+ full_name = "#{quoted_table_name}.#{name}"
20
+ scope :"#{name}_to", lambda { |value| where("#{full_name} <= ?", value) }
21
+ scope :"#{name}_from", lambda { |value| where("#{full_name} >= ?", value) }
22
+ scope :"#{name}_above", lambda { |value| where("#{full_name} > ?", value) }
23
+ scope :"#{name}_below", lambda { |value| where("#{full_name} < ?", value) }
24
+ scope :"#{name}_between", lambda { |from, to| where("#{full_name} BETWEEN ? AND ?", from, to) }
25
+ scope :"#{name}_not_between", lambda { |from, to| where("#{full_name} BETWEEN ? AND ?", from, to) }
26
+ scope :"#{name}_within", lambda { |from, to| where("#{full_name} > ? AND #{full_name} < ?", from, to) }
27
+ scope :"#{name}_not_within", lambda { |from, to| where("#{full_name} <= ? OR #{full_name} >= ?", from, to) }
24
28
  end
25
29
  end
26
30
  end
data/lib/string_scopes.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  module StringScopes
2
+ TYPES = ["character", "text"].freeze
3
+
2
4
  def self.included(model)
3
5
  model.extend(ClassMethods)
4
6
  model.create_string_scopes
@@ -13,16 +15,17 @@ module StringScopes
13
15
  module ClassMethods
14
16
  def create_string_scopes
15
17
  for column in columns
16
- if column.sql_type.index("character") == 0 || column.sql_type.index("text") == 0
18
+ if TYPES.any? { |type| column.sql_type.include?(type) }
17
19
  create_string_scopes_for_column(column.name)
18
20
  end
19
21
  end
20
22
  end
21
23
 
22
24
  def create_string_scopes_for_column(name)
23
- scope :"#{name}_contains", lambda { |str| where("#{quoted_table_name}.#{name} LIKE ?", "%#{StringScopes.escape(str)}%") }
24
- scope :"#{name}_starts_with", lambda { |str| where("#{quoted_table_name}.#{name} LIKE ?", "#{StringScopes.escape(str)}%") }
25
- scope :"#{name}_ends_with", lambda { |str| where("#{quoted_table_name}.#{name} LIKE ?", "%#{StringScopes.escape(str)}") }
25
+ full_name = "#{quoted_table_name}.#{name}"
26
+ scope :"#{name}_contains", lambda { |str| where("#{full_name} LIKE ?", "%#{StringScopes.escape(str)}%") }
27
+ scope :"#{name}_starts_with", lambda { |str| where("#{full_name} LIKE ?", "#{StringScopes.escape(str)}%") }
28
+ scope :"#{name}_ends_with", lambda { |str| where("#{full_name} LIKE ?", "%#{StringScopes.escape(str)}") }
26
29
  end
27
30
  end
28
31
  end
@@ -1,4 +1,6 @@
1
1
  module TimestampScopes
2
+ TYPES = ["timestamp", "datetime", "date"].freeze
3
+
2
4
  def self.included(model)
3
5
  model.extend(ClassMethods)
4
6
  model.create_timestamp_scopes
@@ -7,19 +9,23 @@ module TimestampScopes
7
9
  module ClassMethods
8
10
  def create_timestamp_scopes
9
11
  for column in columns
10
- if column.sql_type.index("timestamp") == 0
12
+ if TYPES.any? { |type| column.sql_type.include?(type) }
11
13
  create_timestamp_scopes_for_column(column.name)
12
14
  end
13
15
  end
14
16
  end
15
17
 
16
18
  def create_timestamp_scopes_for_column(name)
19
+ full_name = "#{quoted_table_name}.#{name}"
17
20
  short_name = shorten_column_name(name)
18
- scope :"#{short_name}_to", lambda { |date| where("#{quoted_table_name}.#{name} <= ?", date) }
19
- scope :"#{short_name}_from", lambda { |date| where("#{quoted_table_name}.#{name} >= ?", date) }
20
- scope :"#{short_name}_after", lambda { |date| where("#{quoted_table_name}.#{name} > ?", date) }
21
- scope :"#{short_name}_before", lambda { |date| where("#{quoted_table_name}.#{name} < ?", date) }
22
- scope :"#{short_name}_between", lambda { |from, to| where("#{quoted_table_name}.#{name} BETWEEN ? AND ?", from, to) }
21
+ scope :"#{short_name}_to", lambda { |date| where("#{full_name} <= ?", date) }
22
+ scope :"#{short_name}_from", lambda { |date| where("#{full_name} >= ?", date) }
23
+ scope :"#{short_name}_after", lambda { |date| where("#{full_name} > ?", date) }
24
+ scope :"#{short_name}_before", lambda { |date| where("#{full_name} < ?", date) }
25
+ scope :"#{short_name}_between", lambda { |from, to| where("#{full_name} BETWEEN ? AND ?", from, to) }
26
+ scope :"#{short_name}_not_between", lambda { |from, to| where("#{full_name} NOT BETWEEN ? AND ?", from, to) }
27
+ scope :"#{short_name}_within", lambda { |from, to| where("#{full_name} > ? AND #{full_name} < ?", from, to) }
28
+ scope :"#{short_name}_not_within", lambda { |from, to| where("#{full_name} <= ? OR #{full_name} >= ?", from, to) }
23
29
  end
24
30
 
25
31
  def shorten_column_name(name)
@@ -1,3 +1,3 @@
1
1
  module TypeScopes
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-06 00:00:00.000000000 Z
11
+ date: 2021-06-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Useful scopes based on columns' types (dates, times, strings and numerics).
14
14
  email:
@@ -44,10 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  requirements: []
47
- rubyforge_project:
48
- rubygems_version: 2.4.8
47
+ rubygems_version: 3.0.3
49
48
  signing_key:
50
49
  specification_version: 4
51
50
  summary: Automatic scopes for ActiveRecord models.
52
51
  test_files: []
53
- has_rdoc: