How can I add "DOES NOT CONTAIN" option to field_search?

37 views
Skip to first unread message

Lindsay Morris

unread,
Jun 6, 2011, 10:11:32 AM6/6/11
to actives...@googlegroups.com
My users will need to filter out some records using a wildcard. 
This doesn't seem to be built-in.  Where would I go to add it? Or am I just a rev or two out of date, and it's already there?

Hernan Astudillo

unread,
Jun 6, 2011, 4:33:07 PM6/6/11
to actives...@googlegroups.com
see there a != option in the select before the input field?

> --
> You received this message because you are subscribed to the Google Groups
> "ActiveScaffold : Ruby on Rails plugin" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/activescaffold/-/RzctUGJuMUprYVFK.
> To post to this group, send email to actives...@googlegroups.com.
> To unsubscribe from this group, send email to
> activescaffol...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/activescaffold?hl=en.
>

lmorris99

unread,
Jun 6, 2011, 5:03:09 PM6/6/11
to actives...@googlegroups.com
Yes, but that's not the same as DOES NOT CONTAIN.
CONTAINS implements a wildcard search;
!= does not.
At least that's what it seems like when I try it.

Hernan Astudillo

unread,
Jun 6, 2011, 8:15:52 PM6/6/11
to actives...@googlegroups.com
is it set to :full ?
config.field_search.text_search = :full
full is wildcard search: '%?%'

> --
> You received this message because you are subscribed to the Google Groups
> "ActiveScaffold : Ruby on Rails plugin" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/activescaffold/-/dk0zX0lFUi1ybVFK.

vhochstein

unread,
Jun 7, 2011, 5:38:51 AM6/7/11
to ActiveScaffold : Ruby on Rails plugin
Hi,

take a look at finder.rb
extend StringComparators hash as follows:
StringComparators = {
:not_contains => '%?%',
:contains => '%?%',
:begins_with => '?%',
:ends_with => '%?'
}

override def condition_for_range to apply correct search condition for
not_contains.

Hope that helps.

--
Volker

lmorris99

unread,
Jun 7, 2011, 2:23:33 PM6/7/11
to actives...@googlegroups.com
:full is the default; I set it on anyway and tried to search where my field named "entity" != _ART.
It didn't work, and the log says this:
"entity"=>{"from"=>"_ART", "to"=>"", "opt"=>"!="},

When I try the same search with "CONTAINS" instead of "!=", it does what I expect and the log says this:
 "entity"=>{"from"=>"_ART", "to"=>"", "opt"=>"%?%"},

I used  Volker's advice, changing finder.rb

 StringComparators = {
      :doesnt_contain    => '!%?%',    #<-- added this line

      :contains    => '%?%',
      :begins_with => '?%',
      :ends_with   => '%?'
    }
  and also around line 94:
       elsif ActiveScaffold::Finder::StringComparators.values.include?(value[:opt])
          if value[:opt].match(/\!/)
                  value[:opt].delete! "!"
                  ["#{column.search_sql} NOT LIKE ?", value[:opt].sub('?', value[:from])]
          else
                  ["#{column.search_sql} LIKE ?", value[:opt].sub('?', value[:from])]
          end

Seems to be working.
Thanks all!

lmorris99

unread,
Jun 8, 2011, 11:16:43 AM6/8/11
to actives...@googlegroups.com
Well, it's mostly working... but there's this little problem:
I use the search window to say " [Field A] DOESNT CONTAIN [thing x]"
I click Search, and it finds what I want. Great.
Now I close the search window (with "x", upper right);
I click "Search" to re-open it.
The window is now loaded with  "
[Field A] CONTAINS [thing x]".
It should still be loaded as before,
" [Field A] DOESNT CONTAIN [thing x]"

Any hints on why it got switched, or where to fix it?

vhochstein

unread,
Jun 9, 2011, 4:10:14 AM6/9/11
to ActiveScaffold : Ruby on Rails plugin
As a starting point:

Debug into
def active_scaffold_search_range(column, options)
---
end

And take a look why selected option for select box is wrong.

--
Volker

lmorris99

unread,
Jun 16, 2011, 3:23:17 PM6/16/11
to actives...@googlegroups.com
Thanks Volker, but after two hours of looking at things in the debugger and trying to trace it through, I give up.
If anybody else wants to give active_scaffold a "DOES NOT CONTAIN" feature, please feel free to contact me.

vhochstein

unread,
Jun 17, 2011, 6:14:45 AM6/17/11
to ActiveScaffold : Ruby on Rails plugin
Hi,

just tried it out and it works for me without any issue...

I ve changed the following:
ActiveScaffold::Finder::StringComparators[:not_contains] = 'not%?%'

Override Method in one of my controllers:
def self.condition_for_range(column, value, like_pattern = nil)
if !value.is_a?(Hash)
if column.column.nil? || column.column.text?
["#{column.search_sql}
#{ActiveScaffold::Finder.like_operator} ?", like_pattern.sub('?',
value)]
else
["#{column.search_sql} = ?", column.column.type_cast(value)]
end
elsif value[:from].blank?
nil
elsif ActiveScaffold::Finder::StringComparators.values.include?
(value[:opt])
if value[:opt] ==
ActiveScaffold::Finder::StringComparators[:not_contains]
["#{column.search_sql} NOT LIKE ?", value[:opt]
[3..value[:opt].length].sub('?', value[:from])]
else
["#{column.search_sql} LIKE ?", value[:opt].sub('?',
value[:from])]
end
elsif value[:opt] == 'BETWEEN'
["#{column.search_sql} BETWEEN ? AND ?", value[:from],
value[:to]]
elsif ActiveScaffold::Finder::NumericComparators.include?
(value[:opt])
["#{column.search_sql} #{value[:opt]} ?", value[:from]]
else
nil
end
end

Lindsay Morris

unread,
Jun 19, 2011, 6:50:05 AM6/19/11
to actives...@googlegroups.com
Thanks so much!
I got that working, and it looks great!
Deeply appreciate the help.
Reply all
Reply to author
Forward
0 new messages