ernie/ransack — Issue #41: Generating Inbound links to populate advanced search forms
Author: arjes
Created: 2011-11-14 14:43:53+00:00
State: open

This is more of a request/am I missing this somewhere

It is easy enough to create inbound links that will pre-populate the simple form, such as:
http://ransack-demo.herokuapp.com/users?utf8=%E2%9C%93&q%5Bfirst_name_or_last_name_cont%5D=&q%5Bemail_cont%5D=&q%5Bposts_title_cont%5D=t&commit=Search

The query params can be easily written by a human. However the query params generated by the Adv. search are much more complicated, the inbound link will filter the results but it will not fill-in the form
http://ransack-demo.herokuapp.com/users/advanced_search?q[first_name_or_last_name_cont]=&q[email_cont]=&q[posts_title_cont]=t&commit=Search

I'm looking over the code for how to modify the core code but I'm just not that good. In the mean time I just reversed the query param's structure and came up with this helper method:

```
def ransack_query_hash(params)
ret_hash = {}
i = 0
ret_hash['g'] = {}
params.each do |group_type, values|
group = {}
group['m'] = group_type == :all ? 'and' : 'or'

group['c'] = {}
j = 0
values.each do |property,details|
group['c'][j.to_s] = {'a' => {"0" => {'name'=> property}}, 'p' => details[:p].to_s, 'v' => [*details[:v]] }
j += 1
end
ret_hash['g'][i.to_s] = group
i += 1
end
ret_hash
end
```

This function takes input like:
```
:all => {
:sub_object_parent_id => {
:p => 'eq_any',
:v => rbs.sector_ids
},
:sub_object_parent_type => {
:p => 'eq',
:v => 'Rbs'
}
}
```

This will build a param hash understood by the adv search form and hence pre-populate the form.

Is there an easier way to get this done?