ransack_ui 1.0.2 → 1.1.0

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.
@@ -33,6 +33,9 @@ Ransack.predicate_inputs = {}
33
33
  when 'date','datetime','time' then type
34
34
  else false # Hide for unhandled types.
35
35
 
36
+ # Setup predicates for fixed select options. Includes relevant any/all permutations
37
+ Ransack.option_predicates = ['eq', 'eq_any', 'not_eq', 'not_eq_all', 'null', 'not_null']
38
+
36
39
  # Use a tags input for 'in' if Select2 is available
37
40
  if Select2?
38
41
  Ransack.predicate_inputs.in = 'tags'
@@ -32,6 +32,7 @@
32
32
 
33
33
  base_id = target.attr('id').slice(0, -8)
34
34
  predicate_select = @element.find("select##{base_id}p")
35
+ available = predicate_select.data['predicates']
35
36
  query_input = $("input##{base_id}v_0_value")
36
37
 
37
38
  # Initialize datepicker if column is date/datetime/time
@@ -40,7 +41,7 @@
40
41
  # Handle association columns with AJAX autocomplete
41
42
  if selected.data('ajax-url') and Select2?
42
43
  controller = selected.data('controller')
43
- @force_eq_predicate(base_id)
44
+ @set_option_predicates(base_id, available, column_type)
44
45
 
45
46
  # Set up Select2 for query input
46
47
  query_input.val('')
@@ -61,7 +62,7 @@
61
62
 
62
63
  # Handle columns with options detected from validates :inclusion
63
64
  else if selected.data('select-options') and Select2?
64
- @force_eq_predicate(base_id)
65
+ @set_option_predicates(base_id, available, column_type)
65
66
  query_input.val('')
66
67
  query_input.select2
67
68
  data: selected.data('select-options')
@@ -83,7 +84,6 @@
83
84
  previous_val = predicate_select.val()
84
85
 
85
86
  # Build array of supported predicates
86
- available = predicate_select.data['predicates']
87
87
  predicates = Ransack.type_predicates[column_type] || []
88
88
  predicates = $.map predicates, (p) -> [p, Ransack.predicates[p]]
89
89
 
@@ -114,12 +114,16 @@
114
114
 
115
115
  return true
116
116
 
117
+
117
118
  predicate_changed: (e) ->
118
119
  target = $(e.currentTarget)
119
120
  p = target.val() || ""
120
121
  base_id = target.attr('id').slice(0, -1)
121
122
  query_input = $("input##{base_id}v_0_value")
122
123
 
124
+ attribute_select = @element.find("select##{base_id}a_0_name")
125
+ selected_attribute = attribute_select.find('option:selected')
126
+
123
127
  # We need to use a dummy input to handle multiple terms
124
128
  multi_id = query_input.attr('id') + '_multi'
125
129
  multi_input = @element.find("##{multi_id}")
@@ -127,18 +131,50 @@
127
131
  if Select2?
128
132
  # Turn query input into Select2 tag list when query accepts multiple values
129
133
  if p in ["in", "not_in"] || p.match(/_(all|any)$/)
134
+ # If Select2 is on query input, remove and set defaults
135
+ if @element.find("#s2id_#{base_id}v_0_value").length
136
+ query_input.select2('destroy')
137
+
130
138
  # Add dummy 'multi' input for select2 if not already added
131
139
  if multi_input.length == 0
132
- @setup_multi_query_input(target, query_input, multi_id)
140
+ # Set up multi-query input with fixed options, if present
141
+ query_input.val('')
142
+ options = selected_attribute.data('select-options')
143
+ @setup_multi_query_input(target, query_input, multi_id, options)
144
+
133
145
  return
134
- else
135
- if multi_input.length > 0
136
- # Otherwise, remove Select2 from multi-query input, and remove input
146
+
147
+ else
148
+ if multi_input.length
149
+ # Otherwise, remove Select2 from multi-query input, and remove input.
137
150
  multi_input.select2('destroy').remove()
151
+ # Also remove all extra inputs
152
+ base_name = multi_input.data('base-name')
153
+ inputs = @element.find("input[name^=\"#{base_name}\"]")
154
+ inputs = inputs.slice(1)
155
+ inputs.remove()
156
+
157
+ # If no fixed options, show query input
138
158
  query_input.val('').show()
159
+
160
+ # Handle fixed options - set up Select2 for single values if not already present
161
+ if selected_attribute.data('select-options')
162
+ if (query_select2 = @element.find("#s2id_#{base_id}v_0_value")).length == 0
163
+ query_input.select2
164
+ data: selected_attribute.data('select-options')
165
+ placeholder: "Please select a #{selected_attribute.val()}"
166
+ allowClear: true
167
+
168
+ return
169
+
139
170
 
