Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from ersatzryan/ignore-blank

Allow accessible scope exclusion for blank value

[ci skip]
  • Loading branch information...
commit c61370f510d8549b865297a77a1d0019d313b42c 2 parents 572a6a2 + d99c24d
@laserlemon authored
Showing with 46 additions and 3 deletions.
  1. +16 −3 lib/periscope.rb
  2. +30 −0 spec/shared/periscopic.rb
View
19 lib/periscope.rb
@@ -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
View
30 spec/shared/periscopic.rb
@@ -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)

0 comments on commit c61370f

Please sign in to comment.
Something went wrong with that request. Please try again.