ransack_advanced_search 0.1.3 → 0.1.4

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: 82bba0e8cbaf970ff9d233609ac59bf155bfa084
4
- data.tar.gz: 36b15a6a0de51c434d5d10018378aeeb9560bf74
3
+ metadata.gz: 709e75317b253464253e2227e66d97ae1120ead2
4
+ data.tar.gz: 85ec334b704763e7f559041cfbc392ec703513b0
5
5
  SHA512:
6
- metadata.gz: 7a1bdb8394ed1c8c0a1bd121915f0aa3f0b7b8a193be7f15fe7846c77ceaf515ea728d5354100b97a0cd001a2f3fdb9ad29989fdc625f3ce7f07bffbb5650cae
7
- data.tar.gz: 05870bf3a5d6c051f8b42f0c87552539aeeb54ad722d6ba8f2386c8bc545f4390983937d27c7c9d130e0320dfee269f8ed91312e633f6676d7b693b2fc8a74c0
6
+ metadata.gz: 667d4d911afe7cffdc24dc46777adfb5e0ef7114c35abac12c43dabdbf69b888c1368739db65ad7e46df0bd55b4ccc0719f4db4659d41681cdd0e02a024db798
7
+ data.tar.gz: 47400c50f9d161176080e4d0fd74a02ab5a323313de1370495e49fd1e881b6e7846b053eba9ef86c615aa7928d18bb766668acc80f0717695aabdaf76594a651
data/README.md CHANGED
@@ -101,7 +101,7 @@ In your application layout `app/views/layouts/application.erb`, include a yield
101
101
  </body>
102
102
  </html>
103
103
  ```
104
- In the view that you want the advanced search views, insert the following:
104
+ In the view that you want the advanced search views you can choose betwenn quick_search and advanced_search views, so insert the following for advanced_search:
105
105
 
106
106
  ```ruby
107
107
  <%= render partial: 'ransack_advanced_search/advanced_search',
@@ -112,6 +112,18 @@ In the view that you want the advanced search views, insert the following:
112
112
  %>
