collate 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 984410b26595114259b1b7f04b911e545fcf9c1a
4
- data.tar.gz: 7c70901aee7e073d5ce119e14c67b219505ec1b2
3
+ metadata.gz: f6b83915624fb82cd112faccebbd22f5d298f651
4
+ data.tar.gz: 959313f8d709d29499a7f8aad05e7187ee98c5e2
5
5
  SHA512:
6
- metadata.gz: 2c53dd532b38d172b5b278c80c073b5dfec8c306d2fa31db7e57aaa66afceff938f0f3be70437edfc574ece0dabdba9cb1bfb2f167ddc66f2f2967ad32c252d7
7
- data.tar.gz: 8e9a7c5e833e0f6e5a627ebcc78b9286676b7a0fdacfa4c14219e8d387867fd05da2b2ce51c5c87afa01489d1c581c31e26b30769e94f7b5392f3539e49eda04
6
+ metadata.gz: b3ec77b60c452a7e81c0ad455a85d5b00c91c8cec73da5ce8433ae9bce4ba0765031c5cbbad4e5c8ea1fdc80464282a5efbf0b0f7e743be1351c073686d55026
7
+ data.tar.gz: b167d550eb09ca697e0d99d373574926bf1448f360272470b3d0ca78fa013fee841660de221d43fe6241789611153e0845a9a1ea6bf9fa738d34d0e1ead19573
data/README.md CHANGED
@@ -134,6 +134,7 @@ Here are the available value transformations:
134
134
  | Transformation | Behavior |
135
135
  |:-------------------------|:--------------------|
136
136
  | ```:join``` | ```value = value.join(arg1)``` |
137
+ | ```:as_array``` | ```value = "{#{value}}"``` |
137
138
  | ```:downcase``` | ```value = value.downcase``` |
138
139
  | ```:string_part``` | ```value = "%#{value}%"``` |
139
140
 
@@ -149,6 +150,18 @@ collate_on :name, label: 'Character Name'
149
150
 
150
151
  This argument will overwrite the default label for the filter, which is ```field.to_s.titleize```
151
152
 
153
+ #### Or
154
+ --------------
155
+ ```
156
+ collate_on :name, or: true
157
+ ```
158
+
159
+ This argument causes the query to use "OR" for each element of the filter value array passed in from the user. For example, if the user passed in ```["John Doe", "Jane Doe"]``` as the parameter for the above collation, this is the PostgreSQL query that would result:
160
+
161
+ ```
162
+ WHERE name = "John Doe" OR name = "Jane Doe"
163
+ ```
164
+
152
165
  #### Not
153
166
  --------------
