active_filters 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4db334f6ba04d16a4d98c1d2cbf270b6c5c9ae0c14021028dda658ff4c72e06
4
+ data.tar.gz: 0c146475da64250d96d681977a9e58e11c3dc85bd287fbf4e21504940b9db96b
5
+ SHA512:
6
+ metadata.gz: 498d857af4d5e6dd3f8f5a81b61e290abd7056fe6ab81af616e9a825a6ff02c5a6ec8a07af908ede654bffa9b0f890491e66b4953da08b4bd33dd3bd1ada8f33
7
+ data.tar.gz: edb023ee0b84c34d4c3f8f75f5d20771bf3db53f13131c59205067ea615ba02066d1d413ed10b776df1390f75e829ebcb47012146441b3cfddfc41e43e9c2bb3
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_filters.gemspec
4
+ gemspec
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_filters (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (5.2.2)
10
+ actionview (= 5.2.2)
11
+ activesupport (= 5.2.2)
12
+ rack (~> 2.0)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (5.2.2)
17
+ activesupport (= 5.2.2)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
22
+ activemodel (5.2.2)
23
+ activesupport (= 5.2.2)
24
+ activerecord (5.2.2)
25
+ activemodel (= 5.2.2)
26
+ activesupport (= 5.2.2)
27
+ arel (>= 9.0)
28
+ activesupport (5.2.2)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ arel (9.0.0)
34
+ builder (3.2.3)
35
+ concurrent-ruby (1.1.4)
36
+ crass (1.0.4)
37
+ erubi (1.8.0)
38
+ i18n (1.5.3)
39
+ concurrent-ruby (~> 1.0)
40
+ loofah (2.2.3)
41
+ crass (~> 1.0.2)
42
+ nokogiri (>= 1.5.9)
43
+ metaclass (0.0.4)
44
+ mini_portile2 (2.4.0)
45
+ minitest (5.11.3)
46
+ mocha (1.8.0)
47
+ metaclass (~> 0.0.1)
48
+ nokogiri (1.10.1)
49
+ mini_portile2 (~> 2.4.0)
50
+ rack (2.0.6)
51
+ rack-test (1.1.0)
52
+ rack (>= 1.0, < 3)
53
+ rails-dom-testing (2.0.3)
54
+ activesupport (>= 4.2.0)
55
+ nokogiri (>= 1.6)
56
+ rails-html-sanitizer (1.0.4)
57
+ loofah (~> 2.2, >= 2.2.2)
58
+ rake (10.5.0)
59
+ thread_safe (0.3.6)
60
+ tzinfo (1.2.5)
61
+ thread_safe (~> 0.1)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ actionpack (>= 4.2)
68
+ active_filters!
69
+ activerecord (>= 4.2)
70
+ activesupport (>= 4.2)
71
+ bundler (~> 2.0)
72
+ minitest (~> 5.0)
73
+ mocha (>= 1.8.0)
74
+ rake (~> 10.0)
75
+
76
+ BUNDLED WITH
77
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nathan Huberty
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.
@@ -0,0 +1,120 @@
1
+ # ActiveFilters
2
+
3
+ Active Filters allows you to map incoming controller parameters to filter your resources by chaining scopes.
4
+
5
+ It is inspired by [Justin Weiss solution](https://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/) offering a cleaner way to declare your filters.
6
+
7
+ The gem [has_scope](https://github.com/plataformatec/has_scope) exists but does not handle params stored as the value of a hash.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_filters'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_filters
24
+
25
+ ## Usage
26
+
27
+ Let's say you have several scopes on your User model
28
+
29
+ ```ruby
30
+ class User < ApplicationRecord
31
+ scope :country, -> country_code { where(country: country_code) }
32
+ scope :gender, -> gender { where(gender: gender) }
33
+ end
34
+ ```
35
+
36
+ And you want to chain thoses two scopes.
37
+ You first need to include the Filterable controller module in your application controller
38
+
39
+ ```ruby
40
+ class ApplicationController < ActionController::Base
41
+ include ActiveFilters::Controller::Filterable
42
+ end
43
+ ```
44
+
45
+ and then include the model Filterable one in the model you want to filter.
46
+
47
+ ```ruby
48
+ class User < ApplicationRecord
49
+ include ActiveFilters::Model::Filterable
50
+
51
+ scope :country, -> country_code { where(country: country_code) }
52
+ scope :gender, -> gender { where(gender: gender) }
53
+ end
54
+ ```
55
+
56
+ Then you just need to declare your named scopes as filters using the `has_filters` DSL
57
+
58
+ ```ruby
59
+ class UsersController < ApplicationController
60
+ has_filters :country, :gender
61
+ end
62
+ ```
63
+
64
+ and apply them to a specific resource using the `filter` class method and the `filterable params` hash of params as argument.
65
+
66
+ ```ruby
67
+ class UsersController < ApplicationController
68
+ has_filters :country, :gender
69
+
70
+ def index
71
+ @users = User.filter(filterable_params)
72
+ end
73
+ end
74
+ ```
75
+
76
+ For each request:
77
+
78
+ ```
79
+ /users
80
+ #=> acts like a normal request
81
+
82
+ /users?country=FR&gender=female
83
+ #=> calls the named scope and bring only females in France
84
+ ```
85
+
86
+ Now let's say you want to use incoming params stored as the value of a hash as scopes:
87
+
88
+ ```
89
+ /users?filter[country]=FR&filter[gender]=female
90
+ #=> { filter: { country: 'FR', gender: 'female' } }
91
+ ```
92
+
93
+ Then you can add the `in_key` attribute with the name of the key in which params are stored.
94
+
95
+ ```ruby
96
+ class UsersController < ApplicationController
97
+ has_filters :country, :gender, in_key: :filter
98
+
99
+ def index
100
+ @users = User.filter(filterable_params)
101
+ end
102
+ end
103
+ ```
104
+
105
+ ## Initializer
106
+
107
+ By default the params variable used by Active Filters is `params`.
108
+ If you want to use another params variable, you have to create an initializer in `config/initializers/active_filters.rb` and set the variable name: (for instance with @params)
109
+
110
+ ```ruby
111
+ ActiveFilters::Setup.params_variable = '@params'
112
+ ```
113
+
114
+ ## Contributing
115
+
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_filters.
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_filters/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_filters"
8
+ spec.version = ActiveFilters::VERSION
9
+ spec.authors = ["Nathan Huberty"]
10
+ spec.email = ["nathan.huberty@gmail.com"]
11
+
12
+ spec.summary = "Map incoming controller parameters to named scopes in your models"
13
+ spec.description = "Map incoming controller parameters to named scopes in your models"
14
+ spec.homepage = "https://github.com/FidMe/active_filters"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 2.0"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "activerecord", ">= 4.2"
26
+ spec.add_development_dependency "activesupport", ">= 4.2"
27
+ spec.add_development_dependency "actionpack", ">= 4.2"
28
+ spec.add_development_dependency "mocha", ">= 1.8.0"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_filters"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,44 @@
1
+ require "active_filters/version"
2
+ require "active_support/all"
3
+ require_relative "active_filters/setup"
4
+
5
+ module ActiveFilters
6
+ module Model
7
+ module Filterable
8
+ extend ::ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ def filter(filterable_params)
12
+ results = all
13
+ (filterable_params || []).each do |key, value|
14
+ results = results.public_send(key, value) if value.present?
15
+ end
16
+ results
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module Controller
23
+ module Filterable
24
+ extend ::ActiveSupport::Concern
25
+
26
+ class_methods do
27
+ def has_filters(*filters)
28
+ filter_key = filters.last.is_a?(Hash) && filters.last[:in_key]
29
+ filters.pop if filter_key
30
+
31
+ define_method(:filterable_params) do
32
+ return {} unless _get_filter_params(filter_key) && filters
33
+ _get_filter_params(filter_key).slice(*filters)
34
+ end
35
+ end
36
+ end
37
+
38
+ def _get_filter_params(filter_key)
39
+ _params = eval(::ActiveFilters::Setup.params_variable)
40
+ (filter_key ? _params[filter_key] : _params)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveFilters
2
+ class Setup
3
+ @params_variable = 'params'
4
+
5
+ class << self
6
+ attr_accessor :params_variable
7
+ end
8
+
9
+ def self.configure
10
+ yield self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveFilters
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_filters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Huberty
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: actionpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 1.8.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.8.0
111
+ description: Map incoming controller parameters to named scopes in your models
112
+ email:
113
+ - nathan.huberty@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - active_filters.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/active_filters.rb
129
+ - lib/active_filters/setup.rb
130
+ - lib/active_filters/version.rb
131
+ homepage: https://github.com/FidMe/active_filters
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.7.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Map incoming controller parameters to named scopes in your models
155
+ test_files: []