140
171
  # Hide query input when not needed
141
172
  if p in ["true", "false", "blank", "present", "null", "not_null"]
173
+ # If Select2 is on query input, remove and set defaults
174
+ if Select2? && @element.find("#s2id_#{base_id}v_0_value").length
175
+ query_input.select2('destroy')
176
+ query_input.val('')
177
+
142
178
  query_input.val("true")
143
179
  query_input.hide()
144
180
  query_input.parent().find('.ui-datepicker-trigger').hide()
@@ -151,16 +187,22 @@
151
187
 
152
188
 
153
189
  # Dsiables predicate choices and sets it to 'eq'
154
- force_eq_predicate: (base_id) ->
190
+ set_option_predicates: (base_id, available, column_type) ->
155
191
  predicate_select = @element.find("select##{base_id}p")
156
- # Clear predicates, and set 'eq' predicate
192
+
193
+ # Remove all predicates, and add any supported predicates
157
194
  predicate_select.find('option').each (i, o) -> $(o).remove()
158
- predicate_select.append $('<option selected="selected" value="eq">is</option>')
195
+
196
+ $.each available, (i, p) =>
197
+ [predicate, label] = [p[0], p[1]]
198
+
199
+ if predicate in Ransack.option_predicates
200
+ # Get alternative predicate label depending on column type
201
+ label = @alt_predicate_label_or_default(predicate, column_type, label)
202
+ predicate_select.append $("<option value=#{predicate}>#{label}</option>")
159
203
 
160
204
  if Select2?
161
205
  predicate_select2 = @element.find("#s2id_#{base_id}p")
162
- # Disable predicate Select2
163
- predicate_select2.select2('disable')
164
206
  predicate_select2.select2('val', 'eq')
165
207
 
166
208
  # Attempts to find a predicate translation for the specific column type,
@@ -196,7 +238,7 @@
196
238
 
197
239
  # If value was added after the first value, then append extra input with added value
198
240
  if values.length && e.added
199
- @add_query_input(target, base_name, inputs.length + 1, e.added.text)
241
+ @add_query_input(target, base_name, inputs.length + 1, e.added.id)
200
242
 
201
243
  else if e.removed
202
244
  # If value was removed, clear all extra inputs, then rebuild inputs for extra terms
@@ -204,7 +246,7 @@
204
246
  $.each values, (i, v) =>
205
247
  @add_query_input(target, base_name, i + 1, v)
206
248
 
207
- setup_multi_query_input: (predicate_el, query_el, multi_id) ->
249
+ setup_multi_query_input: (predicate_el, query_el, multi_id, options) ->
208
250
  base_name = predicate_el.attr('name').slice(0, -3) + '[v]'
209
251
  query_el.after(
210
252
  $('<input class="ransack_query_multi" id="' + multi_id + '" ' +
@@ -221,11 +263,32 @@
221
263
  if Select2?
222
264
  # Find newly created input and setup Select2
223
265
  multi_query = @element.find('#' + multi_id)
224
- multi_query.select2
225
- tags: []
226
- tokenSeparators: [',']
227
- formatNoMatches: (t) ->
228
- "Add a search term"
266
+
267
+ if options?
268
+ # Setup Select2 with fixed options
269
+ multi_query.select2
270
+ data: options
271
+ multiple: true
272
+ tokenSeparators: [',']
273
+ formatNoMatches:
274
+ if options
275
+ (t) -> "No matches found."
276
+ else
277
+ (t) -> "Add a search term"
278
+ initSelection: (element, callback) ->
279
+ data = []
280
+ unless element.val().trim() == ""
281
+ $(element.val().split(",")).each ->
282
+ data.push {id: this, text: this}
283
+ callback(data)
284
+ else
285
+ # Setup Select2 with tagging support (can create options)
286
+ multi_query.select2
287
+ tags: []
288
+ tokenSeparators: [',']
289
+ formatNoMatches: (t) ->
290
+ "Add a search term"
291
+
229
292
  multi_query.select2('val', values)
230
293
 
231
294
  add_query_input: (base_input, base_name, id, value) ->
@@ -1,3 +1,3 @@
1
1
  module RansackUI
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-27 00:00:00.000000000 Z
12
+ date: 2013-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ransack_chronic
@@ -99,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
99
  version: '0'
100
100
  segments:
101
101
  - 0
102
- hash: 1751889683287985587
102
+ hash: 3625052068884160403
103
103
  required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  none: false
105
105
  requirements:
@@ -108,10 +108,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  segments:
110
110
  - 0
111
- hash: 1751889683287985587
111
+ hash: 3625052068884160403
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 1.8.21
114
+ rubygems_version: 1.8.24
115
115
  signing_key:
116
116
  specification_version: 3
117
117
  summary: UI Builder for Ransack