Skip to content
This repository

Make `content_tag_for` and `div_for` accepts the array of records #2816

Merged
merged 1 commit into from over 2 years ago

4 participants

Prem Sichanugrist José Valim Vijay Dev Santiago Pastorino
Prem Sichanugrist
Collaborator

So instead of having to do this:

   @items.each do |item|
     content_tag_for(:li, item) do
        Title: <%= item.title %>
     end
   end

You can now do this:

   content_tag_for(:li, @items) do |item|
     Title: <%= item.title %>
   end

(Same goes for the div_for)

actionpack/lib/action_view/helpers/record_tag_helper.rb
... ...
@@ -17,6 +17,19 @@ module ActionView
17 17
       #
18 18
       #    <div id="person_123" class="person foo"> Joe Bloggs </div>
19 19
       #
  20
+      # You can also passed an array of Active Record objects, which will then
1
Vijay Dev Collaborator

s/passed/pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
railties/guides/source/action_view_overview.textile
... ...
@@ -454,6 +454,83 @@ input("post", "title") # =>
454 454
   <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
455 455
 </ruby>
456 456
 
  457
+h4. RecordTagHelper
  458
+
  459
+This module provides methods for generating a container tag, such as a +<div>+, for your record. This this the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.
1
Vijay Dev Collaborator

s/This this/This is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
railties/guides/source/action_view_overview.textile
((13 lines not shown))
  466
+
  467
+<ruby>
  468
+<%= content_tag_for(:tr, @post) do %>
  469
+  <td><%= @post.title %></td>
  470
+<% end %>
  471
+</ruby>
  472
+
  473
+This will generate this HTML output:
  474
+
  475
+<html>
  476
+<tr id="post_1234" class="post">
  477
+  <td>Hello World!</td>
  478
+</tr>
  479
+</html>
  480
+
  481
+You can also supply a HTML attributes as an additional option hash. For example:
1
Vijay Dev Collaborator

remove the 'a'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
railties/guides/source/action_view_overview.textile
((29 lines not shown))
  482
+
  483
+<ruby>
  484
+<%= content_tag_for(:tr, @post, :class => "frontpage") do %>
  485
+  <td><%= @post.title %></td>
  486
+<% end %>
  487
+</ruby>
  488
+
  489
+Will generate this HTML output:
  490
+
  491
+<html>
  492
+<tr id="post_1234" class="post frontpage">
  493
+  <td>Hello World!</td>
  494
+</tr>
  495
+</html>
  496
+
  497
+Additionaly, you can pass a collection of Active Record objects. This method will loops through your objects and create a container for each of them. For example, given +@posts+ is an array of two +Post+ objects:
1
Vijay Dev Collaborator

s/Additionaly/Additionally. Though I prefer, "You can also pass a collection of .. "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Prem Sichanugrist
Collaborator

@vijaydev pull request updated as requested.

Prem Sichanugrist
Collaborator

@josevalim bro, can I have a review of this commit?

actionpack/lib/action_view/helpers/record_tag_helper.rb
... ...
@@ -53,10 +81,20 @@ module ActionView
53 81
       #    <li id="person_123" class="person bar">...
54 82
       #
55 83
       def content_tag_for(tag_name, record, prefix = nil, options = nil, &block)
56  
-        options, prefix = prefix, nil if prefix.is_a?(Hash)
57  
-        options ||= {}
58  
-        options.merge!({ :class => "#{dom_class(record, prefix)} #{options[:class]}".strip, :id => dom_id(record, prefix) })
59  
-        content_tag(tag_name, options, &block)
  84
+        if record.respond_to?(:to_ary)
  85
+          record.map do |single_record|
2
José Valim Owner

Shouldn't this be record.to_ary.map ? I don't think implementing to_ary means it implements all Enumerable methods.

Prem Sichanugrist Collaborator

