├── test
├── dummy
│ ├── log
│ │ └── .gitkeep
│ ├── app
│ │ ├── mailers
│ │ │ └── .gitkeep
│ │ ├── models
│ │ │ └── .gitkeep
│ │ ├── helpers
│ │ │ └── application_helper.rb
│ │ ├── controllers
│ │ │ └── application_controller.rb
│ │ ├── views
│ │ │ └── layouts
│ │ │ │ └── application.html.erb
│ │ └── assets
│ │ │ ├── stylesheets
│ │ │ └── application.css
│ │ │ └── javascripts
│ │ │ └── application.js
│ ├── lib
│ │ └── assets
│ │ │ └── .gitkeep
│ ├── public
│ │ ├── favicon.ico
│ │ ├── 500.html
│ │ ├── 422.html
│ │ └── 404.html
│ ├── config
│ │ ├── routes.rb
│ │ ├── environment.rb
│ │ ├── locales
│ │ │ └── en.yml
│ │ ├── initializers
│ │ │ ├── mime_types.rb
│ │ │ ├── backtrace_silencers.rb
│ │ │ ├── session_store.rb
│ │ │ ├── secret_token.rb
│ │ │ ├── wrap_parameters.rb
│ │ │ └── inflections.rb
│ │ ├── boot.rb
│ │ ├── database.yml
│ │ ├── environments
│ │ │ ├── development.rb
│ │ │ ├── test.rb
│ │ │ └── production.rb
│ │ └── application.rb
│ ├── config.ru
│ ├── Rakefile
│ ├── script
│ │ └── rails
│ └── README.rdoc
├── ransack_advanced_search_test.rb
├── integration
│ └── navigation_test.rb
└── test_helper.rb
├── app
├── assets
│ ├── images
│ │ └── ransack_advanced_search
│ │ │ └── .gitkeep
│ ├── javascripts
│ │ └── ransack_advanced_search
│ │ │ ├── application.js
│ │ │ └── ransack_advanced_search.js
│ └── stylesheets
│ │ └── ransack_advanced_search
│ │ └── application.css
├── models
│ └── saved_search.rb
├── controllers
│ ├── ransack_advanced_search
│ │ └── application_controller.rb
│ ├── saved_searches_controller.rb
│ └── concerns
│ │ └── ransack_advanced_search
│ │ └── saved_search_utils.rb
├── views
│ └── ransack_advanced_search
│ │ ├── _value_fields.erb
│ │ ├── _sort_fields.erb
│ │ ├── _attribute_fields.erb
│ │ ├── _condition_fields.erb
│ │ ├── _saved_searches_list.erb
│ │ ├── _grouping_fields.erb
│ │ ├── _quick_search.html.erb
│ │ └── _advanced_search.html.erb
└── helpers
│ └── ransack_advanced_search_helper.rb
├── lib
├── ransack_advanced_search
│ ├── version.rb
│ ├── engine.rb
│ └── helpers
│ │ └── configuration.rb
├── tasks
│ └── ransack_advanced_search_tasks.rake
├── generators
│ ├── templates
│ │ ├── ransack_advanced_search.rb
│ │ └── create_ransack_advanced_search_saved_search.rb
│ └── ransack_advanced_search
│ │ ├── install_generator.rb
│ │ └── saved_search_generator.rb
└── ransack_advanced_search.rb
├── config
├── initializers
│ └── ransack_advanced_search_setup.rb
├── routes.rb
└── locales
│ ├── ransack.en.yml
│ └── ransack.pt-BR.yml
├── .gitignore
├── script
└── rails
├── Gemfile
├── ransack_advanced_search.gemspec
├── Rakefile
├── MIT-LICENSE
├── Gemfile.lock
└── README.md
/test/dummy/log/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/app/mailers/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/app/models/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/lib/assets/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/public/favicon.ico:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/assets/images/ransack_advanced_search/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/app/helpers/application_helper.rb:
--------------------------------------------------------------------------------
1 | module ApplicationHelper
2 | end
3 |
--------------------------------------------------------------------------------
/lib/ransack_advanced_search/version.rb:
--------------------------------------------------------------------------------
1 | module RansackAdvancedSearch
2 | VERSION = "0.1.8"
3 | end
4 |
--------------------------------------------------------------------------------
/config/initializers/ransack_advanced_search_setup.rb:
--------------------------------------------------------------------------------
1 | Rails.application.config.i18n.fallbacks = [:en]
2 |
--------------------------------------------------------------------------------
/config/routes.rb:
--------------------------------------------------------------------------------
1 | Rails.application.routes.draw do
2 | resources :saved_searches, only: [:destroy]
3 | end
4 |
--------------------------------------------------------------------------------
/lib/ransack_advanced_search/engine.rb:
--------------------------------------------------------------------------------
1 | module RansackAdvancedSearch
2 | class Engine < ::Rails::Engine
3 | end
4 | end
5 |
--------------------------------------------------------------------------------
/test/dummy/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | protect_from_forgery
3 | end
4 |
--------------------------------------------------------------------------------
/test/dummy/config/routes.rb:
--------------------------------------------------------------------------------
1 | Rails.application.routes.draw do
2 |
3 | mount RansackAdvancedSearch::Engine => "/ransack_advanced_search"
4 | end
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle/
2 | log/*.log
3 | pkg/
4 | test/dummy/db/*.sqlite3
5 | test/dummy/log/*.log
6 | test/dummy/tmp/
7 | test/dummy/.sass-cache
8 | *.gem
9 |
--------------------------------------------------------------------------------
/lib/tasks/ransack_advanced_search_tasks.rake:
--------------------------------------------------------------------------------
1 | # desc "Explaining what the task does"
2 | # task :ransack_advanced_search do
3 | # # Task goes here
4 | # end
5 |
--------------------------------------------------------------------------------
/app/models/saved_search.rb:
--------------------------------------------------------------------------------
1 | class SavedSearch < ActiveRecord::Base
2 | validates_presence_of :context, :description, :search_params
3 |
4 | serialize :search_params
5 | end
6 |
--------------------------------------------------------------------------------
/test/dummy/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Dummy::Application
5 |
--------------------------------------------------------------------------------
/lib/generators/templates/ransack_advanced_search.rb:
--------------------------------------------------------------------------------
1 | RansackAdvancedSearch.configuration do |config|
2 |
3 | # Enable feature for Saving Searches
4 | config.enable_saved_searches = false
5 | end
6 |
--------------------------------------------------------------------------------
/test/dummy/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the rails application
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the rails application
5 | Dummy::Application.initialize!
6 |
--------------------------------------------------------------------------------
/test/ransack_advanced_search_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 |
3 | class RansackAdvancedSearchTest < ActiveSupport::TestCase
4 | test "truth" do
5 | assert_kind_of Module, RansackAdvancedSearch
6 | end
7 | end
8 |
--------------------------------------------------------------------------------
/app/controllers/ransack_advanced_search/application_controller.rb:
--------------------------------------------------------------------------------
1 | module RansackAdvancedSearch
2 | class ApplicationController < ActionController::Base
3 | protect_from_forgery :with => :exception
4 | end
5 | end
6 |
--------------------------------------------------------------------------------
/app/views/ransack_advanced_search/_value_fields.erb:
--------------------------------------------------------------------------------
1 | <%= content_tag(:span, f.text_field(:value, {class: 'form-control input-sm ransack-attribute-value'}),
2 | { class: 'fields value', 'data-object-name' => f.object_name }, false) %>
3 |
--------------------------------------------------------------------------------
/test/integration/navigation_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 |
3 | class NavigationTest < ActionDispatch::IntegrationTest
4 | fixtures :all
5 |
6 | # test "the truth" do
7 | # assert true
8 | # end
9 | end
10 |
11 |
--------------------------------------------------------------------------------
/app/views/ransack_advanced_search/_sort_fields.erb:
--------------------------------------------------------------------------------
1 | <%= content_tag(:div,
2 | "#{button_to_remove_fields} #{f.sort_select({}, {class: 'form-control input-sm'})}",
3 | { class: 'fields', 'data-object-name' => f.object_name }, false) %>
4 |
--------------------------------------------------------------------------------
/test/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Sample localization file for English. Add more files in this directory for other locales.
2 | # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 |
4 | en:
5 | hello: "Hello world"
6 |
--------------------------------------------------------------------------------
/test/dummy/config/initializers/mime_types.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new mime types for use in respond_to blocks:
4 | # Mime::Type.register "text/richtext", :rtf
5 | # Mime::Type.register_alias "text/html", :iphone
6 |
--------------------------------------------------------------------------------
/app/views/ransack_advanced_search/_attribute_fields.erb:
--------------------------------------------------------------------------------
1 | <%= content_tag(:span,
2 | f.attribute_select({associations: @search.klass.ransackable_associations}, {class: 'ransack-attribute-select form-control input-sm'}),
3 | { class: 'fields', 'data-object-name' => f.object_name }, false) %>
4 |
--------------------------------------------------------------------------------
/test/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3 |
4 | if File.exist?(gemfile)
5 | ENV['BUNDLE_GEMFILE'] = gemfile
6 | require 'bundler'
7 | Bundler.setup
8 | end
9 |
10 | $:.unshift File.expand_path('../../../../lib', __FILE__)
--------------------------------------------------------------------------------
/test/dummy/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env rake
2 | # Add your own tasks in files placed in lib/tasks ending in .rake,
3 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4 |
5 | require File.expand_path('../config/application', __FILE__)
6 |
7 | Dummy::Application.load_tasks
8 |
--------------------------------------------------------------------------------
/test/dummy/script/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3 |
4 | APP_PATH = File.expand_path('../../config/application', __FILE__)
5 | require File.expand_path('../../config/boot', __FILE__)
6 | require 'rails/commands'
7 |
--------------------------------------------------------------------------------
/test/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
2 |
3 |
4 | <% if RansackAdvancedSearch.enable_saved_searches %>
5 |
6 | <%= render 'ransack_advanced_search/saved_searches_list', redirect_path: redirect_path %>
7 |
8 |
9 | <% else %>
10 |
11 | <% end %>
12 | <%= search_form_for(@search, url: search_url, html: { method: :post, class: 'form-inline', role: 'form' }) do |f| %>
13 | <% setup_search_form f, @search %>
14 |
15 | <% if RansackAdvancedSearch.enable_saved_searches %>
16 |
17 | <%= label_tag 'Descrição da Busca (necessária para salvar a busca)' %>
18 | <%= text_field_tag :description, @saved_search.try(:description), class: 'form-control input-sm' %>
19 | <% if @saved_search %>
20 | <%= hidden_field_tag :saved_search, @saved_search.id %>
21 | <%= hidden_field_tag :use_search_params, true %>
22 | <% end %>
23 |
24 | <% end %>
25 |
26 |
<%= t('ransack.quick_search.condition_group.title') %>
27 |
28 | <% if f.grouping_fields.empty? %>
29 | <%= render partial: 'ransack_advanced_search/grouping_fields',
30 | locals: { f: f, remove_able: false } %>
31 | <% else %>
32 | <%= f.grouping_fields do |g| %>
33 | <%= render partial: 'ransack_advanced_search/grouping_fields',
34 | locals: { f: f, remove_able: true } %>
35 | <% end %>
36 | <%= button_to_add_fields '', f, :grouping %>
37 | <% end %>
38 |
39 |
40 |
41 |
42 | <%= f.submit t('ransack.advanced_search.form.submit_text'), class: 'btn btn-primary' %>
43 | <% if RansackAdvancedSearch.enable_saved_searches %>
44 | <%= f.submit t('ransack.advanced_search.form.save_submit_text'), name: 'save_search', class: 'btn btn-primary' %>
45 | <%= f.submit t('ransack.advanced_search.form.save_new_submit_text'), name: 'save_new_search', class: 'btn btn-primary' %>
46 | <% end %>
47 | <%= link_to t('ransack.advanced_search.form.clear_search_text'), redirect_path, class: 'btn btn-default' %>
48 |
49 | <% end %>
50 |
51 |
52 |
53 |
54 | <% content_for(:ransack_advanced_search_setup) do %>
55 | <%= stylesheet_link_tag 'ransack_advanced_search/application', media: 'all' %>
56 | <%= javascript_include_tag 'ransack_advanced_search/application' %>
57 |
58 |
63 | <% end %>
64 |
--------------------------------------------------------------------------------
/app/helpers/ransack_advanced_search_helper.rb:
--------------------------------------------------------------------------------
1 | module RansackAdvancedSearchHelper
2 | def setup_search_form(builder, search_object)
3 | fields = builder.grouping_fields builder.object.new_grouping,
4 | object_name: 'new_object_name', child_index: "new_grouping" do |f|
5 | render('ransack_advanced_search/grouping_fields', f: f)
6 | end
7 | content_for :ransack_setup, %Q{
8 | var search = new Search({grouping: "#{escape_javascript(fields)}"});
9 | search.fieldsType = #{get_fields_data_type(search_object).to_json.html_safe}
10 | $('select.ransack-attribute-select').each(function(e) {
11 | fieldName = $(this).find('option:selected')[0].value;
12 | search.changeValueInputsType(this, fieldName, search);
13 | });
14 | $(document).on("click", "i.add_fields", function() {
15 | search.add_fields(this, $(this).data('fieldType'), $(this).data('content'));
16 | if($(this).hasClass('ransack-add-attribute')) {
17 | fieldName = $(this).parents('.ransack-condition-field').find('select.ransack-attribute-select').find('option:selected')[0].value;
18 | search.changeValueInputsType(this, fieldName, search);
19 | }
20 | return false;
21 | });
22 | $(document).on('change', 'select.ransack-attribute-select', function(e) {
23 | fieldName = $(this).find('option:selected')[0].value;
24 | search.changeValueInputsType(this, fieldName, search);
25 | });
26 | $(document).on("click", "i.remove_fields", function() {
27 | search.remove_fields(this);
28 | return false;
29 | });
30 | $(document).on("click", "button.nest_fields", function() {
31 | search.nest_fields(this, $(this).data('fieldType'));
32 | return false;
33 | });
34 | }.html_safe
35 | end
36 |
37 | def get_fields_data_type(search)
38 | bases = [''] + search.klass.ransackable_associations
39 | fields_type = Hash.new
40 | bases.each do |model|
41 | model_name = model.present? ? "#{model}_" : ""
42 | search.context.traverse(model).columns_hash.each do |field, attributes|
43 | fields_type["#{model_name}#{field}"] = attributes.type
44 | end
45 | end
46 | fields_type
47 | end
48 |
49 | def button_to_remove_fields
50 | content_tag :i, nil, class: 'remove_fields glyphicon glyphicon-minus-sign text-danger'
51 | end
52 |
53 | def button_to_add_fields(name, f, type, custom_class='')
54 | new_object = f.object.send "build_#{type}"
55 | fields = f.send("#{type}_fields", new_object, child_index: "new_#{type}") do |builder|
56 | render('ransack_advanced_search/' + type.to_s + "_fields", f: builder)
57 | end
58 | content_tag :i, name, :class => custom_class + ' add_fields glyphicon glyphicon-plus-sign text-success', :type => 'button', 'data-field-type' => type, 'data-content' => "#{fields}"
59 | end
60 |
61 | def button_to_nest_fields(name, type)
62 | content_tag :button, name, :class => 'nest_fields', 'data-field-type' => type
63 | end
64 | end
65 |
--------------------------------------------------------------------------------
/app/views/ransack_advanced_search/_advanced_search.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | <% if RansackAdvancedSearch.enable_saved_searches %>
5 |
6 | <%= render 'ransack_advanced_search/saved_searches_list', redirect_path: redirect_path %>
7 |
8 |
9 | <% else %>
10 |
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 |
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 |
25 | <% end %>
26 |
27 |
<%= t('ransack.advanced_search.sort.title') %>
28 |
29 | <%= f.sort_fields do |s| %>
30 | <%= render 'ransack_advanced_search/sort_fields', f: s %>
31 | <% end %>
32 | <%= button_to_add_fields '', f, :sort %>
33 |
34 |
35 |
<%= t('ransack.advanced_search.condition_group.title') %>
36 |
37 | <%= f.grouping_fields do |g| %>
38 | <%= render 'ransack_advanced_search/grouping_fields', f: g %>
39 | <% end %>
40 | <%= button_to_add_fields '', f, :grouping %>
41 |
42 |
43 |
44 |
45 |
46 |
47 | <%= f.submit t('ransack.advanced_search.form.submit_text'), class: 'btn btn-primary' %>
48 | <% if RansackAdvancedSearch.enable_saved_searches %>
49 | <%= f.submit t('ransack.advanced_search.form.save_submit_text'), name: 'save_search', class: 'btn btn-primary' %>
50 | <%= f.submit t('ransack.advanced_search.form.save_new_submit_text'), name: 'save_new_search', class: 'btn btn-primary' %>
51 | <% end %>
52 | <%= link_to t('ransack.advanced_search.form.clear_search_text'), redirect_path, class: 'btn btn-default' %>
53 |
54 | <% end %>
55 |
56 |
57 |
58 |
59 | <% content_for(:ransack_advanced_search_setup) do %>
60 | <%= stylesheet_link_tag 'ransack_advanced_search/application', media: 'all' %>
61 | <%= javascript_include_tag 'ransack_advanced_search/application' %>
62 |
63 |
68 | <% end %>
69 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | ransack_advanced_search (0.1.5)
5 | rails (>= 3.2.6, < 5)
6 | ransack (~> 1.7.0, >= 1.7.0)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (3.2.22.2)
12 | actionpack (= 3.2.22.2)
13 | mail (~> 2.5.4)
14 | actionpack (3.2.22.2)
15 | activemodel (= 3.2.22.2)
16 | activesupport (= 3.2.22.2)
17 | builder (~> 3.0.0)
18 | erubis (~> 2.7.0)
19 | journey (~> 1.0.4)
20 | rack (~> 1.4.5)
21 | rack-cache (~> 1.2)
22 | rack-test (~> 0.6.1)
23 | sprockets (~> 2.2.1)
24 | activemodel (3.2.22.2)
25 | activesupport (= 3.2.22.2)
26 | builder (~> 3.0.0)
27 | activerecord (3.2.22.2)
28 | activemodel (= 3.2.22.2)
29 | activesupport (= 3.2.22.2)
30 | arel (~> 3.0.2)
31 | tzinfo (~> 0.3.29)
32 | activeresource (3.2.22.2)
33 | activemodel (= 3.2.22.2)
34 | activesupport (= 3.2.22.2)
35 | activesupport (3.2.22.2)
36 | i18n (~> 0.6, >= 0.6.4)
37 | multi_json (~> 1.0)
38 | arel (3.0.3)
39 | builder (3.0.4)
40 | coderay (1.1.1)
41 | erubis (2.7.0)
42 | hike (1.2.3)
43 | i18n (0.7.0)
44 | journey (1.0.4)
45 | jquery-rails (3.1.4)
46 | railties (>= 3.0, < 5.0)
47 | thor (>= 0.14, < 2.0)
48 | json (1.8.3)
49 | mail (2.5.4)
50 | mime-types (~> 1.16)
51 | treetop (~> 1.4.8)
52 | method_source (0.8.2)
53 | mime-types (1.25.1)
54 | multi_json (1.11.3)
55 | polyamorous (1.3.0)
56 | activerecord (>= 3.0)
57 | polyglot (0.3.5)
58 | pry (0.10.3)
59 | coderay (~> 1.1.0)
60 | method_source (~> 0.8.1)
61 | slop (~> 3.4)
62 | rack (1.4.7)
63 | rack-cache (1.6.1)
64 | rack (>= 0.4)
65 | rack-ssl (1.3.4)
66 | rack
67 | rack-test (0.6.3)
68 | rack (>= 1.0)
69 | rails (3.2.22.2)
70 | actionmailer (= 3.2.22.2)
71 | actionpack (= 3.2.22.2)
72 | activerecord (= 3.2.22.2)
73 | activeresource (= 3.2.22.2)
74 | activesupport (= 3.2.22.2)
75 | bundler (~> 1.0)
76 | railties (= 3.2.22.2)
77 | railties (3.2.22.2)
78 | actionpack (= 3.2.22.2)
79 | activesupport (= 3.2.22.2)
80 | rack-ssl (~> 1.3.2)
81 | rake (>= 0.8.7)
82 | rdoc (~> 3.4)
83 | thor (>= 0.14.6, < 2.0)
84 | rake (11.1.2)
85 | ransack (1.7.0)
86 | actionpack (>= 3.0)
87 | activerecord (>= 3.0)
88 | activesupport (>= 3.0)
89 | i18n
90 | polyamorous (~> 1.2)
91 | rdoc (3.12.2)
92 | json (~> 1.4)
93 | slop (3.6.0)
94 | sprockets (2.2.3)
95 | hike (~> 1.2)
96 | multi_json (~> 1.0)
97 | rack (~> 1.0)
98 | tilt (~> 1.1, != 1.3.0)
99 | thor (0.19.1)
100 | tilt (1.4.1)
101 | treetop (1.4.15)
102 | polyglot
103 | polyglot (>= 0.3.1)
104 | tzinfo (0.3.49)
105 |
106 | PLATFORMS
107 | ruby
108 |
109 | DEPENDENCIES
110 | jquery-rails
111 | pry (~> 0.10)
112 | ransack_advanced_search!
113 |
--------------------------------------------------------------------------------
/config/locales/ransack.en.yml:
--------------------------------------------------------------------------------
1 | en:
2 | ransack:
3 | quick_search:
4 | condition_group:
5 | title: Conditions
6 | advanced_search:
7 | form:
8 | submit_text: Search
9 | save_submit_text: Save and Search
10 | save_new_submit_text: Save as new and Search
11 | clear_search_text: Reset
12 | delete_saved_search: Delete this saved search
13 | delete_saved_search_confirm: Confirm deletion of this saved search?
14 | sort:
15 | title: Sort
16 | label_title: Sort by
17 | add: Add Sort
18 | condition_group:
19 | title: Condition Groups
20 | add: Add Condition Group
21 | match: Match
22 | match_condition: conditions
23 | attribute_field:
24 | add: Add Condition
25 | attribute_value:
26 | add: Add Value
27 | saved_search:
28 | title: Saved Searches
29 | empty_list: There are no saved searches in this context.
30 | save:
31 | success: Search saved successfully.
32 | error: Error while saving the search. You must enter a description of the search and at least one filter.
33 | delete:
34 | success: Search successfully destroyed.
35 | search: "search"
36 | predicate: "predicate"
37 | and: "and"
38 | or: "or"
39 | any: "any"
40 | all: "all"
41 | combinator: "combinator"
42 | attribute: "attribute"
43 | value: "value"
44 | condition: "condition"
45 | sort: "sort"
46 | asc: "ascending"
47 | desc: "descending"
48 | predicates:
49 | eq: "equals"
50 | eq_any: "equals any"
51 | eq_all: "equals all"
52 | not_eq: "not equal to"
53 | not_eq_any: "not equal to any"
54 | not_eq_all: "not equal to all"
55 | matches: "matches"
56 | matches_any: "matches any"
57 | matches_all: "matches all"
58 | does_not_match: "doesn't match"
59 | does_not_match_any: "doesn't match any"
60 | does_not_match_all: "doesn't match all"
61 | lt: "less than"
62 | lt_any: "less than any"
63 | lt_all: "less than all"
64 | lteq: "less than or equal to"
65 | lteq_any: "less than or equal to any"
66 | lteq_all: "less than or equal to all"
67 | gt: "greater than"
68 | gt_any: "greater than any"
69 | gt_all: "greater than all"
70 | gteq: "greater than or equal to"
71 | gteq_any: "greater than or equal to any"
72 | gteq_all: "greater than or equal to all"
73 | in: "in"
74 | in_any: "in any"
75 | in_all: "in all"
76 | not_in: "not in"
77 | not_in_any: "not in any"
78 | not_in_all: "not in all"
79 | cont: "contains"
80 | cont_any: "contains any"
81 | cont_all: "contains all"
82 | not_cont: "doesn't contain"
83 | not_cont_any: "doesn't contain any"
84 | not_cont_all: "doesn't contain all"
85 | start: "starts with"
86 | start_any: "starts with any"
87 | start_all: "starts with all"
88 | not_start: "doesn't start with"
89 | not_start_any: "doesn't start with any"
90 | not_start_all: "doesn't start with all"
91 | end: "ends with"
92 | end_any: "ends with any"
93 | end_all: "ends with all"
94 | not_end: "doesn't end with"
95 | not_end_any: "doesn't end with any"
96 | not_end_all: "doesn't end with all"
97 | 'true': "is true"
98 | 'false': "is false"
99 | present: "is present"
100 | blank: "is blank"
101 | 'null': "is null"
102 | not_null: "is not null"
103 |
--------------------------------------------------------------------------------
/config/locales/ransack.pt-BR.yml:
--------------------------------------------------------------------------------
1 | pt-BR:
2 | ransack:
3 | quick_search:
4 | condition_group:
5 | title: Filtros
6 | advanced_search:
7 | form:
8 | submit_text: Pesquisar
9 | save_submit_text: Salvar e Pesquisar
10 | save_new_submit_text: Salvar nova e Pesquisar
11 | clear_search_text: Limpar
12 | delete_saved_search: Excluir esta busca salva
13 | delete_saved_search_confirm: Confirma a exclusão desta busca salva?
14 | sort:
15 | title: Ordenação
16 | add: Adicionar ordenação
17 | condition_group:
18 | title: Grupos de filtros
19 | add: Adicionar grupo
20 | match: Filtrar
21 | match_condition: condições
22 | attribute_field:
23 | add: Adicionar filtro
24 | attribute_value:
25 | add: Adicionar valor
26 | saved_search:
27 | title: Buscas Salvas
28 | empty_list: Não existem buscas salvas para este contexto.
29 | save:
30 | success: Busca salva com sucesso.
31 | error: Erro ao salvar a busca. Você precisa informar a descrição da busca e pelo menos um filtro.
32 | delete:
33 | success: Busca excluída com sucesso.
34 | search: "pesquisar"
35 | predicate: "predicado"
36 | and: "e"
37 | or: "ou"
38 | any: "alguma das"
39 | all: "todas as"
40 | combinator: "combinador"
41 | attribute: "atributo"
42 | value: "valor"
43 | condition: "condição"
44 | sort: "classificar"
45 | asc: "ascendente"
46 | desc: "descendente"
47 | predicates:
48 | eq: "igual"
49 | eq_any: "igual a algum"
50 | eq_all: "igual a todos"
51 | not_eq: "não é igual a"
52 | not_eq_any: "não é igual a algum"
53 | not_eq_all: "não é igual a todos"
54 | matches: "corresponde"
55 | matches_any: "corresponde a algum"
56 | matches_all: "corresponde a todos"
57 | does_not_match: "não corresponde"
58 | does_not_match_any: "não corresponde a algum"
59 | does_not_match_all: "não corresponde a todos"
60 | lt: "menor que"
61 | lt_any: "menor que algum"
62 | lt_all: "menor que todos"
63 | lteq: "menor ou igual a"
64 | lteq_any: "menor ou igual a algum"
65 | lteq_all: "menor ou igual a todos"
66 | gt: "maior que"
67 | gt_any: "maior que algum"
68 | gt_all: "maior que todos"
69 | gteq: "maior que ou igual a"
70 | gteq_any: "maior que ou igual a algum"
71 | gteq_all: "maior que ou igual a todos"
72 | in: "em"
73 | in_any: "em algum"
74 | in_all: "em todos"
75 | not_in: "não em"
76 | not_in_any: "não em algum"
77 | not_in_all: "não em todos"
78 | cont: "contém"
79 | cont_any: "contém algum"
80 | cont_all: "contém todos"
81 | not_cont: "não contém"
82 | not_cont_any: "não contém algum"
83 | not_cont_all: "não contém todos"
84 | start: "começa com"
85 | start_any: "começa com algum"
86 | start_all: "começa com todos"
87 | not_start: "não começa com"
88 | not_start_any: "não começa com algum"
89 | not_start_all: "não começa com algum"
90 | end: "termina com"
91 | end_any: "termina com algum"
92 | end_all: "termina com todos"
93 | not_end: "não termina com"
94 | not_end_any: "não termina com algum"
95 | not_end_all: "não termina com todos"
96 | 'true': "é verdadeiro"
97 | 'false': "é falso"
98 | present: "está presente"
99 | blank: "está em branco"
100 | 'null': "é nullo"
101 | not_null: "não é nulo"
102 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://badge.fury.io/rb/ransack_advanced_search)
2 |
3 | # Ransack Advanced Search
4 |
5 | [ransack](https://github.com/activerecord-hackery/ransack) is a Object-based searching for Rails.
6 |
7 | The `ransack_advanced_search` gem provides Bootstrap based templates for the [ransack](https://github.com/activerecord-hackery/ransack) Advanced query mode. This gem also provides some additional features to make search experience better and easy to use.
8 |
9 | ## Features
10 | * Full [ransack](https://github.com/activerecord-hackery/ransack) compatibility (you can still use the default [ransack](https://github.com/activerecord-hackery/ransack) features);
11 | * Ransack integrated Advanced Search mode with helpers and views - extracted from [ransack-demo](https://github.com/activerecord-hackery/ransack_demo) project;
12 | * Saved Searches, scoped by contexts, to persist search params and use it in the future;
13 | * Custom search value inputs based on data type;
14 | * TODO: scope by current user or current account (abstract scope).
15 |
16 | ## Installation
17 |
18 | Remove any entry of `ransack` gem from your application's Gemfile. The `ransack_advanced_search` gem will include the `ransack` gem as a dependency.
19 |
20 | Add this line to your application's Gemfile:
21 |
22 | ```ruby
23 | gem 'ransack_advanced_search'
24 | ```
25 |
26 | Execute:
27 |
28 | $ bundle
29 |
30 | Or install it yourself as:
31 |
32 | $ gem install ransack_advanced_search
33 |
34 | Run the generator to install the gem initializer, this will create the file `config/initializers/ransack_advanced_search.rb`:
35 |
36 | $ rails generate ransack_advanced_search:install
37 |
38 | For while we don't need to change this file.
39 |
40 | ## Usage
41 |
42 | First, in your controller action that you will use for search, include the following:
43 |
44 | ```ruby
45 | # GET /your_models
46 | # GET /your_models.json
47 | # POST /your_models/advanced_search
48 | def index
49 | # The ransack search must be in the @search instance variable, because the advanced search will use it to build the search form. You must provide associations you will use in the includes method.
50 | @search = YourModel.search(params[:q])
51 | @results = @search.result().includes(:association1, :association2)
52 | # or, if the above doesn't work
53 | @search = YourModel.ransack(params[:q])
54 | @results = @search.result(:association1, :association2)
55 | end
56 | ```
57 |
58 | To use the Advanced Search with associtions you must provide a method in your model to tell what are the associations for that model
59 | ```ruby
60 | class YourModel < ActiveRecord::Base
61 | # Associations to be included in the search attributes
62 | def self.ransackable_associations(*)
63 | %w( association1 association2 )
64 | end
65 | end
66 | ```
67 |
68 | By default ransack will provide all your model fields to the avaliable field to search. You can restrict what fields will be included in the Advanced Search by defining it in your model, like this:
69 | ```ruby
70 | class YourModel < ActiveRecord::Base
71 | # Fields that will be included in ransack advanced search
72 | def self.ransackable_attributes(*)
73 | %w( name description other_fields_names ) + _ransackers.keys
74 | end
75 | end
76 | ```
77 |
78 | This rule applies to each model included in the search, even in the associations you can restrict fields to search.
79 |
80 |
81 | Now, we have to create a POST route to this action, in your `config/routes.rb` provide a POST route to this controller/action:
82 |
83 | ```ruby
84 | # For example
85 | resources :your_models do
86 | collection do
87 | match :advanced_search, to: 'your_models#index', via: :post
88 | end
89 | end
90 | ```
91 |
92 | We have a ransack search well configured, from this step we will include the Advanced Search query mode in our views.
93 |
94 | In your application layout `app/views/layouts/application.erb`, include a yield in the head section to load ransack advanced search dependencies:
95 | ```html
96 |
97 |
98 |
99 | <%= yield(:ransack_advanced_search_setup) %>
100 |
101 |
102 |
103 |
104 | ```
105 | 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:
106 |
107 | ```ruby
108 | <%= render partial: 'ransack_advanced_search/advanced_search',
109 | locals: {
110 | search_url: advanced_search_your_models_path, # POST route we created above
111 | redirect_path: your_models_path # GET redirect path, to return after some actions
112 | }
113 | %>
114 | ```
115 |
116 | Or, the following for quick_search:
117 |
118 | ```ruby
119 | <%= render partial: 'ransack_advanced_search/quick_search',
120 | locals: {
121 | search_url: advanced_search_your_models_path, # POST route we created above
122 | redirect_path: your_models_path # GET redirect path, to return after some actions
123 | }
124 | %>
125 | ```
126 | IMPORTANT: If you are using saved searches and you want to change from quick search to advanced search views or vice versa, you must provide a new context for this saved searches or delete all saved search for that context before changing the view.
127 |
128 | All done! Enjoy the search!
129 |
130 | ## Saving Searches
131 |
132 | If you want to provide the feature to Save ransack searches, follow these steps.
133 |
134 | Enable Saved Searches configuration in `config/initializers/ransack_advanced_search.rb`:
135 |
136 | ```ruby
137 | config.enable_saved_searches = true
138 | ```
139 |
140 | Run this command to generate the Saved Search Migration:
141 |
142 | $ rails generate ransack_advanced_search:saved_search
143 |
144 | Execute:
145 |
146 | $ rake db:migrate
147 |
148 | In each controller action with the Advanced Search:
149 |
150 | * Include the Saved Search Utils methods:
151 | ```ruby
152 | include RansackAdvancedSearch::SavedSearchUtils
153 | ```
154 |
155 | * Insert this line before creating the search:
156 | ```ruby
157 | # GET /your_models
158 | # GET /your_models.json
159 | # POST /your_models/advanced_search
160 | def index
161 | # Call this methods passing a context(to scope the saved searches, can be any symbol) and the params variable
162 | params[:q] = perform_saved_searches_actions(:your_models_index, params)
163 | @search = YourModel.search(params[:q])
164 | @results = @search.result()
165 | end
166 | ```
167 | IMPORTANT: if you use custom inflections settings, you can receive this error:
168 | ```
169 | Table 'calendario_development.saved_searchs' doesn't exist
170 | ```
171 | To avoid this you will have to include an irregular inflection:
172 | ```ruby
173 | inflect.irregular 'saved_search', 'saved_searches'
174 | ```
175 |
176 | ## i18n Support
177 |
178 | This gem was built using i18n translation supports, and has bult-in support for English (en) and Brazilian Portuguese (pt-BR). If you want to translate to your specific language, add a new locale file in your `config/locales` and translate the values to your language. You can get one of the locales of this project to make it easier to translate to your language.
179 |
180 |
181 | ## Contributing
182 |
183 | 1. Fork it ( https://github.com/davidbrusius/ransack_advanced_search/fork )
184 | 2. Create your feature branch (`git checkout -b my-new-feature`)
185 | 3. Commit your changes (`git commit -am 'Add some feature'`)
186 | 4. Push to the branch (`git push origin my-new-feature`)
187 | 5. Create a new Pull Request
188 |
189 | This project uses MIT-LICENSE.
190 |
--------------------------------------------------------------------------------
/test/dummy/README.rdoc:
--------------------------------------------------------------------------------
1 | == Welcome to Rails
2 |
3 | Rails is a web-application framework that includes everything needed to create
4 | database-backed web applications according to the Model-View-Control pattern.
5 |
6 | This pattern splits the view (also called the presentation) into "dumb"
7 | templates that are primarily responsible for inserting pre-built data in between
8 | HTML tags. The model contains the "smart" domain objects (such as Account,
9 | Product, Person, Post) that holds all the business logic and knows how to
10 | persist themselves to a database. The controller handles the incoming requests
11 | (such as Save New Account, Update Product, Show Post) by manipulating the model
12 | and directing data to the view.
13 |
14 | In Rails, the model is handled by what's called an object-relational mapping
15 | layer entitled Active Record. This layer allows you to present the data from
16 | database rows as objects and embellish these data objects with business logic
17 | methods. You can read more about Active Record in
18 | link:files/vendor/rails/activerecord/README.html.
19 |
20 | The controller and view are handled by the Action Pack, which handles both
21 | layers by its two parts: Action View and Action Controller. These two layers
22 | are bundled in a single package due to their heavy interdependence. This is
23 | unlike the relationship between the Active Record and Action Pack that is much
24 | more separate. Each of these packages can be used independently outside of
25 | Rails. You can read more about Action Pack in
26 | link:files/vendor/rails/actionpack/README.html.
27 |
28 |
29 | == Getting Started
30 |
31 | 1. At the command prompt, create a new Rails application:
32 |
rails new myapp (where
myapp is the application name)
33 |
34 | 2. Change directory to
myapp and start the web server:
35 |
cd myapp; rails server (run with --help for options)
36 |
37 | 3. Go to http://localhost:3000/ and you'll see:
38 | "Welcome aboard: You're riding Ruby on Rails!"
39 |
40 | 4. Follow the guidelines to start developing your application. You can find
41 | the following resources handy:
42 |
43 | * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44 | * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45 |
46 |
47 | == Debugging Rails
48 |
49 | Sometimes your application goes wrong. Fortunately there are a lot of tools that
50 | will help you debug it and get it back on the rails.
51 |
52 | First area to check is the application log files. Have "tail -f" commands
53 | running on the server.log and development.log. Rails will automatically display
54 | debugging and runtime information to these files. Debugging info will also be
55 | shown in the browser on requests from 127.0.0.1.
56 |
57 | You can also log your own messages directly into the log file from your code
58 | using the Ruby logger class from inside your controllers. Example:
59 |
60 | class WeblogController < ActionController::Base
61 | def destroy
62 | @weblog = Weblog.find(params[:id])
63 | @weblog.destroy
64 | logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65 | end
66 | end
67 |
68 | The result will be a message in your log file along the lines of:
69 |
70 | Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71 |
72 | More information on how to use the logger is at http://www.ruby-doc.org/core/
73 |
74 | Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75 | several books available online as well:
76 |
77 | * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78 | * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79 |
80 | These two books will bring you up to speed on the Ruby language and also on
81 | programming in general.
82 |
83 |
84 | == Debugger
85 |
86 | Debugger support is available through the debugger command when you start your
87 | Mongrel or WEBrick server with --debugger. This means that you can break out of
88 | execution at any point in the code, investigate and change the model, and then,
89 | resume execution! You need to install ruby-debug to run the server in debugging
90 | mode. With gems, use
sudo gem install ruby-debug. Example:
91 |
92 | class WeblogController < ActionController::Base
93 | def index
94 | @posts = Post.all
95 | debugger
96 | end
97 | end
98 |
99 | So the controller will accept the action, run the first line, then present you
100 | with a IRB prompt in the server window. Here you can do things like:
101 |
102 | >> @posts.inspect
103 | => "[#
nil, "body"=>nil, "id"=>"1"}>,
105 | #"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107 | >> @posts.first.title = "hello from a debugger"
108 | => "hello from a debugger"
109 |
110 | ...and even better, you can examine how your runtime objects actually work:
111 |
112 | >> f = @posts.first
113 | => #nil, "body"=>nil, "id"=>"1"}>
114 | >> f.
115 | Display all 152 possibilities? (y or n)
116 |
117 | Finally, when you're ready to resume execution, you can enter "cont".
118 |
119 |
120 | == Console
121 |
122 | The console is a Ruby shell, which allows you to interact with your
123 | application's domain model. Here you'll have all parts of the application
124 | configured, just like it is when the application is running. You can inspect
125 | domain models, change values, and save to the database. Starting the script
126 | without arguments will launch it in the development environment.
127 |
128 | To start the console, run rails console from the application
129 | directory.
130 |
131 | Options:
132 |
133 | * Passing the -s, --sandbox argument will rollback any modifications
134 | made to the database.
135 | * Passing an environment name as an argument will load the corresponding
136 | environment. Example: rails console production.
137 |
138 | To reload your controllers and models after launching the console run
139 | reload!
140 |
141 | More information about irb can be found at:
142 | link:http://www.rubycentral.org/pickaxe/irb.html
143 |
144 |
145 | == dbconsole
146 |
147 | You can go to the command line of your database directly through rails
148 | dbconsole. You would be connected to the database with the credentials
149 | defined in database.yml. Starting the script without arguments will connect you
150 | to the development database. Passing an argument will connect you to a different
151 | database, like rails dbconsole production. Currently works for MySQL,
152 | PostgreSQL and SQLite 3.
153 |
154 | == Description of Contents
155 |
156 | The default directory structure of a generated Ruby on Rails application:
157 |
158 | |-- app
159 | | |-- assets
160 | | | |-- images
161 | | | |-- javascripts
162 | | | `-- stylesheets
163 | | |-- controllers
164 | | |-- helpers
165 | | |-- mailers
166 | | |-- models
167 | | `-- views
168 | | `-- layouts
169 | |-- config
170 | | |-- environments
171 | | |-- initializers
172 | | `-- locales
173 | |-- db
174 | |-- doc
175 | |-- lib
176 | | |-- assets
177 | | `-- tasks
178 | |-- log
179 | |-- public
180 | |-- script
181 | |-- test
182 | | |-- fixtures
183 | | |-- functional
184 | | |-- integration
185 | | |-- performance
186 | | `-- unit
187 | |-- tmp
188 | | `-- cache
189 | | `-- assets
190 | `-- vendor
191 | |-- assets
192 | | |-- javascripts
193 | | `-- stylesheets
194 | `-- plugins
195 |
196 | app
197 | Holds all the code that's specific to this particular application.
198 |
199 | app/assets
200 | Contains subdirectories for images, stylesheets, and JavaScript files.
201 |
202 | app/controllers
203 | Holds controllers that should be named like weblogs_controller.rb for
204 | automated URL mapping. All controllers should descend from
205 | ApplicationController which itself descends from ActionController::Base.
206 |
207 | app/models
208 | Holds models that should be named like post.rb. Models descend from
209 | ActiveRecord::Base by default.
210 |
211 | app/views
212 | Holds the template files for the view that should be named like
213 | weblogs/index.html.erb for the WeblogsController#index action. All views use
214 | eRuby syntax by default.
215 |
216 | app/views/layouts
217 | Holds the template files for layouts to be used with views. This models the
218 | common header/footer method of wrapping views. In your views, define a layout
219 | using the layout :default and create a file named default.html.erb.
220 | Inside default.html.erb, call <% yield %> to render the view using this
221 | layout.
222 |
223 | app/helpers
224 | Holds view helpers that should be named like weblogs_helper.rb. These are
225 | generated for you automatically when using generators for controllers.
226 | Helpers can be used to wrap functionality for your views into methods.
227 |
228 | config
229 | Configuration files for the Rails environment, the routing map, the database,
230 | and other dependencies.
231 |
232 | db
233 | Contains the database schema in schema.rb. db/migrate contains all the
234 | sequence of Migrations for your schema.
235 |
236 | doc
237 | This directory is where your application documentation will be stored when
238 | generated using rake doc:app
239 |
240 | lib
241 | Application specific libraries. Basically, any kind of custom code that
242 | doesn't belong under controllers, models, or helpers. This directory is in
243 | the load path.
244 |
245 | public
246 | The directory available for the web server. Also contains the dispatchers and the
247 | default HTML files. This should be set as the DOCUMENT_ROOT of your web
248 | server.
249 |
250 | script
251 | Helper scripts for automation and generation.
252 |
253 | test
254 | Unit and functional tests along with fixtures. When using the rails generate
255 | command, template test files will be generated for you and placed in this
256 | directory.
257 |
258 | vendor
259 | External libraries that the application depends on. Also includes the plugins
260 | subdirectory. If the app has frozen rails, those gems also go here, under
261 | vendor/rails/. This directory is in the load path.
262 |
--------------------------------------------------------------------------------