ransack_ui 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@
8
8
  el.on 'click', '.remove_fields', $.proxy(this.remove_fields, this)
9
9
  el.on 'change', 'select.ransack_predicate', $.proxy(this.predicate_changed, this)
10
10
  el.on 'change', 'select.ransack_attribute', $.proxy(this.attribute_changed, this)
11
+ el.on 'click focus', 'input.ransack_query', $.proxy(this.query_focus, this)
11
12
 
12
13
  # Set up Select2 on select lists in .filters
13
14
  this.init_select2(this.element.find('.filters'))
@@ -26,7 +27,8 @@
26
27
 
27
28
  predicate_changed: (e) ->
28
29
  target = $(e.currentTarget)
29
- query_input = $('input#' + target.attr('id').slice(0, -1) + "v_0_value")
30
+ base_id = target.attr('id').slice(0, -1)
31
+ query_input = $('input#' + base_id + "v_0_value")
30
32
  if target.val() in ["true", "false", "blank", "present", "null", "not_null"]
31
33
  query_input.val("true")
32
34
  query_input.hide()
@@ -38,12 +40,16 @@
38
40
  attribute_changed: (e) ->
39
41
  target = $(e.currentTarget)
40
42
  selected = target.find('option:selected')
43
+ column_type = selected.data('type')
41
44
 
42
45
  base_id = target.attr('id').slice(0, -8)
43
46
  predicate_select = this.element.find('select#' + base_id + 'p')
44
47
  predicate_select2 = this.element.find('#s2id_' + base_id + 'p')
45
48
  query_input = $('input#' + base_id + "v_0_value")
46
49
 
50
+ # Clear any datepicker from query input
51
+ query_input.datepicker('destroy')
52
+
47
53
  if selected.data('ajax-url') and Select2?
48
54
  controller = selected.data('controller')
49
55
 
@@ -81,15 +87,17 @@
81
87
 
82
88
  # Build array of supported predicates
83
89
  available = predicate_select.data['predicates']
84
- predicates = Ransack.type_predicates[selected.data('type')] || []
90
+ predicates = Ransack.type_predicates[column_type] || []
85
91
  predicates = $.map predicates, (p) -> [p, Ransack.predicates[p]]
86
92
 
87
93
  # Remove all predicates, and add any supported predicates
88
94
  predicate_select.find('option').each (i, o) -> $(o).remove()
89
95
 
90
- $.each available, (i, p) ->
96
+ $.each available, (i, p) =>
91
97
  [val, label] = [p[0], p[1]]
92
98
  if val in predicates
99
+ # Get alternative predicate label depending on column type
100
+ label = this.alt_predicate_label_or_default(val, column_type, label)
93
101
  predicate_select.append $('<option value='+val+'>'+label+'</option>')
94
102
 
95
103
  # Select first predicate if current selection is invalid
@@ -100,6 +108,51 @@
100
108
 
101
109
  return true
102
110
 
111
+ query_focus: (e) ->
112
+ if $.ui?.timepicker?
113
+ target = $(e.currentTarget)
114
+ base_id = target.attr('id').slice(0, -9)
115
+ query_input = $('input#' + base_id + "v_0_value")
116
+
117
+ # Only set up new datepicker if not already initialized
118
+ if query_input.not('.hasDatePicker')
119
+ selected_attr = this.element.find('select#' + base_id + 'a_0_name option:selected')
120
+
121
+ datepicker_options =
122
+ changeMonth: true
123
+ constrainInput: false
124
+ dateFormat: 'yy-mm-dd'
125
+ # Always prefer custom input text over selected date
126
+ onClose: (date) -> $(this).val(date)
127
+
128
+ switch selected_attr.data('type')
129
+ when "date"
130
+ query_input.datepicker(datepicker_options)
131
+ when "datetime"
132
+ query_input.datetimepicker(datepicker_options)
133
+ when "time"
134
+ query_input.datetimepicker $.extend(datepicker_options, {timeOnly: true})
135
+
136
+ # Attempts to find a predicate translation for the specific column type,
137
+ # or returns the default label.
138
+ # For example, 'lt' on an integer column will be translated to 'is less than',
139
+ # while a date column will have it translated as 'is before'.
140
+ # This is mainly to avoid confusion when building conditions using Chronic strings.
141
+ # 'created_at is less than 2 weeks ago' is misleading, and
142
+ # 'created_at is before 2 weeks ago' is much easier to understand.
143
+ alt_predicate_label_or_default: (p, type, default_label) ->
144
+ return default_label unless Ransack?.alt_predicates_i18n?
145
+
146
+ alt_labels = {}
147
+ switch type
148
+ when "date", "datetime", "time"
149
+ alt_labels = Ransack.alt_predicates_i18n["date"] || {}
150
+ else
151
+ alt_labels = Ransack.alt_predicates_i18n[type] || {}
152
+
153
+ alt_labels[p] || default_label
154
+
155
+
103
156
  form_submit: (e) ->
104
157
  $("#loading").show()
105
158
  this.element.css({ opacity: 0.4 })
@@ -149,8 +202,8 @@
149
202
  placeholder: "Select a Field"
150
203
  allowClear: true
151
204
  formatSelection: (object, container) ->
152
- # Return 'Model: field' if column is not on base model
153
- if $(object.element).data('base')
205
+ # Return 'Model: field' unless column is on root model
206
+ if $(object.element).data('root-model')
154
207
  object.text
155
208
  else
156
209
  group_label = $(object.element).parent().attr('label')
@@ -1,5 +1,9 @@
1
1
  = search_form_for search, :url => url_for(:action => :index), :html => {:method => :get, :class => "advanced_search"}, :remote => true do |f|