Yep, I totally agree on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
actionpack/lib/action_view/helpers/record_tag_helper.rb
... ...
@@ -53,10 +81,20 @@ module ActionView
53 81
       #    <li id="person_123" class="person bar">...
54 82
       #
55 83
       def content_tag_for(tag_name, record, prefix = nil, options = nil, &block)
4
José Valim Owner

For readability, maybe we could break this method into two? content_tag_for and content_tag_for_each?

Prem Sichanugrist Collaborator

What do you mean?

  1. Two private methods that performs on multiple/single objects?
  2. Retain the old method, but add another method name content_tag_for_each?
José Valim Owner

I had in mind something like this:

def content_tag_for(tag_name, record, *args, &block)
  if record.respond_to?(:to_ary)
    record.to_ary.map do |single_record|
      capture { content_tag_for_each(tag_name, single_record, *args, &block) }
    end.join("\n")
  else
    content_tag_for_each(tag_name, record, *args, &block)
  end
end

And the content_tag_for_each would do the magic (and would likely be a private method).

Prem Sichanugrist Collaborator

Then we're on the same page. Thanks for suggestion dude, changing it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Prem Sichanugrist Make `content_tag_for` and `div_for` accepts the array of records
So instead of having to do this:

   @items.each do |item|
     content_tag_for(:li, item) do
        Title: <%= item.title %>
     end
   end

You can now do this:

   content_tag_for(:li, @items) do |item|
     Title: <%= item.title %>
   end
b84cee0
Prem Sichanugrist
Collaborator

@josevalim pull request updated.

I think it's better to change the method signature a bit too, to make sure what to expected from the variable.

José Valim josevalim merged commit 036a250 into from September 04, 2011
José Valim josevalim closed this September 04, 2011
José Valim
Owner

Perfect pull request: docs + code + tests. Merged.

Santiago Pastorino
Collaborator

NICE CATCH!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 04, 2011
Prem Sichanugrist Make `content_tag_for` and `div_for` accepts the array of records
So instead of having to do this:

   @items.each do |item|
     content_tag_for(:li, item) do
        Title: <%= item.title %>
     end
   end

You can now do this:

   content_tag_for(:li, @items) do |item|
     Title: <%= item.title %>
   end
b84cee0
This page is out of date. Refresh to see the latest.
16  actionpack/CHANGELOG
... ...
@@ -1,5 +1,21 @@
1 1
 *Rails 3.2.0 (unreleased)*
2 2
 
  3
+* content_tag_for and div_for can now take the collection of records. It will also yield the record as the first argument if you set a receiving argument in your block [Prem Sichanugrist]
  4
+
  5
+  So instead of having to do this:
  6
+
  7
+    @items.each do |item|
  8
+      content_tag_for(:li, item) do
  9
+         Title: <%= item.title %>
  10
+      end
  11
+    end
  12
+
  13
+  You can now do this:
  14
+
  15
+    content_tag_for(:li, @items) do |item|
  16
+      Title: <%= item.title %>
  17
+    end
  18
+
3 19
 * send_file now guess the mime type [Esad Hajdarevic]
4 20
 
5 21
 * Mime type entries for PDF, ZIP and other formats were added [Esad Hajdarevic]
56  actionpack/lib/action_view/helpers/record_tag_helper.rb
@@ -17,6 +17,19 @@ module RecordTagHelper
17 17
       #
18 18
       #    <div id="person_123" class="person foo"> Joe Bloggs </div>
19 19
       #
  20
+      # You can also pass an array of Active Record objects, which will then
  21
+      # get iterates over and yield each record as an argument for the block.
  22
+      # For example:
  23
+      #
  24
+      #    <%= div_for(@people, :class => "foo") do |person| %>
  25
+      #      <%= person.name %>
  26
+      #    <% end %>
  27
+      #
  28
+      # produces:
  29
+      #
  30
+      #    <div id="person_123" class="person foo"> Joe Bloggs </div>
  31