113
113
  ```
114
114
 
115
+ Or, the following for quick_search:
116
+
117
+ ```ruby
118
+ <%= render partial: 'ransack_advanced_search/quick_search',
119
+ locals: {
120
+ search_url: advanced_search_your_models_path, # POST route we created above
121
+ redirect_path: your_models_path # GET redirect path, to return after some actions
122
+ }
123
+ %>
124
+ ```
125
+
126
+
115
127
  All done! Enjoy the search!
116
128
 
117
129
  ## Saving Searches
@@ -1,15 +1,38 @@
1
1
  module RansackAdvancedSearchHelper
2
- def setup_search_form(builder)
2
+ def setup_search_form(builder, search_object)
3
3
  fields = builder.grouping_fields builder.object.new_grouping,
4
4
  object_name: 'new_object_name', child_index: "new_grouping" do |f|
5
5
  render('ransack_advanced_search/grouping_fields', f: f)
6
6
  end
7
7
  content_for :ransack_setup, %Q{
8
8
  var search = new Search({grouping: "#{escape_javascript(fields)}"});
9
+ search.fieldsType = #{get_fields_data_type(search_object).to_json.html_safe}
10
+ search.convertFieldType = function (fieldType) {
11
+ var fieldTypeToHtmlType = {
12
+ 'default': 'text',
13
+ 'integer': 'number',
14
+ 'date' : 'date',
15
+ 'datetime' : 'date'
16
+ }
17
+ return (fieldTypeToHtmlType[fieldType] || fieldTypeToHtmlType['default']);
18
+ }
19
+ search.changeValueInputsType = function(element, fieldName) {
20
+ fieldType = search.fieldsType[fieldName];
21
+ conditionValueInputs = $(element).parents('.ransack-condition-field').find('.ransack-attribute-value');
22
+ conditionValueInputs.attr('type', search.convertFieldType(fieldType));
23
+ }
9
24
  $(document).on("click", "button.add_fields", function() {
10
25
  search.add_fields(this, $(this).data('fieldType'), $(this).data('content'));
26
+ if($(this).hasClass('ransack-add-attribute')) {
27
+ fieldName = $(this).parents('.ransack-condition-field').find('select.ransack-attribute-select').find('option:selected')[0].value;
28
+ search.changeValueInputsType(this, fieldName)
29
+ }
11
30
  return false;
12
31
  });
32
+ $(document).on('change', 'select.ransack-attribute-select', function(e) {
33
+ fieldName = $(this).find('option:selected')[0].value;
34
+ search.changeValueInputsType(this, fieldName)
35
+ });
13
36
  $(document).on("click", "i.remove_fields", function() {
14
37
  search.remove_fields(this);
15
38
  return false;
@@ -21,16 +44,28 @@ module RansackAdvancedSearchHelper
21
44
  }.html_safe
22
45
  end
23
46
 
47
+ def get_fields_data_type(search)
48
+ bases = [''] + search.klass.ransackable_associations
49
+ fields_type = Hash.new
50
+ bases.each do |model|
51
+ model_name = model.present? ? "#{model}_" : ""
52
+ search.context.traverse(model).columns_hash.each do |field, attributes|
53
+ fields_type["#{model_name}#{field}"] = attributes.type
54
+ end
55
+ end
56
+ fields_type
57
+ end
58
+
24
59
  def button_to_remove_fields
25
60
  content_tag :i, nil, class: 'remove_fields glyphicon glyphicon-remove text-danger'
26
61
  end
27
62
 
28
- def button_to_add_fields(name, f, type)
63
+ def button_to_add_fields(name, f, type, custom_class='')
29
64
  new_object = f.object.send "build_#{type}"
30
65
  fields = f.send("#{type}_fields", new_object, child_index: "new_#{type}") do |builder|
31
66
  render('ransack_advanced_search/' + type.to_s + "_fields", f: builder)
32
67
  end
33
- content_tag :button, name, :class => 'add_fields btn btn-default btn-sm', :type => 'button', 'data-field-type' => type, 'data-content' => "#{fields}"
68
+ content_tag :button, name, :class => custom_class + ' add_fields btn btn-default btn-sm', :type => 'button', 'data-field-type' => type, 'data-content' => "#{fields}"
34
69
  end
35
70
 
36
71
  def button_to_nest_fields(name, type)
@@ -9,9 +9,9 @@
9
9
  <% else %>
10
10
  <div class="col-md-12">
11
11
  <% end %>
12
- <%= search_form_for(@search, url: search_url, html: { method: :post, class: 'form-vertical', role: 'form' }) do |f| %>
12
+ <%= search_form_for(@search, url: search_url, html: { method: :post, class: 'form-inline', role: 'form' }) do |f| %>
13
13
 
14
- <% setup_search_form f %>
14
+ <% setup_search_form f, @search %>
15
15
 
16
16
  <% if RansackAdvancedSearch.enable_saved_searches %>
17
17
  <div class="form-group">
@@ -43,6 +43,7 @@
43
43
 
44
44
  <!-- <%= label_tag :distinct, 'Return distinct records?' %> -->
45
45
  <!-- <%= check_box_tag :distinct, '1', params[:distinct].to_i == 1 %> -->
46
+ <div class="clearfix"></div>
46
47
  <div class="form-group">
47
48
  <%= f.submit t('ransack.advanced_search.form.submit_text'), class: 'btn btn-primary' %>
48
49
  <% if RansackAdvancedSearch.enable_saved_searches %>
@@ -1,3 +1,3 @@
1
1
  <%= content_tag(:span,
2
- f.attribute_select({associations: @search.klass.ransackable_associations}, {class: 'form-control input-sm'}),
2
+ f.attribute_select({associations: @search.klass.ransackable_associations}, {class: 'ransack-attribute-select form-control input-sm'}),
3
3
  { class: 'fields', 'data-object-name' => f.object_name }, false) %>
@@ -1,5 +1,5 @@
1
1
  <%= content_tag :div, {
2
- class: 'fields condition', 'data-object-name' => f.object_name
2
+ class: 'fields condition ransack-condition-field', 'data-object-name' => f.object_name
3
3
  } do %>
4
4
 
5
5
  <%= content_tag(:span, button_to_remove_fields,
@@ -16,6 +16,6 @@
16
16
  <% end %>
17
17
 
18
18
  <%= button_to_add_fields t('ransack.advanced_search.attribute_value.add'),
19
- f, :value %>
19
+ f, :value, 'ransack-add-attribute' %>
20
20
 
21
21
  <% end %>
@@ -1,20 +1,33 @@
1
1
  <%= content_tag :div, { class: 'fields',
2
2
  'data-object-name' => f.object_name } do %>
3
3
 
4
+ <% remove_able = true if local_assigns[:remove_able].nil? %>
5
+ <% if remove_able == false %>
6
+ <%= content_tag(:span, "#{t('ransack.advanced_search.condition_group.match')}
7
+ #{f.combinator_select({}, {class: 'form-control input-sm'})}
8
+ #{t('ransack.advanced_search.condition_group.match_condition')}", nil, false) %>
9
+ <% else %>
10
+ <%= content_tag(:span, "#{t('ransack.advanced_search.condition_group.match')}
11
+ #{f.combinator_select({}, {class: 'form-control input-sm'})} #{t('ransack.advanced_search.condition_group.match_condition')}
12
+ #{button_to_remove_fields}", nil, false) %>
13
+ <% end %>
4
14
 
5
- <%= content_tag(:span, "#{t('ransack.advanced_search.condition_group.match')}
6
- #{f.combinator_select} #{t('ransack.advanced_search.condition_group.match_condition')}
7
- #{button_to_remove_fields}", nil, false) %>
8
-
9
- <%= f.condition_fields do |c| %>
15
+ <% condition_fields = f.condition_fields do |c| %>
10
16
  <%= render 'ransack_advanced_search/condition_fields', f: c %>
11
17
  <% end %>
18
+ <%= condition_fields %>
19
+ <% if condition_fields.empty? %>
20
+ <%= f.send("condition_fields", f.object.send("build_condition"), child_index: "new_condition") do |builder| %>
21
+ <%= render 'ransack_advanced_search/condition_fields', f: builder %>
22
+ <% end %>
23
+ <% end %>
12
24
 
13
25
  <%= button_to_add_fields t('ransack.advanced_search.attribute_field.add'),
14
26
  f, :condition %>
15
27
 
16
28
  <%= f.grouping_fields do |g| %>
17
- <%= render 'ransack_advanced_search/grouping_fields', f: g %>
29
+ <%= render partial: 'ransack_advanced_search/grouping_fields',
30
+ locals: { f: g, remove_able: true } %>
18
31
  <% end %>
19
32
 
20
33
  <% end %>
@@ -0,0 +1,65 @@
1
+ <div class="ransack-advanced-search">
2
+
3
+ <div class="row">
4
+ <% if RansackAdvancedSearch.enable_saved_searches %>
5
+ <div class="col-md-4">
6
+ <%= render 'ransack_advanced_search/saved_searches_list' %>
7
+ </div>
8
+ <div class="col-md-8">
9
+ <% else %>
10
+ <div class="col-md-12">
11
+ <% end %>
12
+ <%= search_form_for(@search, url: search_url, html: { method: :post, class: 'form-inline', role: 'form' }) do |f| %>
13
+
14
+ <% setup_search_form f, @search %>
15
+
16
+ <% if RansackAdvancedSearch.enable_saved_searches %>
17
+ <div class="form-group">
18
+ <%= label_tag 'Descrição da Busca (necessária para salvar a busca)' %>
19
+ <%= text_field_tag :description, @saved_search.try(:description), class: 'form-control input-sm' %>
20
+ <% if @saved_search %>
21
+ <%= hidden_field_tag :saved_search, @saved_search.id %>
22
+ <%= hidden_field_tag :use_search_params, true %>
23
+ <% end %>
24
+ </div>
25
+ <% end %>
26
+
27
+ <h4><%= t('ransack.quick_search.condition_group.title') %></h4>
28
+ <div class="form-group">
29
+ <% if f.grouping_fields.empty? %>
30
+ <%= render partial: 'ransack_advanced_search/grouping_fields',
31
+ locals: { f: f, remove_able: false } %>
32
+ <% else %>
33
+ <%= f.grouping_fields do |g| %>
34
+ <%= render partial: 'ransack_advanced_search/grouping_fields',
35
+ locals: { f: f, remove_able: true } %>
36
+ <% end %>
37
+ <%= button_to_add_fields t('ransack.advanced_search.condition_group.add'),
38
+ f, :grouping %>
39
+ <% end %>
40
+ </div>
41
+
42
+ <div class="clearfix"></div>
43
+ <div class="form-group">
44
+ <%= f.submit t('ransack.advanced_search.form.submit_text'), class: 'btn btn-primary' %>
45
+ <% if RansackAdvancedSearch.enable_saved_searches %>
46
+ <%= f.submit t('ransack.advanced_search.form.save_submit_text'), name: 'save_search', class: 'btn btn-primary' %>
47
+ <%= f.submit t('ransack.advanced_search.form.save_new_submit_text'), name: 'save_new_search', class: 'btn btn-primary' %>
48
+ <% end %>
49
+ <%= link_to t('ransack.advanced_search.form.clear_search_text'), redirect_path, class: 'btn btn-default' %>
50
+ </div>
51
+ <% end %>
52
+ </div>
53
+ </div>
54
+ </div>
55
+
56
+ <% content_for(:ransack_advanced_search_setup) do %>
57
+ <%= stylesheet_link_tag 'ransack_advanced_search/application', media: 'all' %>
58
+ <%= javascript_include_tag 'ransack_advanced_search/application' %>
59
+
60
+ <script>
61
+ $(document).ready(function () {
62
+ <%= yield(:ransack_setup) %>
63
+ });
64
+ </script>
65
+ <% end %>
@@ -1,2 +1,2 @@
1
- <%= content_tag(:span, f.text_field(:value, {class: 'form-control input-sm'}),
1
+ <%= content_tag(:span, f.text_field(:value, {class: 'form-control input-sm ransack-attribute-value'}),
2
2
  { class: 'fields value', 'data-object-name' => f.object_name }, false) %>
@@ -1,5 +1,8 @@
1
1
  en:
2
2
  ransack:
3
+ quick_search:
4
+ condition_group:
5
+ title: Conditions
3
6
  advanced_search:
4
7
  form:
5
8
  submit_text: Search
@@ -1,5 +1,8 @@
1
1
  pt-BR:
2
2
  ransack:
3
+ quick_search:
4
+ condition_group:
5
+ title: Filtros
3
6
  advanced_search:
4
7
  form:
5
8
  submit_text: Pesquisar
@@ -1,3 +1,3 @@
1
1
  module RansackAdvancedSearch
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_advanced_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Brusius
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-28 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -85,6 +85,7 @@ files:
85
85
  - app/views/ransack_advanced_search/_attribute_fields.erb
86
86
  - app/views/ransack_advanced_search/_condition_fields.erb
87
87
  - app/views/ransack_advanced_search/_grouping_fields.erb
88
+ - app/views/ransack_advanced_search/_quick_search.html.erb
88
89
  - app/views/ransack_advanced_search/_saved_searches_list.erb
89
90
  - app/views/ransack_advanced_search/_sort_fields.erb
90
91
  - app/views/ransack_advanced_search/_value_fields.erb