154
167
  ```
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  end
23
23
 
24
24
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
- f.match(%r{^(test|spec|features|app|coverage|config|log)/})
25
+ f.match(%r{^(test|spec|features|app/controllers|app/models|app/views/actors|app/views/movies|coverage|config|log)/})
26
26
  end
27
27
  spec.bindir = "exe"
28
28
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
@@ -0,0 +1,5 @@
1
+ %label.custom-control.custom-checkbox
2
+ = check_box_tag filter.param_key, nil, params[filter.param_key], id: "#{filter.html_id}", class: 'custom-control-input'
3
+ %span.custom-control-indicator
4
+ %span.custom-control-description
5
+ = filter.label
@@ -0,0 +1,8 @@
1
+ = filter.label
2
+ %br
3
+ - filter.component[:values].each do |value|
4
+ %label.custom-control.custom-checkbox
5
+ = check_box_tag "#{filter.param_key}[]", value[:id], ([] + params[filter.param_key].to_a).include?(value[:id]), class: 'custom-control-input'
6
+ %span.custom-control-indicator
7
+ %span.custom-control-description
8
+ = value[:text]
@@ -0,0 +1,3 @@
1
+ = filter.label
2
+ %br
3
+ = text_field_tag filter.param_key, params[filter.param_key], id: "#{filter.html_id}"
@@ -0,0 +1,5 @@
1
+ = filter.label
2
+ %br
3
+ - select_params = [{style: 'width:100%', id: "#{filter.html_id}"}]
4
+ - select_params[0].merge!({multiple: "multiple"}) if filter.component[:multiple]
5
+ = select_tag "#{filter.param_key}[]", options_for_select(filter.component[:values].map { |o| params[filter.param_key].to_a.include?(o[:id].to_s) ? [o[:text], o[:id], selected: "selected"] : [o[:text], o[:id]] } ), *select_params
@@ -0,0 +1,3 @@
1
+ = filter.label
2
+ %br
3
+ = text_field_tag filter.param_key, params[filter.param_key], id: "#{filter.html_id}", style:'width:100%'
@@ -0,0 +1,15 @@
1
+ module Collate
2
+ module ActionViewExtension
3
+ def filters_for_group record, group_key
4
+ groups = record.model.collate_filters ||= {}
5
+
6
+ group = groups[group_key]
7
+
8
+ filters = group[:filters] ||= []
9
+ end
10
+
11
+ def filter_for filter
12
+ render :partial => "collate/#{filter.component[:type]}_field", locals: {filter: filter}
13
+ end
14
+ end
15
+ end
@@ -47,7 +47,13 @@ module Collate
47
47
  self.group_options ||= {}
48
48
  end
49
49
 
50
- def apply_filter ar_rel, filter, filter_value
50
+ def apply_filter ar_rel, filter, param_value
51
+ filter_value = if param_value.duplicable?
52
+ param_value.dup
53
+ else
54
+ param_value
55
+ end
56
+
51
57
  if filter.joins
52
58
  filter.joins.each do |join|
53
59
  ar_rel = if filter.joins_prefix
@@ -125,16 +131,26 @@ module Collate
125
131
  transformation = vt
126
132
  transformation = vt[0] if !transformation.is_a? Symbol
127
133
 
128
- filter_value = case transformation
129
- when :join
130
- "{#{filter_value.join(', ')}}"
131
- when :downcase
132
- filter_value.downcase
133
- when :string_part
134
- "%#{filter_value}%"
135
- else
136
- filter_value
134
+ filter_value = [filter_value] unless filter.or
135
+
136
+ filter_value.each_with_index do |f, i|
137
+ filter_value[i] = case transformation
138
+ when :join
139
+ "#{f.join(vt[1])}"
140
+ when :as_array
141
+ "{#{f}}"
142
+ when :downcase
143
+ f.downcase
144
+ when :string_part
145
+ "%#{f}%"
146
+ when :to_json
147
+ "{\"#{vt[1]}\": \"#{f}\"}"
148
+ else
149
+ f
150
+ end
137
151
  end
152
+ filter_value = filter_value[0] unless filter.or
153
+
138
154
  end
139
155
 
140
156
  ar_method = filter.having ? "having" : "where"
@@ -162,10 +178,15 @@ module Collate
162
178
  ""
163
179
  end
164
180
 
181
+ query_string = filter_value.length.times.collect{query_string}.join(' OR ') if filter.or
165
182
  query_string = "NOT(#{query_string})" if filter.not
166
183
 
167
184
  ar_rel = if query_string.include?('?')
168
- ar_rel.send(ar_method, query_string, filter_value)
185
+ if filter.or
186
+ ar_rel.send(ar_method, query_string, *filter_value)
187
+ else
188
+ ar_rel.send(ar_method, query_string, filter_value)
189
+ end
169
190
  else
170
191
  ar_rel.send(ar_method, query_string)
171
192
  end
@@ -1,4 +1,5 @@
1
1
  require_relative 'active_record_extension'
2
+ require_relative 'action_view_extension'
2
3
  require 'rails'
3
4
 
4
5
  module Collate
@@ -6,9 +7,16 @@ module Collate
6
7
 
7
8
  isolate_namespace Collate
8
9
 
10
+ ActiveSupport.on_load :action_view do
11
+ include Collate::ActionViewExtension
12
+ end
13
+
9
14
  ActiveSupport.on_load :active_record do
10
15
  extend Collate::ActiveRecordExtension
11
16
  end
12
17
 
18
+ if defined? ActionController::Base
19
+ ActionController::Base.prepend_view_path File.dirname(__FILE__) + "/../app/views"
20
+ end
13
21
  end
14
22
  end
@@ -4,11 +4,11 @@ module Collate
4
4
 
5
5
  FIELD_TRANSFORMATIONS = [:date_difference, :date_part, :array_agg, :downcase, :split, :array_length]
6
6
  AGGREGATE_TRANSFORMATIONS = [:array_agg]
7
- VALUE_TRANSFORMATIONS = [:join, :downcase, :string_part]
7
+ VALUE_TRANSFORMATIONS = [:join, :downcase, :string_part, :to_json]
8
8
 
9
9
  attr_accessor :field, :operator, :base_model_table_name, :field_transformations, :label,
10
10
  :component, :joins, :value_transformations, :grouping, :html_id, :having,
11
- :joins_prefix, :not
11
+ :joins_prefix, :not, :or
12
12
 
13
13
  def initialize(field, opt={})
14
14
  opt.each do |field, value|
@@ -1,3 +1,3 @@
1
1
  module Collate
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Page
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-02-04 00:00:00.000000000 Z
12
+ date: 2017-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -172,7 +172,13 @@ files:
172
172
  - circle.yml
173
173
  - collate.gemspec
174
174
  - deploy.sh
175
+ - lib/app/views/collate/_checkbox_field.html.haml
176
+ - lib/app/views/collate/_checkboxgroup_field.html.haml
177
+ - lib/app/views/collate/_date_field.html.haml
178
+ - lib/app/views/collate/_select_field.html.haml
179
+ - lib/app/views/collate/_string_field.html.haml
175
180
  - lib/collate.rb
181
+ - lib/collate/action_view_extension.rb
176
182
  - lib/collate/active_record_extension.rb
177
183
  - lib/collate/engine.rb
178
184
  - lib/collate/filter.rb