Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add attribute proc for greater control over markup #48

blakehilscher opened this Issue Jul 21, 2012 · 6 comments


None yet
4 participants

blakehilscher commented Jul 21, 2012

I'm proposing that we add this on line 41 of lib/show_for/content.rb #content

  content = block.call(content).html_safe if ![Array, Hash].include?(value.class) && block_given?

This would allow us to compose views like such:

= show_for(@user) do |r|
  = r.attribute( :first_name ){|name| "Posted by: " + link_to(name, user_path(user)) }
  = r.attribute( :email ) do |e|
    = link_to email, "mailto:#{e}"

Here is a failing test for the case:

  test "show_for#content given a block should be wrapped in the result" do
    with_attribute_for( @user, :name, {}, ){|name| "<div class='block'>#{name}</div>".html_safe }
    assert_select "p.wrapper.user_name div.block"

rafaelfranca commented Jul 21, 2012

Seems good.

@carlosantoniodasilva @josevalim What do you think?


carlosantoniodasilva commented Sep 8, 2012

I don't think you'll get there, since this method is only triggered for Array / Hash classes. I thought giving a block for attribute was already supported, but I don't quite remember since it's been a while I didn't use show_for. If it's not, we can add support for it for sure.

I would like a attribute proc. Our use case is simple: some people type line breaks in textareas. These don't get rendered with show_for. They get rendered as one line.

I would like to wrap these values in simple_format by default.


carlosantoniodasilva commented Sep 12, 2012

@michelgrootjans And passing a block doesn't work right now?

Passing a block for every field with type (text) is rather cumbersome. I would like to specify by default that text types get wrapped in simple_format. There's no way to configure this that I know of. Something along the lines of:

ShowFor.setup do |config|
    config.value_proc = lambda {|v| simple_format(v)}

Just dreaming out loud now:

ShowFor.setup do |config|
    config.value_proc = lambda {|v| simple_format(v)}, :if => :text

carlosantoniodasilva commented Sep 12, 2012

Ok, but I think that's definitely a different issue/feature request :).

In any case, I don't think ShowFor would support something like that, because there'll be times where you will not want simple_format to take in - there're always very particular cases - and in these cases, there'll be no way to handle it.

I can't think of another a possibility off the top of my head now, other than using simple_format with a block where necessary, but I'll try to give it some thought. Thanks.

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