Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from ersatzryan/ignore-blank
Browse files Browse the repository at this point in the history
Allow accessible scope exclusion for blank value

[ci skip]
  • Loading branch information
laserlemon committed Sep 11, 2013
2 parents 572a6a2 + d99c24d commit c61370f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/periscope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ def periscope_call(chain, scope, param)
method = periscope_method(scope, options)
values = periscope_values(param, options)

if options[:boolean]
values.first ? chain.send(method) : chain
if periscope_ignore?(values.first, options)
chain
else
chain.send(method, *values)
options[:boolean] ? chain.send(method) : chain.send(method, *values)
end
end

def periscope_ignore?(value, options)
if options[:ignore_blank]
periscope_blank?(value)
elsif options[:boolean]
!value
end
end

def periscope_blank?(value)
return true unless value
value.respond_to?(:blank?) ? value.blank? : value.empty?
end

def periscope_method(scope, options)
options[:method] || [options[:prefix], scope, options[:suffix]].compact.join
end
Expand Down
30 changes: 30 additions & 0 deletions spec/shared/periscopic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ def expect_scopes(calls)
model.periscope(foo: "bar")
end

it "allows accessible scope exclusion given a blank param" do
model.should_not_receive(:foo)
model.scope_accessible(:foo, ignore_blank: true)
model.periscope(foo: nil)
model.periscope(foo: "")
model.periscope(foo: [])
model.periscope(foo: {})
end

it "calls accessible scopes with values when ignoring blank params" do
expect_scopes(foo: ["bar"])
model.scope_accessible(:foo, ignore_blank: true)
model.periscope(foo: "bar")
end

it "allows accessible boolean scope exclusion given a blank param" do
model.should_not_receive(:foo)
model.scope_accessible(:foo, boolean: true, ignore_blank: true)
model.periscope(foo: nil)
model.periscope(foo: "")
model.periscope(foo: [])
model.periscope(foo: {})
end

it "calls accessible boolean scopes when ignoring blank params" do
expect_scopes(foo: [])
model.scope_accessible(:foo, boolean: true, ignore_blank: true)
model.periscope(foo: "bar")
end

it "passes along a nil param" do
expect_scopes(foo: [nil])
model.scope_accessible(:foo)
Expand Down

0 comments on commit c61370f

Please sign in to comment.