Skip to content

Commit

Permalink
add "except" option to extends
Browse files Browse the repository at this point in the history
  • Loading branch information
perryqh committed Aug 15, 2022
1 parent 219595f commit 3b084a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rabl/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def attribute(name, options = {})
# node(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
def node(name, options = {}, &block)
return unless resolve_condition(options)
return if @options.has_key?(:except) && [@options[:except]].flatten.include?(name)

result = block.call(@_object)
if name.present?
Expand Down
28 changes: 28 additions & 0 deletions test/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@
build_hash @user, :node => [{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } }]
end.equivalent_to({:foo => 'bar'})

asserts "that it excludes nodes in the :except list" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: [:foo]}
end.equivalent_to({})

asserts "that it excludes nodes in the :except attribute" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: :foo}
end.equivalent_to({})

asserts "that it has node not in except list" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: [:unknown]}
end.equivalent_to({:foo => 'bar'})

asserts "that it has multiple nodes" do
build_hash(@user,
{:node => [{ :name => :foo, :block => lambda { |u| "bar" } },
{:name => :baz, :block => lambda{|_u| "bar"}},
{:name => :bam, :block => lambda{|_u| "boom"}}]},
{except: [:unknown, :baz]})
end.equivalent_to({:foo => 'bar', :bam => 'boom'})

asserts "that using object it has node :boo" do
build_hash @user, :node => [
{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } },
Expand Down Expand Up @@ -297,6 +317,14 @@
mock(e).render.returns({:user => 'xyz'}).subject
b.to_hash(false)
end.equivalent_to({:user => 'xyz'})

asserts "that it generates with exclude 'except' option" do
b = builder nil, :extends => [{ :file => 'users/show', :options => { :except => :user_id }, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('users/show')
mock(b).partial_as_engine('users/show',{ :object => @user, except: :user_id}).returns(e)
mock(e).render.returns({:user => 'xyz'}).subject
b.to_hash(@user)
end.equivalent_to({:user => 'xyz'})
end

context "#resolve_conditionals" do
Expand Down

0 comments on commit 3b084a8

Please sign in to comment.