qry_filter 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17501b95f2d0615442a1b81056df65461f31c31263f508e5458898162df6bb91
4
+ data.tar.gz: e759286af0273f0b89df702ba2a396b622e6856175f874f31027fff9b12ad9c1
5
+ SHA512:
6
+ metadata.gz: 8795ad6c170e0f5d27f4ac359ac58a85f8951f4faf246eba3e27eb49d34ca22aab95074d730b9b9e0cd6de46dc7415b3b506c06bda273a45da68fdabe3d56ed8
7
+ data.tar.gz: 654164d8518af4e6cb858ac469f9f25ffc9d6832ec855c668a16007720e4b40f1f58a5b3d68f2b8678235a624df1aa5d1edf7517f61e6fbd527f35f7f4feab9a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # QryFilter
2
+ *Note: This is a work in progress and should not be used in production.*
3
+
4
+ QryFilter aka "QueryFilter" is a simple Rails gem that provides a pattern and helper when dealing with lots of filter clauses in your ActiveRecord query.
5
+
6
+ ## Usage
7
+ **Filter Class**
8
+ ```ruby
9
+ # app/filters/user_filter.rb
10
+ class UserFilter < ApplicationFilter
11
+ def default
12
+ by_id
13
+ by_age
14
+ end
15
+
16
+ def by_id
17
+ @scope = @scope.where(id: @filter_hash[:id])
18
+ end
19
+
20
+ def by_age
21
+ @scope = @scope.where(age: @filter_hash[:age])
22
+ end
23
+ end
24
+ ```
25
+
26
+ **Class Method**
27
+ ```ruby
28
+ QryFilter.compose(scope, params)
29
+ QryFilter.compose(scope, params, filter_by: [:id, :age])
30
+ QryFilter.compose(scope, params, filter_by: [:id, :age], filter_class: UserFilter)
31
+ ```
32
+
33
+ **Helper**
34
+ ```ruby
35
+ filter scope, params
36
+ filter scope, params, filter_by: [:id, :age]
37
+ filter scope, params, filter_by: [:id, :age], filter_class: UserFilter
38
+ ```
39
+
40
+ ## TODO
41
+ - Autodetect Model name to match with Filter class
42
+ - Work with ActiveRecord::Relation
43
+ - Generator for ApplicationFilter
44
+ - More tests
45
+
46
+ ## Installation
47
+ Add this line to your application's Gemfile:
48
+
49
+ ```ruby
50
+ gem 'qry_filter'
51
+ ```
52
+
53
+ And then execute:
54
+ ```bash
55
+ $ bundle
56
+ ```
57
+
58
+ Or install it yourself as:
59
+ ```bash
60
+ $ gem install qry_filter
61
+ ```
62
+
63
+ Generate app/filters/application_filter.rb:
64
+ ```bash
65
+ $ rails g qry_filter:install
66
+ ```
67
+
68
+ ## Contributing
69
+ Contribution directions go here.
70
+
71
+ ## License
72
+ 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,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'QryFilter'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates initial application filter.
@@ -0,0 +1,11 @@
1
+ module QryFilter
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def copy_application_filter
7
+ template "application_filter.rb", "app/filters/application_filter.rb"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class ApplicationFilter
2
+ attr_reader :scope
3
+
4
+ def initialize(scope, filter_hash)
5
+ @scope = scope
6
+ @filter_hash = filter_hash
7
+ end
8
+
9
+ def default
10
+ end
11
+ end
data/lib/qry_filter.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "qry_filter/filter_class_finder"
2
+
3
+ module QryFilter
4
+ class << self
5
+ def compose(scope, filter_hash, filter_class: nil, filter_by: nil)
6
+ if filter_class.nil?
7
+ filter_class = FilterClassFinder.new(scope).filter_class
8
+ end
9
+
10
+ filter = filter_class.new(scope, filter_hash)
11
+
12
+ if filter_by.nil?
13
+ filter.send(:default)
14
+ else
15
+ filter_by.each do |subject|
16
+ filter.send("by_#{subject}")
17
+ end
18
+ end
19
+
20
+ filter.scope
21
+ end
22
+ end
23
+
24
+ def filter(*args)
25
+ QryFilter.compose(*args)
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module QryFilter
2
+
3
+ class FilterClassFinder
4
+ def initialize(scope)
5
+ @scope = scope
6
+ end
7
+
8
+ def filter_class
9
+ klass = find(@scope)
10
+ klass.is_a?(String) ? klass.safe_constantize : klass
11
+ end
12
+
13
+ private
14
+
15
+ def find(subject)
16
+ if subject.is_a?(Array)
17
+ modules = subject.dup
18
+ last = modules.pop
19
+ context = modules.map { |x| find_class_name(x) }.join("::")
20
+ [context, find(last)].join("::")
21
+ else
22
+ class_name = find_class_name(subject)
23
+ "#{class_name}Filter"
24
+ end
25
+ end
26
+
27
+ def find_class_name(subject)
28
+ if subject.respond_to?(:model_name)
29
+ subject.model_name
30
+ elsif subject.class.respond_to?(:model_name)
31
+ subject.class.model_name
32
+ elsif subject.is_a?(Class)
33
+ subject
34
+ elsif subject.is_a?(Symbol)
35
+ subject.to_s.camelize
36
+ else
37
+ subject.class
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ module QryFilter
2
+ VERSION = '0.1.1'
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qry_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Iñaki Ibarra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.1
55
+ description: A Rails add-on for handling query filters
56
+ email:
57
+ - inaki.mateo.ibarra@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/generators/qry_filter/install/USAGE
66
+ - lib/generators/qry_filter/install/install_generator.rb
67
+ - lib/generators/qry_filter/install/templates/application_filter.rb
68
+ - lib/qry_filter.rb
69
+ - lib/qry_filter/filter_class_finder.rb
70
+ - lib/qry_filter/version.rb
71
+ homepage: https://github.com/ibarrain/qry_filter
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Query filter helper for Rails
94
+ test_files: []