2
2
 
3
+ :javascript
4
+ if (window.Ransack == null) { window.Ransack = {}; }
5
+ Ransack.alt_predicates_i18n = #{I18n.translate(:"ransack.predicates.alt", :default => {}).to_json}
6
+
3
7
  = f.grouping_fields do |g|
4
8
  = render 'ransack/grouping_fields', :f => g
5
9
 
@@ -10,4 +10,4 @@
10
10
 
11
11
  = f.value_fields do |v|
12
12
  %span.fields.value{ 'data-object-name' => f.object_name }
13
- = v.text_field :value, :style => "width: 200px;"
13
+ = v.text_field :value, :style => "width: 200px;", :class => "ransack_query"
@@ -0,0 +1,17 @@
1
+ en-US:
2
+ ransack:
3
+ predicates:
4
+ alt:
5
+ date:
6
+ lt: "is before"
7
+ lt_any: "is before any"
8
+ lt_all: "is before all"
9
+ lteq: "is before or on"
10
+ lteq_any: "is before or on any"
11
+ lteq_all: "is before or on all"
12
+ gt: "is after"
13
+ gt_any: "ais fter any"
14
+ gt_all: "is after all"
15
+ gteq: "is after or on"
16
+ gteq_any: "is after or on any"
17
+ gteq_all: "is after or on all"
@@ -0,0 +1,17 @@
1
+ en:
2
+ ransack:
3
+ predicates:
4
+ alt:
5
+ date:
6
+ lt: "is before"
7
+ lt_any: "is before any"
8
+ lt_all: "is before all"
9
+ lteq: "is before or on"
10
+ lteq_any: "is before or on any"
11
+ lteq_all: "is before or on all"
12
+ gt: "is after"
13
+ gt_any: "ais fter any"
14
+ gt_all: "is after all"
15
+ gteq: "is after or on"
16
+ gteq_any: "is after or on any"
17
+ gteq_all: "is after or on all"
@@ -81,7 +81,7 @@ module Ransack
81
81
  # Add column type as data attribute
82
82
  html_options = {:'data-type' => type}
83
83
  # Set 'base' attribute if attribute is on base model
84
- html_options[:'data-base'] = true if base.blank?
84
+ html_options[:'data-root-model'] = true if base.blank?
85
85
 
86
86
  if foreign_klass
87
87
  # If field is a foreign key, set up 'data-ajax-*' attributes for auto-complete
@@ -0,0 +1,32 @@
1
+ require 'ransack/nodes'
2
+
3
+ # Add Chronic parsing to date and time casting
4
+ module Ransack
5
+ module Nodes
6
+ Value.class_eval do
7
+ def cast_to_date(val)
8
+ if val.is_a?(String)
9
+ Chronic.parse(val).in_time_zone.to_date rescue nil
10
+ elsif val.respond_to?(:to_date)
11
+ val.to_date rescue nil
12
+ else
13
+ y, m, d = *[val].flatten
14
+ m ||= 1
15
+ d ||= 1
16
+ Date.new(y,m,d) rescue nil
17
+ end
18
+ end
19
+
20
+ def cast_to_time(val)
21
+ if val.is_a?(Array)
22
+ Time.zone.local(*val) rescue nil
23
+ else
24
+ unless val.acts_like?(:time)
25
+ val = val.is_a?(String) ? Chronic.parse(val) : val.to_time rescue nil
26
+ end
27
+ val.in_time_zone rescue nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module RansackUI
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/ransack_ui.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "ransack_ui/version"
2
2
  require "ransack_ui/rails/engine"
3
3
  require "core_ext/enumerable"
4
+ require "chronic"
4
5
 
5
6
  # Require ransack overrides
6
7
  Dir.glob(File.expand_path('../ransack_ui/ransack_overrides/**/*.rb', __FILE__)) {|f| require f }
data/ransack_ui.gemspec CHANGED
@@ -17,4 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'chronic', '>= 0.6.7'
20
22
  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: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-11-12 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chronic
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.7
14
30
  description: Framework for building a search UI with Ransack
15
31
  email:
16
32
  - nathan.f77@gmail.com
@@ -33,6 +49,8 @@ files:
33
49
  - app/views/ransack/_condition_fields.html.haml
34
50
  - app/views/ransack/_grouping_fields.html.haml
35
51
  - app/views/ransack/_search.html.haml
52
+ - config/locales/en-US.yml
53
+ - config/locales/en.yml
36
54
  - lib/core_ext/enumerable.rb
37
55
  - lib/ransack_ui.rb
38
56
  - lib/ransack_ui/rails/engine.rb
@@ -40,6 +58,7 @@ files:
40
58
  - lib/ransack_ui/ransack_overrides/configuration.rb
41
59
  - lib/ransack_ui/ransack_overrides/context.rb
42
60
  - lib/ransack_ui/ransack_overrides/helpers/form_builder.rb
61
+ - lib/ransack_ui/ransack_overrides/nodes/value.rb
43
62
  - lib/ransack_ui/version.rb
44
63
  - lib/ransack_ui/view_helpers.rb
45
64
  - ransack_ui.gemspec
@@ -58,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
77
  version: '0'
59
78
  segments:
60
79
  - 0
61
- hash: -11773573269345332
80
+ hash: 4598873639471933372
62
81
  required_rubygems_version: !ruby/object:Gem::Requirement
63
82
  none: false
64
83
  requirements:
@@ -67,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
86
  version: '0'
68
87
  segments:
69
88
  - 0
70
- hash: -11773573269345332
89
+ hash: 4598873639471933372
71
90
  requirements: []
72
91
  rubyforge_project:
73
92
  rubygems_version: 1.8.24