Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added association_proc #38

Closed
wants to merge 1 commit into from

3 participants

@fabiokr

Allows one to set an association_proc to process an association value, like the already existing label_proc:

config.association_proc = lambda { |association, t| "#{association.id} - #{t}" }
@carlosantoniodasilva
Collaborator

Hey @fabiokr, thanks for your pull request. The implementation is fine, I'm just wondering what's your use case for this option. Thanks again.

@fabiokr

We wanted to add links to associations automatically:

config.association_proc = lambda { |association, t| link_to(t, association) }
@carlosantoniodasilva
Collaborator

Got it. But what happens if you end up using an association that does not map automatically to a url route using polymorphic url? You'd get an exception, that'd be hard to track down. And you can't skip the association proc, so no way to fix unless doing manual work. Still unsure that's a good thing.

@nashby
Collaborator

@carlosantoniodasilva we can add wrap_association to skip thus proc. Right now we have such option for label_proc.

@carlosantoniodasilva
Collaborator

@nashby what!?!? Seriously that we have that? :tongue: I hope that it was not something I have accepted hahaha.

Well, I still think it's not worth having such option, there's no system that's so automatic like that to apply a link to every single association object. In any case, if we have a way to add it as default and opt-out in a per-call basis, I think it's fine either way, as long as the default is to not have a proc.

@nashby
Collaborator

@carlosantoniodasilva it's ok, it wasn't you :wink2: So maybe close it then?

@carlosantoniodasilva
Collaborator

I think so.. if this happens to be asked more and more we may review it again in the future. @fabiokr @nashby thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 28, 2012
  1. @fabiokr

    Added association_proc

    fabiokr authored
This page is out of date. Refresh to see the latest.
View
8 README.md
@@ -205,6 +205,14 @@ Here are some other examples of the many possibilites to custom the output conte
<% end %>
```
+Optionally, if you want to wrap the inner part of the value with some text
+(e.g. adding quotes), you can do so by specifying a proc for `ShowFor.association_proc`
+that will be called with any association text. E.g.:
+
+```ruby
+ ShowFor.association_proc = lambda { |association, t| "'#{t}'" }
+```
+
## Maintainers
* José Valim (http://github.com/josevalim)
View
4 lib/generators/show_for/templates/show_for.rb
@@ -34,4 +34,8 @@
# If you want to wrap the text inside a label (e.g. to append a semicolon),
# specify label_proc - it will be automatically called, passing in the label text.
# config.label_proc = lambda { |l| l + ":" }
+
+ # If you want to wrap the text of an association value (e.g. wrap with quotes),
+ # specify association_proc - it will be automatically called, passing in the association text.
+ # config.association_proc = lambda { |association, t| "'#{t}'" }
end
View
5 lib/show_for.rb
@@ -44,10 +44,13 @@ module ShowFor
mattr_accessor :association_methods
@@association_methods = [ :name, :title, :to_s ]
-
+
mattr_accessor :label_proc
@@label_proc = nil
+ mattr_accessor :association_proc
+ @@association_proc = nil
+
# Yield self for configuration block:
#
# ShowFor.setup do |config|
View
9 lib/show_for/association.rb
@@ -46,7 +46,12 @@ def values_from_association(association, options) #:nodoc:
end
method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
- association.is_a?(Array) ? association.map(&method) : association.try(method)
+
+ association.is_a?(Array) ? association.map{ |a| apply_association_proc(a, method) } : apply_association_proc(association, method)
+ end
+
+ def apply_association_proc(association, method)
+ ShowFor.association_proc ? template.instance_exec(association, association.try(method), &ShowFor.association_proc) : association.try(method)
end
end
-end
+end
View
17 test/association_test.rb
@@ -69,4 +69,21 @@ class AssociationTest < ActionView::TestCase
assert_select "div.show_for p.wrapper p.collection span", "Tag 2"
assert_select "div.show_for p.wrapper p.collection span", "Tag 3"
end
+
+ test "should let you override the association wrapper" do
+ swap ShowFor, :association_proc => proc { |a, v| "|#{v}|" } do
+ with_association_for @user, :company
+ assert_select "div.show_for p.wrapper", /|PlataformaTec|/
+ end
+ end
+
+ test "should let you override the association wrapper with has_many/has_and_belongs_to_many" do
+ swap ShowFor, :association_proc => proc { |a, v| "|#{v}|" } do
+ with_association_for @user, :tags
+ assert_select "div.show_for p.wrapper ul.collection"
+ assert_select "div.show_for p.wrapper ul.collection li", "|Tag 1|"
+ assert_select "div.show_for p.wrapper ul.collection li", "|Tag 2|"
+ assert_select "div.show_for p.wrapper ul.collection li", "|Tag 3|"
+ end
+ end
end
Something went wrong with that request. Please try again.