filterable_model 0.1.1
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/filterable_model.gemspec +38 -0
- data/lib/filterable_model.rb +50 -0
- data/lib/filterable_model/version.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6078a2bd24668ce14e00ccf274a3965c4536e99c43b316a10775d53d606c7958
|
|
4
|
+
data.tar.gz: b11cd635de59d42d7ac76ef0967dbf21735e82ef77d2934f2071c24cc81e25cb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 753f40faa8a128a3af5b69a81a0a8c4e3209e82665dace115f2e1848ae0cf72806a8194a41c2f5db7963bf6595d4d2591f1e2711da272f10fe6a4314dc712fb6
|
|
7
|
+
data.tar.gz: ed6bff929dbf8c5e3cffbc948f8c4d8cfd31bc6c53ba4db336214bf72ca9c27c3a408d063c550dc8e9a735d277b2d6cfe5a5aad66e6428e16f9343740678de61
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Umar Al-Kfaween
|
|
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,104 @@
|
|
|
1
|
+
# FilterableModel
|
|
2
|
+
|
|
3
|
+
FilterableModel provides an organized and seamless way to filter your ActiveRecord objects using real and custom attributes.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'filterable_model', '~> 0.1.0'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install filterable_model
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Include FilterableModel inside ApplicationRecord or directly inside your ActiveRecord model:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
class User < ApplicationRecord
|
|
27
|
+
include FilterableModel
|
|
28
|
+
end
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Filtering by real attributes
|
|
32
|
+
|
|
33
|
+
To filter using the exact values of your ActiveRecord model attributes, override the `filterable_attributes` class method to return an array of whitelisted attributes:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
class User < ApplicationRecord
|
|
37
|
+
include FilterableModel
|
|
38
|
+
|
|
39
|
+
concerning :Filtering do
|
|
40
|
+
class_methods do
|
|
41
|
+
|
|
42
|
+
def filterable_attributes
|
|
43
|
+
%w[id gender is_subscribed]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Filtering by custom attributes
|
|
53
|
+
|
|
54
|
+
Filtering using custom attributes works by calling the add_filter method and passing a block that accepts the filter-by value, and returns an `ActiveRecord::Relation`:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
class User < ApplicationRecord
|
|
58
|
+
include FilterableModel
|
|
59
|
+
|
|
60
|
+
concerning :Filtering do
|
|
61
|
+
included do
|
|
62
|
+
|
|
63
|
+
add_filter :name do |name| # search by first name or username
|
|
64
|
+
where("LOWER(users.first_name) LIKE :query OR LOWER(users.username) LIKE :query", query: "%#{name.downcase}%")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
add_filter :just_active do |value| # filter by users with active sessions
|
|
69
|
+
if value.to_s == 'true'
|
|
70
|
+
includes(:session).where(session: { active: true })
|
|
71
|
+
else
|
|
72
|
+
current_scope # do not change the current relation
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Filter your relation by calling `filter` on your model and passing the filtering hash:
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
@users = User.all
|
|
85
|
+
|
|
86
|
+
filtering_hash = {
|
|
87
|
+
gender: 'male',
|
|
88
|
+
is_subscribed: 'false',
|
|
89
|
+
name: 'John',
|
|
90
|
+
just_active: 'true'
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
@users = @users.filter(filtering_hash)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Passing an unknown filter will raise a `FilterNotSupported` error.
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/umar221b/filterable_model.
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "filterable_model"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "filterable_model/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "filterable_model"
|
|
7
|
+
spec.version = FilterableModel::VERSION
|
|
8
|
+
spec.authors = ["Umar Al-Kfaween"]
|
|
9
|
+
spec.email = ["omar.ka923@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = %q{FilterableModel provides an organized and seamless way to filter your ActiveRecord objects using model attributes or custom attributes.}
|
|
12
|
+
spec.description = %q{FilterableModel provides an organized and seamless way to filter your ActiveRecord objects using model attributes or custom attributes.}
|
|
13
|
+
spec.homepage = "https://github.com/umar221b/filterable_model"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.required_ruby_version = '>= 2.2.2'
|
|
17
|
+
|
|
18
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
|
19
|
+
|
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/umar221b/filterable_model"
|
|
22
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
|
23
|
+
|
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
26
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
27
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
28
|
+
end
|
|
29
|
+
spec.bindir = "exe"
|
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
31
|
+
spec.require_paths = ["lib"]
|
|
32
|
+
|
|
33
|
+
spec.add_dependency "activesupport", ">= 4.2.0"
|
|
34
|
+
|
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
38
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "filterable_model/version"
|
|
2
|
+
|
|
3
|
+
module FilterableModel
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
class FilterNotSupported < Error; end
|
|
6
|
+
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
included {}
|
|
9
|
+
|
|
10
|
+
class_methods do
|
|
11
|
+
def filter(filters)
|
|
12
|
+
relations = build_relations(filters.as_json)
|
|
13
|
+
result = send('all')
|
|
14
|
+
relations.each { |relation| result.merge!(relation) }
|
|
15
|
+
result
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# array of strings of model attributes that support being filtered through a basic where statement
|
|
19
|
+
def filterable_attributes
|
|
20
|
+
[]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def add_filter(filter_name, &block)
|
|
24
|
+
filter_name = filter_name.to_s
|
|
25
|
+
custom_filters[filter_name] = block
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def custom_filters
|
|
31
|
+
@custom_filters ||= {}
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def build_relations(filters)
|
|
35
|
+
filters.map do |filter_name, value|
|
|
36
|
+
get_relation(filter_name, value)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def get_relation(filter_name, value)
|
|
41
|
+
if custom_filters.key?(filter_name)
|
|
42
|
+
custom_filters[filter_name].call(value)
|
|
43
|
+
elsif filterable_attributes.include?(filter_name)
|
|
44
|
+
where(filter_name => value)
|
|
45
|
+
else
|
|
46
|
+
raise FilterNotSupported, "Filtering by '#{filter_name}' is not supported."
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: filterable_model
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Umar Al-Kfaween
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-02-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 4.2.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 4.2.0
|
|
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: FilterableModel provides an organized and seamless way to filter your
|
|
70
|
+
ActiveRecord objects using model attributes or custom attributes.
|
|
71
|
+
email:
|
|
72
|
+
- omar.ka923@gmail.com
|
|
73
|
+
executables: []
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".rspec"
|
|
79
|
+
- ".travis.yml"
|
|
80
|
+
- Gemfile
|
|
81
|
+
- LICENSE.txt
|
|
82
|
+
- README.md
|
|
83
|
+
- Rakefile
|
|
84
|
+
- bin/console
|
|
85
|
+
- bin/setup
|
|
86
|
+
- filterable_model.gemspec
|
|
87
|
+
- lib/filterable_model.rb
|
|
88
|
+
- lib/filterable_model/version.rb
|
|
89
|
+
homepage: https://github.com/umar221b/filterable_model
|
|
90
|
+
licenses:
|
|
91
|
+
- MIT
|
|
92
|
+
metadata:
|
|
93
|
+
homepage_uri: https://github.com/umar221b/filterable_model
|
|
94
|
+
source_code_uri: https://github.com/umar221b/filterable_model
|
|
95
|
+
post_install_message:
|
|
96
|
+
rdoc_options: []
|
|
97
|
+
require_paths:
|
|
98
|
+
- lib
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 2.2.2
|
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0'
|
|
109
|
+
requirements: []
|
|
110
|
+
rubygems_version: 3.0.3
|
|
111
|
+
signing_key:
|
|
112
|
+
specification_version: 4
|
|
113
|
+
summary: FilterableModel provides an organized and seamless way to filter your ActiveRecord
|
|
114
|
+
objects using model attributes or custom attributes.
|
|
115
|
+
test_files: []
|