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

Commit

Permalink
Allow accessible scope exclusion for blank value
Browse files Browse the repository at this point in the history
  • Loading branch information
ersatzryan committed Sep 11, 2013
1 parent 572a6a2 commit d99c24d
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
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
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 d99c24d

Please sign in to comment.