+      #    <div id="person_124" class="person foo"> Jane Bloggs </div>
  32
+      #
20 33
       def div_for(record, *args, &block)
21 34
         content_tag_for(:div, record, *args, &block)
22 35
       end
@@ -42,6 +55,21 @@ def div_for(record, *args, &block)
42 55
       #
43 56
       #    <tr id="foo_person_123" class="person">...
44 57
       #
  58
+      # You can also pass an array of objects which this method will loop through
  59
+      # and yield the current object to the supplied block, reduce the need for
  60
+      # having to iterate through the object (using <tt>each</tt>) beforehand.
  61
+      # For example (assuming @people is an array of Person objects):
  62
+      #
  63
+      #    <%= content_tag_for(:tr, @people) do |person| %>
  64
+      #      <td><%= person.first_name %></td>
  65
+      #      <td><%= person.last_name %></td>
  66
+      #    <% end %>
  67
+      #
  68
+      # produces:
  69
+      #
  70
+      #   <tr id="person_123" class="person">...</tr>
  71
+      #   <tr id="person_124" class="person">...</tr>
  72
+      #
45 73
       # content_tag_for also accepts a hash of options, which will be converted to
46 74
       # additional HTML attributes. If you specify a <tt>:class</tt> value, it will be combined
47 75
       # with the default class name for your object. For example:
@@ -52,12 +80,30 @@ def div_for(record, *args, &block)
52 80
       #
53 81
       #    <li id="person_123" class="person bar">...
54 82
       #
55  
-      def content_tag_for(tag_name, record, prefix = nil, options = nil, &block)
56  
-        options, prefix = prefix, nil if prefix.is_a?(Hash)
57  
-        options ||= {}
58  
-        options.merge!({ :class => "#{dom_class(record, prefix)} #{options[:class]}".strip, :id => dom_id(record, prefix) })
59  
-        content_tag(tag_name, options, &block)
  83
+      def content_tag_for(tag_name, single_or_multiple_records, prefix = nil, options = nil, &block)
  84
+        if single_or_multiple_records.respond_to?(:to_ary)
  85
+          single_or_multiple_records.to_ary.map do |single_record|
  86
+            capture { content_tag_for_single_record(tag_name, single_record, prefix, options, &block) }
  87
+          end.join("\n")
  88
+        else
  89
+          content_tag_for_single_record(tag_name, single_or_multiple_records, prefix, options, &block)
  90
+        end
60 91
       end
  92
+
  93
+      private
  94
+
  95
+        # Called by <tt>content_tag_for</tt> internally to render a content tag
  96
+        # for each record.
  97
+        def content_tag_for_single_record(tag_name, record, prefix, options, &block)
  98
+          options, prefix = prefix, nil if prefix.is_a?(Hash)
  99
+          options ||= {}
  100
+          options.merge!({ :class => "#{dom_class(record, prefix)} #{options[:class]}".strip, :id => dom_id(record, prefix) })
  101
+          if block.arity == 0
  102
+            content_tag(tag_name, capture(&block), options)
  103
+          else
  104
+            content_tag(tag_name, capture(record, &block), options)
  105
+          end
  106
+        end
61 107
     end
62 108
   end
63 109
 end
24  actionpack/test/template/record_tag_helper_test.rb
@@ -4,11 +4,12 @@
4 4
 class Post
5 5
   extend ActiveModel::Naming
6 6
   include ActiveModel::Conversion
  7
+  attr_writer :id, :body
7 8
   def id
8  
-     45
  9
+     @id || 45
9 10
   end
10 11
   def body
11  
-    super || "What a wonderful world!"
  12
+    super || @body || "What a wonderful world!"
12 13
   end
13 14
 end
14 15
 
@@ -58,4 +59,23 @@ def test_div_for_in_erb
58 59
     actual = div_for(@post, :class => "bar") { concat @post.body }
59 60
     assert_dom_equal expected, actual
