Today I would like to introduce a new little feature for field search.
Let s assume you have a controller which shows lots of log entries and you would like to limit these log entries to the current day as a default, however if the user wants to take a look at older ones that should be also possible.
You might achieve that by using conditions_for_collection in your controller, but I think this is nt the right way.
The perfect solution for this requirement is a default field search condition. If user has nt specified a search condition we will use the default one.
Lets take a look how that might work. We do not have a log entry model, but howto app has a player model/controller.
Initially, We will setup our players controller to use fieldsearch and we will turn on human conditions:
class PlayersController < ApplicationController active_scaffold :player do |conf| conf.actions.swap :search, :field_search conf.field_search.human_conditions = true end end
The following code will enable our default search condition:
conf.field_search.default_params = {:salary => {"from"=>"400","to"=>"", "opt"=>"<"}}
I hope you except my excuse that there is nt a nice looking dsl to define a default search condition, but I ve decided to just use the format of the field search params sent by the field search form. The easiest why to get your required default_params value is by simply submitting the field search form and copying the params from your development log.
Our default search will only list players, which have a salary below 400.
That s already a cool thing, but let s assume you have a requirement to dynamically create a default search condition, e.g. specific to the current_user. That can be implemented by using a proc. I will show you with a quite stupid example, which will change our existing hard coded 400 to a random one.
Firstly, we have to add a method to our controller which returns a random number.
def random_salary_limit 400 + rand(100) end
Secondly, we have to adapt our existing default_params to use that random number method.
conf.field_search.default_params = Proc.new { {:salary => {"from"=> random_salary_limit.to_s, "to"=>"", "opt"=>"<"}}}
You can try it out. If you click action link on the right-hand side of human condition in your list view you hopefully will see that human conditions will change after every click.
Thanks a lot for your attention and good luck.
Daniel
Dec 18, 2010 @ 12:08:37
It seems there is a typo…
active_scaffold :team do |conf| # should (may be) :
active_scaffold :player do |conf|
vhochstein
Dec 18, 2010 @ 14:02:57
Thanks a lot. I ve fixed that.
Daniel
Mar 28, 2011 @ 14:46:52
Hi,
What is the best way to pass search params when calling active_scaffold controller ?
Thanks !
Dan
vhochstein
Mar 31, 2011 @ 12:29:38
via http request ?
I would recommend that you perform a field search in your application with your specific conditions and then just copy search params
Hope that helps.
Lindsay Morris
Apr 22, 2011 @ 01:06:25
Great thinking, Volker: “would like to limit these log entries to the current day as a default, however if the user wants to take a look at older ones that should be also possible.”
conditions_for_collection fails us here. The user is stuck with whatever selection conditions we put in the controller.
But conditions_for_collection is more flexible. I can say:
def conditions_for_collection
[‘access IN (?)’, [‘UNAVAILABLE’, ‘READWRITE’]]
[‘status IN (?)’, [‘FULL’]]
end
In other words, I can search for multiple values on a field, and I can do that on multiple fields.
The “conf.field_search.default_params = ” technique seems to limit me to searching for just one value on one field.
Can I get the best of both worlds? That is, can I use your “conf.field_search.default_params = ” technique and tell it to search where access is EITHER of two values?
And also, do the above AND do it again for a second field?
Thanks for your continued wisdom here.
Lindsay Morris
Apr 22, 2011 @ 01:13:56
Hmmm, looks like I’m wrong about “and do it again for a seoncd field”. my development.log shows that only the second line is working.
Oh well.
My first questions is still valid: can I use default_params to search out either of two different values?
vhochstein
Apr 22, 2011 @ 11:38:13
field_search ANDs each column search conditions together. There s no way to OR them together.
However, if I take your example [‘access IN (?)’, [‘UNAVAILABLE’, ‘READWRITE’]] you can achieve this with a multi_select search_ui for the access attribute.
Just select both values in the multiselect box and field_search will searching for both values.
Bottom line: you are quite flexbile in defining search condition for one column. However, field_search ANDs always search_conditions in case you define them for more than one column.
Hope that helps.
Hope that helps.
Lindsay Morris
Apr 22, 2011 @ 11:46:12
That DOES help. Thanks.
Related question: On of the drop-down operators in field_search is “Contains”. Could I get “Does not contain” somehow?
Or can I use SQL regexes in there? For example, I have a Filesystem type field that has values like
WinNT
AIX
NTFS
API
API:MSExc
and I would like to let the user filter out all the API-anything ones. Is there a way?
vhochstein
Apr 26, 2011 @ 08:12:18
Does not contain is nt supported out of the box, you have to do appropriate overides to achieve this.
SQL reqex are as well not supported, but as can be extended to do so.
On Form side you have to create a new search_ui type regex eg, or if you just would like to implement it for one column, you have to do a search_ui form column override..
Next step would be to create a condition_for overide either for search_ui type or for a specific column.
Please take a look at finder.rb to see examples for condition_for overrides.
Hope that helps
Lucas
Jun 16, 2011 @ 19:22:18
I can’t find anywhere better to put this question, so it is here, where it is slightly related.
On my list page, I am only displaying a few fields, and the table has 30+. How can I change the query from “select table.*” to “select table.[fields I am displaying]”? Have been searching around with no luck.
Thanks!
vhochstein
Jun 17, 2011 @ 14:09:28
take a look at: https://groups.google.com/group/activescaffold/browse_thread/thread/6516134d500050ae?hl=de