active_filterable 0.0.1

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: 7194bc895725f4503620abf39b9fde391c705f16f5e7452577030d9535861c5e
4
+ data.tar.gz: 37ec7b53d8a19f083677c2e6393ac3d8322c30eeb9e7644df60535c4a91967d3
5
+ SHA512:
6
+ metadata.gz: 7a0e733871e4fded3d120979ba5e45875cb463df0cfe0bb528e88fbb3cdf3ad17f1c071cfab1694223f84c705d0e1023c654057cc7092c612cf47d8e7a3ba2d3
7
+ data.tar.gz: 37d0e68f76d23d79f390a05a469617a41f8e725a8d40f609b68f123dc9cceb62b9d4067dc3311ad56c1067a530a30eea68c1e96584c48c2bb340078a6f2545f5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2023-07-11
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kervinchangyu@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in active_filterable.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_filterable (0.0.1)
5
+ activerecord (>= 5.0)
6
+ activesupport (>= 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.1.6.1)
12
+ activesupport (= 6.1.6.1)
13
+ activerecord (6.1.6.1)
14
+ activemodel (= 6.1.6.1)
15
+ activesupport (= 6.1.6.1)
16
+ activesupport (6.1.6.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ zeitwerk (~> 2.3)
22
+ ast (2.4.2)
23
+ concurrent-ruby (1.1.10)
24
+ diff-lcs (1.5.0)
25
+ i18n (1.11.0)
26
+ concurrent-ruby (~> 1.0)
27
+ json (2.6.2)
28
+ minitest (5.16.2)
29
+ parallel (1.22.1)
30
+ parser (3.1.2.1)
31
+ ast (~> 2.4.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.6.0)
35
+ rexml (3.2.5)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.38.0)
50
+ json (~> 2.3)
51
+ parallel (~> 1.10)
52
+ parser (>= 3.1.2.1)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ regexp_parser (>= 1.8, < 3.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.23.0, < 2.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (>= 1.4.0, < 3.0)
59
+ rubocop-ast (1.23.0)
60
+ parser (>= 3.1.1.0)
61
+ ruby-progressbar (1.11.0)
62
+ tzinfo (2.0.4)
63
+ concurrent-ruby (~> 1.0)
64
+ unicode-display_width (2.3.0)
65
+ zeitwerk (2.6.0)
66
+
67
+ PLATFORMS
68
+ universal-darwin-21
69
+
70
+ DEPENDENCIES
71
+ active_filterable!
72
+ bundler
73
+ rake (~> 13.0)
74
+ rspec (~> 3.0)
75
+ rubocop (~> 1.21)
76
+
77
+ BUNDLED WITH
78
+ 2.3.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kervin
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,100 @@
1
+ # ActiveFilterable
2
+
3
+ ActiveFilterable is a gem designed to enable custom query filters for ActiveRecord models through metaprogramming. By using ActiveFilterable, you can specify which attributes of a model can be used for filtering and define different query methods such as full-text matching, prefix matching, suffix matching, IN queries, NOT IN queries, equality, greater than, less than, not equal, greater than or equal to, and less than or equal to.
4
+
5
+ ActiveFilterable provides a concise and flexible way to handle complex query requirements. It allows you to define query filters directly in the model and apply them by chaining method calls in the controller, generating the corresponding query conditions.
6
+
7
+
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add active_filterable
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install active_filterable
18
+
19
+ ## Usage
20
+
21
+ 1. In your Rails application, include the ActiveFilterable module in the model where you want to add the filtering functionality.
22
+
23
+ ```ruby
24
+ include ActiveFilterable
25
+ ```
26
+
27
+ 2. Use the `filterable` method in the model to define filters.
28
+
29
+ ```ruby
30
+ filterable :column1, :column2, by: [:filter1, :filter2]
31
+ ```
32
+
33
+ This method accepts two parameters:
34
+
35
+ - `columns`: An array containing the column names to which you want to add filters.
36
+ - `by`: An array containing the available filter types.
37
+ > Supported filter types and corresponding SQL query methods:
38
+ >- `:eq`: Equal to. Uses `where(column => value)`.
39
+ >- `:in`: Included in. Uses `where(column => value)`.
40
+ >- `:lt`: Less than. Uses `where("#{column} < ?", value)`.
41
+ >- `:gt`: Greater than. Uses `where("#{column} > ?", value)`.
42
+ >- `:le`: Less than or equal to. Uses `where("#{column} <= ?", value)`.
43
+ >- `:ge`: Greater than or equal to. Uses `where("#{column} >= ?", value)`.
44
+ >- `:ne`: Not equal to. Uses `where.not(column => value)`.
45
+ >- `:not_in`: Not included in. Uses `where.not(column => value)`.
46
+ >- `:start_with`: Starts with the specified value. Uses `where("#{column} LIKE ?", "#{value}%")`.
47
+ >- `:like`: Fuzzy matching. Uses `where("#{column} LIKE ?", "%#{value}%")`.
48
+ >- `:end_with`: Ends with the specified value. Uses `where("#{column} LIKE ?", "%#{value}")`.
49
+
50
+ 3. Based on the defined filters, scope methods will be created for each column and filter combination.
51
+
52
+ ```ruby
53
+ ModelName.column1_filter1(value)
54
+ ```
55
+
56
+ For example, if you have a model named `User` and you want to use the "equals" (`eq`) and "greater than" (`gt`) filters for the `name` column, you can use the following code:
57
+
58
+ ```ruby
59
+ filterable :name, by: [:eq]
60
+ filterable :age, by: [:gt]
61
+ ```
62
+
63
+ Then, you can query the `User` model as follows:
64
+
65
+ ```ruby
66
+ User.name_eq("John") # Returns users with the name equal to "John"
67
+ User.age_gt(30) # Returns users with the age greater than 30
68
+ ```
69
+
70
+ 4. Use the `filter_by` method to perform combined filtering on the model.
71
+
72
+ ```ruby
73
+ ModelName.filter_by(filter1: value1, filter2: value2, ...)
74
+ ```
75
+
76
+ For example, if you want to query the `User` model based on the `name` and `age` filters, you can use the following code:
77
+
78
+ ```ruby
79
+ User.filter_by(name_eq: "John", age_gt: 30) # Returns users with the name equal to "John" and age greater than 30
80
+ ```
81
+
82
+ This will return the result set that satisfies the specified filter conditions.
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kervinchang/active_filterable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kervinchang/active_filterable/blob/master/CODE_OF_CONDUCT.md).
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
97
+
98
+ ## Code of Conduct
99
+
100
+ Everyone interacting in the ActiveFilterable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kervinchang/active_filterable/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFilterable
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "active_filterable/version"
4
+
5
+ module ActiveFilterable
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def filterable(*columns, by: [])
12
+ columns.each do |column|
13
+ by.each do |filter|
14
+ scope "#{column}_#{filter}", ->(value) { filter_query(column, filter, value) }
15
+ end
16
+ end
17
+
18
+ define_singleton_method(:filter_by) do |filters|
19
+ query = self
20
+ filters.each_key do |filter|
21
+ value = filters[filter]
22
+ query = query.and(send(filter, value))
23
+ end
24
+ query
25
+ end
26
+ end
27
+
28
+ def filter_query(column, filter, value)
29
+ return if value.blank?
30
+
31
+ case filter
32
+ when :eq, :in
33
+ where(column => value)
34
+ when :lt
35
+ where("#{column} < ?", value)
36
+ when :gt
37
+ where("#{column} > ?", value)
38
+ when :le
39
+ where("#{column} <= ?", value)
40
+ when :ge
41
+ where("#{column} >= ?", value)
42
+ when :ne, :not_in
43
+ where.not(column => value)
44
+ when :start_with
45
+ where("#{column} LIKE ?", "#{value}%")
46
+ when :like
47
+ where("#{column} LIKE ?", "%#{value}%")
48
+ when :end_with
49
+ where("#{column} LIKE ?", "%#{value}")
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveFilterable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_filterable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kervin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-11 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ActiveFilterable is a gem designed to enable custom query filters for
70
+ ActiveRecord models through metaprogramming. By using ActiveFilterable, you can
71
+ specify which attributes of a model can be used for filtering and define different
72
+ query methods such as full-text matching, prefix matching, suffix matching, IN queries,
73
+ NOT IN queries, equality, greater than, less than, not equal, greater than or equal
74
+ to, and less than or equal to. ActiveFilterable provides a concise and flexible
75
+ way to handle complex query requirements. It allows you to define query filters
76
+ directly in the model and apply them by chaining method calls in the controller,
77
+ generating the corresponding query conditions.
78
+ email:
79
+ - kervinchangyu@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".rspec"
85
+ - ".rubocop.yml"
86
+ - CHANGELOG.md
87
+ - CODE_OF_CONDUCT.md
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/active_filterable.rb
94
+ - lib/active_filterable/version.rb
95
+ - sig/active_filterable.rbs
96
+ homepage: https://github.com/kervinchang/active_filterable
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ homepage_uri: https://github.com/kervinchang/active_filterable
101
+ source_code_uri: https://github.com/kervinchang/active_filterable
102
+ changelog_uri: https://github.com/kervinchang/active_filterable/blob/master/CHANGELOG.md
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 2.6.0
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.0.3.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: ActiveFilterable is a Ruby gem that provides custom query filters for ActiveRecord
122
+ models through metaprogramming. It allows you to easily define custom filters based
123
+ on the attributes and query methods of a model in Rails applications, simplifying
124
+ the querying process.
125
+ test_files: []