60 61
   end
  62
+
  63
+  def test_content_tag_for_collection
  64
+    post_1 = Post.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true }
  65
+    post_2 = Post.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true }
  66
+    expected = %(<li class="post" id="post_101">Hello!</li>\n<li class="post" id="post_102">World!</li>)
  67
+    actual = content_tag_for(:li, [post_1, post_2]) { |post| concat post.body }
  68
+    assert_dom_equal expected, actual
  69
+  end
  70
+
  71
+  def test_content_tag_for_collection_is_html_safe
  72
+  end
  73
+
  74
+  def test_div_for_collection
  75
+    post_1 = Post.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true }
  76
+    post_2 = Post.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true }
  77
+    expected = %(<div class="post" id="post_101">Hello!</div>\n<div class="post" id="post_102">World!</div>)
  78
+    actual = div_for([post_1, post_2]) { |post| concat post.body }
  79
+    assert_dom_equal expected, actual
  80
+  end
61 81
 end
77  railties/guides/source/action_view_overview.textile
Source Rendered
@@ -454,6 +454,83 @@ input("post", "title") # =>
454 454
   <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
455 455
 </ruby>
456 456
 
  457
+h4. RecordTagHelper
  458
+
  459
+This module provides methods for generating a container tag, such as a +<div>+, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.
  460
+
  461
+h5. content_tag_for
  462
+
  463
+Renders a container tag that relates to your Active Record Object.
  464
+
  465
+For example, given +@post+ is the object of +Post+ class, you can do:
  466
+
  467
+<ruby>
  468
+<%= content_tag_for(:tr, @post) do %>
  469
+  <td><%= @post.title %></td>
  470
+<% end %>
  471
+</ruby>
  472
+
  473
+This will generate this HTML output:
  474
+
  475
+<html>
  476
+<tr id="post_1234" class="post">
  477
+  <td>Hello World!</td>
  478
+</tr>
  479
+</html>
  480
+
  481
+You can also supply HTML attributes as an additional option hash. For example:
  482
+
  483
+<ruby>
  484
+<%= content_tag_for(:tr, @post, :class => "frontpage") do %>
  485
+  <td><%= @post.title %></td>
  486
+<% end %>
  487
+</ruby>
  488
+
  489
+Will generate this HTML output:
  490
+
  491
+<html>
  492
+<tr id="post_1234" class="post frontpage">
  493
+  <td>Hello World!</td>
  494
+</tr>
  495
+</html>
  496
+
  497
+You can pass a collection of Active Record objects. This method will loops through your objects and create a container for each of them. For example, given +@posts+ is an array of two +Post+ objects:
  498
+
  499
+<ruby>
  500
+<%= content_tag_for(:tr, @posts) do |post| %>
  501
+  <td><%= post.title %></td>
  502
+<% end %>
  503
+</ruby>
  504
+
  505
+Will generate this HTML output:
  506
+
  507
+<html>
  508
+<tr id="post_1234" class="post">
  509
+  <td>Hello World!</td>
  510
+</tr>
  511
+<tr id="post_1235" class="post">
  512
+  <td>Ruby on Rails Rocks!</td>
  513
+</tr>
  514
+</html>
  515
+
  516
+h5. div_for
  517
+
  518
+This is actually a convenient method which calls +content_tag_for+ internally with +:div+ as the tag name. You can pass either an Active Record object or a collection of objects. For example:
  519
+
  520
+<ruby>
  521
+<%= div_for(@post, :class => "frontpage") do %>
  522
+  <td><%= @post.title %></td>
  523
+<% end %>
  524
+</ruby>
  525
+
  526
+Will generate this HTML output:
  527
+
  528
+<html>
  529
+<div id="post_1234" class="post frontpage">
  530
+  <td>Hello World!</td>
  531
+</div>
  532
+</html>
  533
+
457 534
 h4. AssetTagHelper
458 535
 
459 536
 This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.