rice_cooker 0.1.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
+ SHA1:
3
+ metadata.gz: d94102d915bb79855a772a3a7ba515a73c74e873
4
+ data.tar.gz: 5e987147d9dd89a7f2274d406f5387e9382baf52
5
+ SHA512:
6
+ metadata.gz: b47b99cc9fd08a3292249bb9597f6399718fc1c1ae256eafc832f34ac4db32ef34cb29d21d412b271955c3fce26e04b9ac79724fa111f5d2a14e50837bb701c4
7
+ data.tar.gz: 6899285ae631cda10affd5238a392f16aac98781ab09c3ebd6fe82c8302da038c112b02e200f333e086c2b45a55d7024a0cff14a25c8c399768aecd7268c1c7b
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+
2
+ *.rbc
3
+ capybara-*.html
4
+ .rspec
5
+ /log
6
+ /tmp
7
+ /db/*.sqlite3
8
+ /db/*.sqlite3-journal
9
+ /public/system
10
+ /coverage/
11
+ /spec/tmp
12
+ **.orig
13
+ rerun.txt
14
+ pickle-email-*.html
15
+
16
+ pkg/
17
+ test/dummy/db/*.sqlite3
18
+ test/dummy/db/*.sqlite3-journal
19
+ test/dummy/log/*.log
20
+ test/dummy/tmp/
21
+ test/dummy/.sass-cache
22
+
23
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
24
+ config/initializers/secret_token.rb
25
+ config/secrets.yml
26
+
27
+ # dotenv
28
+ # TODO Comment out this rule if environment variables can be committed
29
+ .env
30
+
31
+ ## Environment normalization:
32
+ /.bundle
33
+ /vendor/bundle
34
+
35
+ # these should all be checked in to normalize the environment:
36
+ # Gemfile.lock, .ruby-version, .ruby-gemset
37
+
38
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
39
+ .rvmrc
40
+
41
+ # if using bower-rails ignore default bower_components path bower.json files
42
+ /vendor/assets/bower_components
43
+ *.bowerrc
44
+ bower.json
45
+
46
+ # Ignore pow environment settings
47
+ .powenv
48
+
49
+ # Ignore Byebug command history file.
50
+ .byebug_history
51
+
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+
8
+ Metrics/ClassLength:
9
+ CountComments: false # count full line comments?
10
+ Max: 300
11
+
12
+ Metrics/ModuleLength:
13
+ CountComments: false # count full line comments?
14
+ Max: 300
15
+
16
+ Metrics/PerceivedComplexity:
17
+ Max: 10
18
+
19
+
20
+ Metrics/MethodLength:
21
+ CountComments: false # count full line comments?
22
+ Max: 20
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.2.0
5
+ - 2.3.0
6
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in rice_cooker.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
data/Gemfile.lock ADDED
@@ -0,0 +1,224 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rice_cooker (0.1.0)
5
+ actionpack (~> 5.0.0, < 5.1)
6
+ has_scope (~> 0.7.0, >= 0.6.0)
7
+ rails (~> 5.0.0, < 5.1)
8
+ railties (~> 5.0.0, < 5.1)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actioncable (5.0.0)
14
+ actionpack (= 5.0.0)
15
+ nio4r (~> 1.2)
16
+ websocket-driver (~> 0.6.1)
17
+ actionmailer (5.0.0)
18
+ actionpack (= 5.0.0)
19
+ actionview (= 5.0.0)
20
+ activejob (= 5.0.0)
21
+ mail (~> 2.5, >= 2.5.4)
22
+ rails-dom-testing (~> 2.0)
23
+ actionpack (5.0.0)
24
+ actionview (= 5.0.0)
25
+ activesupport (= 5.0.0)
26
+ rack (~> 2.0)
27
+ rack-test (~> 0.6.3)
28
+ rails-dom-testing (~> 2.0)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ actionview (5.0.0)
31
+ activesupport (= 5.0.0)
32
+ builder (~> 3.1)
33
+ erubis (~> 2.7.0)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
36
+ activejob (5.0.0)
37
+ activesupport (= 5.0.0)
38
+ globalid (>= 0.3.6)
39
+ activemodel (5.0.0)
40
+ activesupport (= 5.0.0)
41
+ activerecord (5.0.0)
42
+ activemodel (= 5.0.0)
43
+ activesupport (= 5.0.0)
44
+ arel (~> 7.0)
45
+ activesupport (5.0.0)
46
+ concurrent-ruby (~> 1.0, >= 1.0.2)
47
+ i18n (~> 0.7)
48
+ minitest (~> 5.1)
49
+ tzinfo (~> 1.1)
50
+ arel (7.0.0)
51
+ ast (2.3.0)
52
+ benchmark-ips (2.6.1)
53
+ builder (3.2.2)
54
+ coderay (1.1.1)
55
+ concurrent-ruby (1.0.2)
56
+ database_cleaner (1.5.3)
57
+ derailed (0.1.0)
58
+ derailed_benchmarks
59
+ derailed_benchmarks (1.3.1)
60
+ benchmark-ips (~> 2)
61
+ get_process_mem (~> 0)
62
+ heapy (~> 0)
63
+ memory_profiler (~> 0)
64
+ rack (>= 1)
65
+ rake (> 10, < 12)
66
+ thor (~> 0.19)
67
+ diff-lcs (1.2.5)
68
+ docile (1.1.5)
69
+ erubis (2.7.0)
70
+ factory_girl (4.7.0)
71
+ activesupport (>= 3.0.0)
72
+ factory_girl_rails (4.7.0)
73
+ factory_girl (~> 4.7.0)
74
+ railties (>= 3.0.0)
75
+ faker (1.6.1)
76
+ i18n (~> 0.5)
77
+ get_process_mem (0.2.1)
78
+ globalid (0.3.6)
79
+ activesupport (>= 4.1.0)
80
+ has_scope (0.7.0)
81
+ actionpack (>= 4.1, < 5.1)
82
+ activesupport (>= 4.1, < 5.1)
83
+ heapy (0.1.2)
84
+ i18n (0.7.0)
85
+ json (1.8.3)
86
+ loofah (2.0.3)
87
+ nokogiri (>= 1.5.9)
88
+ mail (2.6.4)
89
+ mime-types (>= 1.16, < 4)
90
+ memory_profiler (0.9.6)
91
+ metaclass (0.0.4)
92
+ method_source (0.8.2)
93
+ mime-types (3.1)
94
+ mime-types-data (~> 3.2015)
95
+ mime-types-data (3.2016.0521)
96
+ mini_portile2 (2.1.0)
97
+ minitest (5.9.0)
98
+ mocha (1.1.0)
99
+ metaclass (~> 0.0.1)
100
+ nio4r (1.2.1)
101
+ nokogiri (1.6.8)
102
+ mini_portile2 (~> 2.1.0)
103
+ pkg-config (~> 1.1.7)
104
+ parser (2.3.1.2)
105
+ ast (~> 2.2)
106
+ pkg-config (1.1.7)
107
+ power_assert (0.3.0)
108
+ powerpack (0.1.1)
109
+ pry (0.10.3)
110
+ coderay (~> 1.1.0)
111
+ method_source (~> 0.8.1)
112
+ slop (~> 3.4)
113
+ rack (2.0.1)
114
+ rack-test (0.6.3)
115
+ rack (>= 1.0)
116
+ rails (5.0.0)
117
+ actioncable (= 5.0.0)
118
+ actionmailer (= 5.0.0)
119
+ actionpack (= 5.0.0)
120
+ actionview (= 5.0.0)
121
+ activejob (= 5.0.0)
122
+ activemodel (= 5.0.0)
123
+ activerecord (= 5.0.0)
124
+ activesupport (= 5.0.0)
125
+ bundler (>= 1.3.0, < 2.0)
126
+ railties (= 5.0.0)
127
+ sprockets-rails (>= 2.0.0)
128
+ rails-dom-testing (2.0.1)
129
+ activesupport (>= 4.2.0, < 6.0)
130
+ nokogiri (~> 1.6.0)
131
+ rails-html-sanitizer (1.0.3)
132
+ loofah (~> 2.0)
133
+ rails-perftest (0.0.6)
134
+ railties (5.0.0)
135
+ actionpack (= 5.0.0)
136
+ activesupport (= 5.0.0)
137
+ method_source
138
+ rake (>= 0.8.7)
139
+ thor (>= 0.18.1, < 2.0)
140
+ rainbow (2.1.0)
141
+ rake (11.2.2)
142
+ rspec (3.5.0)
143
+ rspec-core (~> 3.5.0)
144
+ rspec-expectations (~> 3.5.0)
145
+ rspec-mocks (~> 3.5.0)
146
+ rspec-activemodel-mocks (1.0.3)
147
+ activemodel (>= 3.0)
148
+ activesupport (>= 3.0)
149
+ rspec-mocks (>= 2.99, < 4.0)
150
+ rspec-core (3.5.0)
151
+ rspec-support (~> 3.5.0)
152
+ rspec-expectations (3.5.0)
153
+ diff-lcs (>= 1.2.0, < 2.0)
154
+ rspec-support (~> 3.5.0)
155
+ rspec-mocks (3.5.0)
156
+ diff-lcs (>= 1.2.0, < 2.0)
157
+ rspec-support (~> 3.5.0)
158
+ rspec-rails (3.5.0)
159
+ actionpack (>= 3.0)
160
+ activesupport (>= 3.0)
161
+ railties (>= 3.0)
162
+ rspec-core (~> 3.5.0)
163
+ rspec-expectations (~> 3.5.0)
164
+ rspec-mocks (~> 3.5.0)
165
+ rspec-support (~> 3.5.0)
166
+ rspec-support (3.5.0)
167
+ rubocop (0.40.0)
168
+ parser (>= 2.3.1.0, < 3.0)
169
+ powerpack (~> 0.1)
170
+ rainbow (>= 1.99.1, < 3.0)
171
+ ruby-progressbar (~> 1.7)
172
+ unicode-display_width (~> 1.0, >= 1.0.1)
173
+ ruby-prof (0.15.8)
174
+ ruby-progressbar (1.8.1)
175
+ simplecov (0.11.1)
176
+ docile (~> 1.1.0)
177
+ json (~> 1.8)
178
+ simplecov-html (~> 0.10.0)
179
+ simplecov-html (0.10.0)
180
+ slop (3.6.0)
181
+ sprockets (3.6.3)
182
+ concurrent-ruby (~> 1.0)
183
+ rack (> 1, < 3)
184
+ sprockets-rails (3.1.1)
185
+ actionpack (>= 4.0)
186
+ activesupport (>= 4.0)
187
+ sprockets (>= 3.0.0)
188
+ sqlite3 (1.3.11)
189
+ stackprof (0.2.9)
190
+ test-unit (3.1.3)
191
+ power_assert
192
+ thor (0.19.1)
193
+ thread_safe (0.3.5)
194
+ tzinfo (1.2.2)
195
+ thread_safe (~> 0.1)
196
+ unicode-display_width (1.1.0)
197
+ websocket-driver (0.6.4)
198
+ websocket-extensions (>= 0.1.0)
199
+ websocket-extensions (0.1.2)
200
+
201
+ PLATFORMS
202
+ ruby
203
+
204
+ DEPENDENCIES
205
+ database_cleaner
206
+ derailed
207
+ factory_girl_rails (~> 4.0)
208
+ faker (= 1.6.1)
209
+ mocha (= 1.1.0)
210
+ pry
211
+ rails-perftest (= 0.0.6)
212
+ rice_cooker!
213
+ rspec
214
+ rspec-activemodel-mocks
215
+ rspec-rails
216
+ rubocop (~> 0.40.0)
217
+ ruby-prof (= 0.15.8)
218
+ simplecov (= 0.11.1)
219
+ sqlite3
220
+ stackprof
221
+ test-unit (= 3.1.3)
222
+
223
+ BUNDLED WITH
224
+ 1.12.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 André Aubin
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Rice Cooker
2
+
3
+ [![Build Status](https://travis-ci.com/lambda2/rice_cooker.svg?token=zsj9q6JjpQd8brNcmt9S&branch=master)](https://travis-ci.com/lambda2/rice_cooker)
4
+
5
+ Handle sort, filters, searches, and ranges on Rails collections.
6
+
7
+ -------------------
8
+
9
+ ## Installation
10
+
11
+ In your `Gemfile`:
12
+
13
+ ```ruby
14
+ gem 'rice_cooker'
15
+ ```
16
+
17
+ Then, in your controllers
18
+
19
+ ```ruby
20
+
21
+ class UsersController < ActionController::Base
22
+
23
+ # Will handle sorting with the 'sort' parameter.
24
+ sorted
25
+
26
+ # Will handle filtering with the 'filter[]' parameter.
27
+ filtered
28
+
29
+ # Will handle range-ing with the 'range[]' parameter.
30
+ ranged
31
+
32
+ def index
33
+ @users = apply_scopes(User).all
34
+ render json: @users
35
+ end
36
+ end
37
+
38
+ ```
data/Rakefile ADDED
@@ -0,0 +1,28 @@
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 = 'RiceCooker'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
27
+
28
+ task default: :test
@@ -0,0 +1,97 @@
1
+ require 'active_support'
2
+
3
+ module RiceCooker
4
+ module Filter
5
+ extend ActiveSupport::Concern
6
+
7
+ FILTER_PARAM = :filter
8
+
9
+ module ClassMethods
10
+ include Helpers
11
+
12
+ def filtered(additional_filtering_params = {})
13
+ cattr_accessor :filtering_keys
14
+ cattr_accessor :custom_filters
15
+
16
+ resource_class ||= controller_resource_class(self)# unless respond_to?(:resource_class)
17
+
18
+ # On normalize tout ca
19
+ additional_filtering_params = format_additional_param(additional_filtering_params, 'filtering')
20
+
21
+ # On recupere tous les filtres autorisés
22
+ allowed_keys = (filterable_fields_for(resource_class) + additional_filtering_params.keys)
23
+
24
+ # On fait une sorte de *register_bool_filter* sur tous les champs *_at
25
+ additional = (filterable_fields_for(resource_class) - [:created_at, :updated_at])
26
+ .select { |e| e =~ /_at$/ }
27
+ .select { |e| additional_filtering_params[e.to_s.gsub(/_at$/, '')].nil? }
28
+
29
+ additional.each do |fi|
30
+ name = fi.to_s.gsub(/_at$/, '')
31
+
32
+ if fi.to_sym == :begin_at
33
+ db_field = "#{resource_class.quoted_table_name}.\"#{fi}\""
34
+ additional_filtering_params[:future] = {
35
+ proc: -> (value) { value.first == 'true' ? where("#{db_field} >= ?", Time.zone.now) : where("#{db_field} < ?", Time.zone.now) },
36
+ all: %w(true false),
37
+ description: "Return only #{resource_class.to_s.underscore.humanize.downcase.pluralize} which begins in the future"
38
+ }
39
+ allowed_keys << :future
40
+ else
41
+ additional_filtering_params[name.to_sym] = {
42
+ proc: -> (value) { value.first == 'true' ? where.not(fi => nil) : where(fi => nil) },
43
+ all: %w(true false),
44
+ description: "Return only #{name} #{resource_class.to_s.underscore.humanize.downcase.pluralize}"
45
+ }
46
+ allowed_keys << name
47
+ end
48
+ end
49
+
50
+ # On recupere le default
51
+ self.filtering_keys = allowed_keys
52
+ self.custom_filters = additional_filtering_params
53
+
54
+ has_scope :filter, type: :hash, only: [:index] do |_controller, scope, value|
55
+ params = parse_filtering_param(value, filtering_keys)
56
+ scope = apply_filter_to_collection(scope, params, custom_filters)
57
+ scope
58
+ end
59
+ end
60
+
61
+ #
62
+ # Ajoute un filtre custom
63
+ #
64
+ # name: le nom du filtre custom (ex: with_mark)
65
+ # proc: le filtre, prend un arg `val` qui est un tableau des args du filtre
66
+ # all: l'ensemble des valeurs acceptées pour le filtre. Laisser nil ou [] pour tout accepter
67
+ # description: La description dans la doc
68
+ #
69
+ def register_filter(name, proc, all = nil, description = nil)
70
+ raise "A '#{name}' filter already exists for class #{self.class}" unless custom_filters[name].nil?
71
+ custom_filters[name] = {
72
+ proc: proc,
73
+ all: all || [],
74
+ description: description || ''
75
+ }
76
+ filtering_keys << name
77
+ end
78
+
79
+ #
80
+ # Raccourci pour un filtre custom qui va filtrer si un champ est nil ou non.
81
+ #
82
+ # name: le nom du filtre custom (ex: with_mark)
83
+ # field: le champ a filtrer sur `nil` ou pas `nil` (ex: final_mark)
84
+ # description: La description dans la doc
85
+ #
86
+ def register_bool_filter(name, field, description = nil)
87
+ raise "A '#{name}' filter already exists for class #{self.class}" unless custom_filters[name].nil?
88
+ custom_filters[name] = {
89
+ proc: -> (value) { value.first == 'true' ? where.not(field => nil) : where(field => nil) },
90
+ all: %w(true false),
91
+ description: description || "Return only #{resource_class.to_s.underscore.humanize.downcase.pluralize} with a #{field}"
92
+ }
93
+ filtering_keys << name
94
+ end
95
+ end
96
+ end
97
+ end