Skip to content

Commit

Permalink
keep filter conditions from being deleted
Browse files Browse the repository at this point in the history
This a backport of 4dd5b10 from master to 0-6-stable

Ref: #2523, #2832
  • Loading branch information
seanlinsley committed Dec 29, 2013
1 parent 2888592 commit fa0edec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
8 changes: 3 additions & 5 deletions lib/active_admin/filters/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ def active_admin_filters_form_for(search, filters, options = {})
form_for search, options do |f|
filters.group_by{ |o| o[:attribute] }.each do |attribute, array|
opts = array.last # grab last-defined `filter` call from DSL
should = opts.delete(:if) || proc{ true }
shouldnt = opts.delete(:unless) || proc{ false }
return if opts.key?(:if) && !call_method_or_proc_on(self, opts[:if])
return if opts.key?(:unless) && call_method_or_proc_on(self, opts[:unless])

if call_method_or_proc_on(self, should) && !call_method_or_proc_on(self, shouldnt)
f.filter attribute, opts
end
f.filter attribute, opts.except(:if, :unless)
end

buttons = content_tag :div, :class => "buttons" do
Expand Down
3 changes: 1 addition & 2 deletions lib/active_admin/filters/resource_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def initialize(*)
#
# @return [Array] Filters that apply for this resource
def filters
return [] unless filters_enabled?
filter_lookup
filters_enabled? ? filter_lookup : []
end

# Setter to enable / disable filters on this resource.
Expand Down
48 changes: 20 additions & 28 deletions spec/unit/filters/filter_form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,34 +308,26 @@ def filter(name, options = {})
end

describe "conditional display" do

context "with :if block" do
let(:body) do
filter :body, :if => proc{true}
filter :author, :if => proc{false}
end

it "should be displayed if true" do
body.should have_tag("input", :attributes => { :name => "q[body_contains]"})
end

it "should NOT be displayed if false" do
body.should_not have_tag("input", :attributes => { :name => "q[author_id_eq]"})
end
end

context "with :unless block" do
let(:body) do
filter :created_at, :unless => proc{false}
filter :updated_at, :unless => proc{true}
end

it "should be displayed if false" do
body.should have_tag("input", :attributes => { :name => "q[created_at_gte]"})
end

it "should NOT be displayed if true" do
body.should_not have_tag("input", :attributes => { :name => "q[updated_at_gte]"})
[:if, :unless].each do |verb|
should = verb == :if ? "should" : "shouldn't"
if_true = verb == :if ? :should : :should_not
if_false = verb == :if ? :should_not : :should
context "with #{verb.inspect} proc" do
it "#{should} be displayed if true" do
body = filter :body, verb => proc{ true }
body.send if_true, have_tag("input", attributes: {name: "q[body_contains]"})
end
it "#{should} be displayed if false" do
body = filter :body, verb => proc{ false }
body.send if_false, have_tag("input", attributes: {name: "q[body_contains]"})
end
it "should still be hidden on the second render" do
filters = [attribute: :body, verb => proc{ verb == :unless }]
2.times do
body = render_filter scope, filters
body.should_not have_tag "input", attributes: {name: "q[body_contains]"}
end
end
end
end
end
Expand Down

0 comments on commit fa0edec

Please sign in to comment.