Skip to content

Commit

Permalink
Allow to skip array! iterations by calling next
Browse files Browse the repository at this point in the history
  • Loading branch information
rwz committed Oct 6, 2014
1 parent 67c654e commit 81a6330
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,26 @@ end
Top level arrays can be handled directly. Useful for index and other collection actions.

``` ruby
# @people = People.all
json.array! @people do |person|
json.name person.name
json.age calculate_age(person.birthday)
# @comments = @post.comments

json.array! @comments do |comment|
next if comment.marked_as_spam_by?(current_user)

json.body comment.body
json.author do
json.first_name comment.author.first_name
json.last_name comment.author.last_name
end
end

# => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ]
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
```

You can also extract attributes from array directly.

``` ruby
# @people = People.all

json.array! @people, :id, :name

# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
Expand Down
11 changes: 8 additions & 3 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ def _merge_block(key, &block)
end

def _read(key, default = nil)
@attributes.fetch(_key(key)){ default }
_blank? ? default : @attributes.fetch(_key(key)){ default }
end

def _write(key, value)
@attributes = {} if _blank?
@attributes[_key(key)] = value
end

Expand All @@ -280,12 +281,12 @@ def _set_value(key, value)
def _map_collection(collection)
collection.map do |element|
_scope{ yield element }
end
end - [BLANK]
end

def _scope
parent_attributes, parent_formatter = @attributes, @key_formatter
@attributes = {}
@attributes = BLANK
yield
@attributes
ensure
Expand All @@ -308,6 +309,10 @@ def _merge_values(attributes, hash_or_array)

attributes
end

def _blank?
BLANK == @attributes
end
end

require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
Expand Down
14 changes: 14 additions & 0 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,20 @@ class JbuilderTest < ActiveSupport::TestCase
assert_equal 'world', result.second['content']
end

test 'it allows using next in array block to skip value' do
comments = [ Comment.new('hello', 1), Comment.new('skip', 2), Comment.new('world', 3) ]
result = jbuild do |json|
json.array! comments do |comment|
next if comment.id == 2
json.content comment.content
end
end

assert_equal 2, result.length
assert_equal 'hello', result.first['content']
assert_equal 'world', result.second['content']
end

test 'extract attributes directly from array' do
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]

Expand Down

0 comments on commit 81a6330

Please sign in to comment.