Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix belongs_to associations with block #51

Closed
wants to merge 1 commit into from

4 participants

@denispeplin

There is attempt to fix issue #50.

The only difference between has_many and belongs_to for show_for is that has_many returns array, and belongs_to returns single object, that is not wrapped inside array. So simple Array.wrap fixes the issue. Only one line of code. But wrapping belongs_to 'collection' inside collection_tag is not needed, it is better to display it inline with its label, so some additional code is needed.

It maybe looks like a hack, because options[:collection_tag] is set to empty string, and wrap_with was modified to handle empty tags correctly. I'm not sure is it better to use some instance variable instead.

@lucasmazza
Owner

@denispeplin I might not be the best one to evaluate your fix, but could you add at least a test in association_test.rb for this? I think that something similar to this test case that deals with associations and blocks will do the job.

@denispeplin

I don't use Test::Unit for my applications and currently don't completely understand how show_for testing package works. I've tried to make failing test for unpatched show_for, without success.
Actually, I recently developed a gem, which also uses Test::Unit, but with dummy Rails application, so it is much easier to setup, and models behavior is closer to real-life models. Maybe it makes the difference, but I'm not sure.

@lucasmazza
Owner

You could try to do something like this:

  test "show_for accepts a block with an argument in belongs_to associations" do
    with_association_for @user, :company do |company|
      company.name.upcase
    end

    assert_select "div.show_for p.wrapper", /PLATAFORMATEC/
  end

/cc @nashby @carlosantoniodasilva

@nashby
Collaborator

@lucasmazza yeah, this test is OK. Anyway I've come up with following solution:

➜  show_for git:(master) ✗ git diff          
diff --git a/lib/show_for/content.rb b/lib/show_for/content.rb
index 2668c3f..c0798e3 100644
--- a/lib/show_for/content.rb
+++ b/lib/show_for/content.rb
@@ -27,7 +27,7 @@ module ShowFor
         when NilClass, Numeric
           value.to_s
         else
-          value
+          block_handler(value, &block)
       end

       options[:content_html] = options.except(:content_tag) i
@@ -46,6 +46,14 @@ module ShowFor
       wrap_with(:collection, response, options)
     end

+    def block_handler(value, &block)
+      if block
+        template.capture(value, &block)
+      else
+        value
+      end
+    end
+
     def translate_blank_html
       template.t(:'show_for.blank_html', :default => translat
     end
diff --git a/test/association_test.rb b/test/association_test.
index ed0ce69..6269861 100644
--- a/test/association_test.rb
+++ b/test/association_test.rb
@@ -42,6 +42,14 @@ class AssociationTest < ActionView::TestCas
     end
   end

+  test "show_for accepts a block with an argument in belongs_
+    with_association_for @user, :company do |company|
+      company.name.upcase
+    end
+
+    assert_select "div.show_for p.wrapper", /PLATAFORMATEC/
+  end
+
   test "show_for accepts :using as option to tell how to retr
     with_association_for @user, :tags, :using => :alternate_n
     assert_select "div.show_for p.wrapper ul.collection"
@nashby
Collaborator
@denispeplin
@denispeplin

And I also checked patch to content.rb within my application. It works as expected.

@rafaelfranca
Collaborator

Sorry for the delay guys.

@nashby your solution seems good. But I would add a elsif block check instead of a new method. Could you open a pull request?

@lucasmazza you are allowed to merge pull requests, don't need to wait me or @carlosantoniodasilva to take a decision if you think it is good.

@denispeplin thank you to send this pull request and to confirming the fix.

@nashby nashby referenced this pull request from a commit in nashby/show_for
@nashby nashby handle belongs_to association with a block
closes #50 #51
2bca8c9
@nashby
Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 26, 2012
  1. @denispeplin
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 4 deletions.
  1. +3 −1 lib/show_for/association.rb
  2. +3 −3 lib/show_for/builder.rb
View
4 lib/show_for/association.rb
@@ -16,7 +16,9 @@ def association(association_name, options={}, &block)
# since we want the collection to be yielded. Otherwise, calculate the values.
value = if collection_block?(block)
collection_block = block
- @object.send(association_name)
+ associations = @object.send(association_name)
+ options[:collection_tag] = '' unless associations.kind_of? Array
+ Array.wrap associations
elsif block
block
else
View
6 lib/show_for/builder.rb
@@ -60,13 +60,13 @@ def apply_wrapper_options!(type, options, value)
def wrap_with(type, content, options) #:nodoc:
tag = options.delete(:"#{type}_tag") || ShowFor.send(:"#{type}_tag")
- if tag
+ if tag.blank?
+ content
+ else
type_class = ShowFor.send :"#{type}_class"
html_options = options.delete(:"#{type}_html") || {}
html_options[:class] = "#{type_class} #{html_options[:class]}".rstrip
@template.content_tag(tag, content, html_options)
- else
- content
end
end
Something went wrong with that request. Please try again.