ransack_advanced_search 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/README.md +107 -0
- data/app/views/ransack_advanced_search/_advanced_search.html.erb +22 -27
- data/app/views/ransack_advanced_search/_saved_searches_list.erb +15 -0
- data/config/locales/ransack.en.yml +1 -0
- data/config/locales/ransack.pt-BR.yml +1 -0
- data/lib/generators/ransack_advanced_search/install_generator.rb +16 -0
- data/lib/generators/templates/ransack_advanced_search.rb +5 -0
- data/lib/ransack_advanced_search/helpers/configuration.rb +26 -0
- data/lib/ransack_advanced_search/version.rb +1 -1
- data/lib/ransack_advanced_search.rb +6 -1
- metadata +6 -2
- /data/config/initializers/{ransack_advanced_search.rb → ransack_advanced_search_setup.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88442c12af23f8798db4d31ab864106694779952
|
4
|
+
data.tar.gz: d230a564262b94f69d58ba9b410d9ed53a1bf8c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41868575d4ab5ab953642a5df1c54707f6596e6771735b7cdb709f1f4729a77de438617150e34e166ee884433219446ba34886600d57934086bfad13ec52abe3
|
7
|
+
data.tar.gz: 31ceeeabd6c1046ba58627bc6fb37fb23a16eb26229fd625a98a508ae705c125cdf4668adbf6f1714fcd56c9714f14622c9a0a41e7625e547b46353a487bb840
|
data/README.md
CHANGED
@@ -30,8 +30,115 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
$ gem install ransack_advanced_search
|
32
32
|
|
33
|
+
Run the generator to install the gem initializer, this will create the file `config/initializers/ransack_advanced_search.rb`:
|
34
|
+
|
35
|
+
$ rails generate ransack_advanced_search:install
|
36
|
+
|
37
|
+
For while we don't need to change this file.
|
38
|
+
|
33
39
|
## Usage
|
34
40
|
|
41
|
+
First, in your controller action that you will use for search, include the following:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# GET /your_models
|
45
|
+
# GET /your_models.json
|
46
|
+
# POST /your_models/advanced_search
|
47
|
+
def index
|
48
|
+
# The ransack search must be in the @search instance variable, because the advanced search will use it to build the search form
|
49
|
+
@search = YourModel.search(params[:q])
|
50
|
+
@results = @search.result()
|
51
|
+
# or, if the above doesn't work
|
52
|
+
@search = YourModel.ransack(params[:q])
|
53
|
+
@results = @search.result()
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Now, we have to create a POST route to this action, in your `config/routes.rb` provide a POST route to this controller/action:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# For example
|
61
|
+
resources :your_models do
|
62
|
+
collection do
|
63
|
+
match :advanced_search, to: 'your_models#index', via: :post
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
We have a ransack search well configured, from this step we will include the Advanced Search query mode in our views
|
69
|
+
|
70
|
+
In your application layout `app/views/layouts/application.erb`, include a yield in the head section to load ransack advanced search dependencies:
|
71
|
+
```html
|
72
|
+
<html>
|
73
|
+
<head>
|
74
|
+
<!-- Your app resources -->
|
75
|
+
<%= yield(:ransack_advanced_search_setup) %>
|
76
|
+
</head>
|
77
|
+
<body>
|
78
|
+
</body>
|
79
|
+
</html>
|
80
|
+
```
|
81
|
+
In the view that you want the advanced search views, insert the following:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
<%= render partial: 'ransack_advanced_search/advanced_search',
|
85
|
+
locals: {
|
86
|
+
search_url: advanced_search_your_models_path, # POST route whe created above
|
87
|
+
redirect_path: your_models_path # GET redirect path, to return after some actions
|
88
|
+
}
|
89
|
+
%>
|
90
|
+
```
|
91
|
+
|
92
|
+
All done! Enjoy the search!
|
93
|
+
|
94
|
+
## Saving Searches
|
95
|
+
|
96
|
+
If you want to provide the feature to Save ransack searches, follow these steps.
|
97
|
+
|
98
|
+
Enable Saved Searches configuration in `config/initializers/ransack_advanced_search.rb`:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
config.enable_saved_searches = true
|
102
|
+
```
|
103
|
+
|
104
|
+
Run this command to generate the Saved Search Migration:
|
105
|
+
|
106
|
+
$ rails generate ransack_advanced_search:saved_search
|
107
|
+
|
108
|
+
Execute:
|
109
|
+
|
110
|
+
$ rake db:migrate
|
111
|
+
|
112
|
+
In each controller action with the Advanced Search:
|
113
|
+
|
114
|
+
* Include the Saved Search Utils methods:
|
115
|
+
```ruby
|
116
|
+
include RansackAdvancedSearch::SavedSearchUtils
|
117
|
+
```
|
118
|
+
|
119
|
+
* Insert this line before creating the search:
|
120
|
+
```ruby
|
121
|
+
# GET /your_models
|
122
|
+
# GET /your_models.json
|
123
|
+
# POST /your_models/advanced_search
|
124
|
+
def index
|
125
|
+
# Call this methods passing a context(to scope the saved searches, can be any symbol) and the params variable
|
126
|
+
params[:q] = perform_saved_searches_actions(:your_models_index, params)
|
127
|
+
@search = YourModel.search(params[:q])
|
128
|
+
@results = @search.result()
|
129
|
+
end
|
130
|
+
```
|
131
|
+
IMPORTANT: if you use custom inflections settings, you can receive this error:
|
132
|
+
```
|
133
|
+
Table 'calendario_development.saved_searchs' doesn't exist
|
134
|
+
```
|
135
|
+
To avoid this you will have to include an irregular inflection:
|
136
|
+
```ruby
|
137
|
+
inlfect.irregular 'saved_search', 'saved_searches'
|
138
|
+
```
|
139
|
+
|
140
|
+
|
141
|
+
|
35
142
|
## Contributing
|
36
143
|
|
37
144
|
1. Fork it ( https://github.com/davidbrusius/ransack_advanced_search/fork )
|
@@ -1,36 +1,28 @@
|
|
1
1
|
<div class="ransack-advanced-search">
|
2
2
|
|
3
3
|
<div class="row">
|
4
|
-
|
5
|
-
<div class="
|
6
|
-
|
7
|
-
<div class="list-group lista-buscas">
|
8
|
-
<% @saved_searches.each do |saved_search| %>
|
9
|
-
<%= link_to url_for(saved_search: saved_search.id), method: :post, class: 'list-group-item' do %>
|
10
|
-
<%= saved_search.description %>
|
11
|
-
<% end %>
|
12
|
-
<% end %>
|
13
|
-
<% if @saved_searches.empty? %>
|
14
|
-
<%= link_to '#', class: 'list-group-item' do %>
|
15
|
-
<em><%= t('ransack.saved_search.empty_list') %></em>
|
16
|
-
<% end %>
|
17
|
-
<% end %>
|
18
|
-
</div>
|
4
|
+
<% if RansackAdvancedSearch.enable_saved_searches %>
|
5
|
+
<div class="col-md-4">
|
6
|
+
<%= render 'ransack_advanced_search/saved_searches_list' %>
|
19
7
|
</div>
|
20
|
-
|
21
|
-
|
8
|
+
<div class="col-md-8">
|
9
|
+
<% else %>
|
10
|
+
<div class="col-md-12">
|
11
|
+
<% end %>
|
22
12
|
<%= search_form_for(@search, url: search_url, html: { method: :post, class: 'form-vertical', role: 'form' }) do |f| %>
|
23
13
|
|
24
14
|
<% setup_search_form f %>
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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 %>
|
34
26
|
|
35
27
|
<h4><%= t('ransack.advanced_search.sort.title') %></h4>
|
36
28
|
<div class="form-group">
|
@@ -53,8 +45,11 @@
|
|
53
45
|
<!-- <%= check_box_tag :distinct, '1', params[:distinct].to_i == 1 %> -->
|
54
46
|
<div class="form-group">
|
55
47
|
<%= f.submit t('ransack.advanced_search.form.submit_text'), class: 'btn btn-primary' %>
|
56
|
-
|
57
|
-
|
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' %>
|
58
53
|
</div>
|
59
54
|
<% end %>
|
60
55
|
</div>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<div class="panel panel-primary">
|
2
|
+
<div class="panel-heading"><%= t('ransack.saved_search.title') %></div>
|
3
|
+
<div class="list-group lista-buscas">
|
4
|
+
<% @saved_searches.each do |saved_search| %>
|
5
|
+
<%= link_to url_for(saved_search: saved_search.id), method: :post, class: 'list-group-item' do %>
|
6
|
+
<%= saved_search.description %>
|
7
|
+
<% end %>
|
8
|
+
<% end %>
|
9
|
+
<% if @saved_searches.empty? %>
|
10
|
+
<%= link_to '#', class: 'list-group-item' do %>
|
11
|
+
<em><%= t('ransack.saved_search.empty_list') %></em>
|
12
|
+
<% end %>
|
13
|
+
<% end %>
|
14
|
+
</div>
|
15
|
+
</div>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module RansackAdvancedSearch
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../../templates", __FILE__)
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
desc "Install Ransack Advanced Search"
|
9
|
+
class_option :orm
|
10
|
+
|
11
|
+
def copy_initializer
|
12
|
+
template "ransack_advanced_search.rb", "config/initializers/ransack_advanced_search.rb"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Configuration
|
2
|
+
|
3
|
+
def configuration
|
4
|
+
yield self
|
5
|
+
end
|
6
|
+
|
7
|
+
def define_setting(name, default = nil)
|
8
|
+
class_variable_set("@@#{name}", default)
|
9
|
+
|
10
|
+
define_class_method "#{name}=" do |value|
|
11
|
+
class_variable_set("@@#{name}", value)
|
12
|
+
end
|
13
|
+
|
14
|
+
define_class_method name do
|
15
|
+
class_variable_get("@@#{name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def define_class_method(name, &block)
|
22
|
+
(class << self; self; end).instance_eval do
|
23
|
+
define_method name, &block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,7 +1,12 @@
|
|
1
|
-
require 'ransack_advanced_search/engine'
|
2
1
|
require 'rails'
|
3
2
|
require 'ransack'
|
4
3
|
|
4
|
+
require 'ransack_advanced_search/engine'
|
5
|
+
require 'ransack_advanced_search/version'
|
6
|
+
require 'ransack_advanced_search/helpers/configuration'
|
5
7
|
|
6
8
|
module RansackAdvancedSearch
|
9
|
+
extend Configuration
|
10
|
+
|
11
|
+
define_setting :enable_saved_searches, false
|
7
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ransack_advanced_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Brusius
|
@@ -85,16 +85,20 @@ 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/_saved_searches_list.erb
|
88
89
|
- app/views/ransack_advanced_search/_sort_fields.erb
|
89
90
|
- app/views/ransack_advanced_search/_value_fields.erb
|
90
|
-
- config/initializers/
|
91
|
+
- config/initializers/ransack_advanced_search_setup.rb
|
91
92
|
- config/locales/ransack.en.yml
|
92
93
|
- config/locales/ransack.pt-BR.yml
|
93
94
|
- config/routes.rb
|
95
|
+
- lib/generators/ransack_advanced_search/install_generator.rb
|
94
96
|
- lib/generators/ransack_advanced_search/saved_search_generator.rb
|
95
97
|
- lib/generators/templates/create_ransack_advanced_search_saved_search.rb
|
98
|
+
- lib/generators/templates/ransack_advanced_search.rb
|
96
99
|
- lib/ransack_advanced_search.rb
|
97
100
|
- lib/ransack_advanced_search/engine.rb
|
101
|
+
- lib/ransack_advanced_search/helpers/configuration.rb
|
98
102
|
- lib/ransack_advanced_search/version.rb
|
99
103
|
- lib/tasks/ransack_advanced_search_tasks.rake
|
100
104
|
- test/dummy/README.rdoc
|
File without changes
|