Add :value option to attribute (#17) #27

merged 4 commits into from Jan 18, 2012


None yet

3 participants

gt-sdi commented Jan 12, 2012

Have added a :value option to attribute per Issue #17.

Believe this respects the intent of the original request and the general behavior of show_for.

@rafaelfranca rafaelfranca commented on an outdated diff Jan 13, 2012
@@ -1,2 +1,3 @@
rafaelfranca Jan 13, 2012 collaborator

Please, do not change the gitignore file to fit your environment needs.

@rafaelfranca rafaelfranca commented on an outdated diff Jan 13, 2012
@@ -34,6 +35,32 @@ def attributes(*attribute_names)
rafaelfranca Jan 13, 2012 collaborator

Use the proper indentation here.


I think that the code could be improved, and I didn't undertand the feature, so @carlosantoniodasilva please, take a look at this.

gt-sdi added some commits Jan 13, 2012
gt-sdi commented Jan 13, 2012

@rafaelfranca thank you for your constructive criticisms. Have reverted the .rvmrc and fixed the indent.

Would appreciate any suggestions regarding your comment that the "code could be improved".


Hey guys, I had to remember what was that issue for, it's been over a year I opened that :).

So, the idea is that when using ShowFor and having to do some conversion to show a value, let's say present a user first + last name using a helper, you'd have to use the block syntax:

show_for @user do |s|
  s.attribute(:name) { user_name(@user) }

The idea is that you could give a :value option instead of the block:

s.attribute :name, :value => user_name(@user)

This seems good to me. I'm going to apply this patch for now and review it.

Thanks! :)

@carlosantoniodasilva carlosantoniodasilva merged commit edfc1c3 into plataformatec:master Jan 18, 2012
gt-sdi commented Jan 21, 2012

Thanks Carlos ...

It does work as you intended.

show_for @user do |s|
 s.attribute :name, :value => :user_name

will return @user.user_name. If the attribute is a Collection it will call the method on every member of the collection.

You can also pass :value => { Some_method(@user) }

Or pass a Proc :value => lambda { do_some_stuff }

Nothing you can't do with the regular block, so the obvious use case is the first one: :value => :instance_method

What I didn't do is make any changes to the documentation to reflect, but would be glad to if you would like.


Sure, docs are always welcome =).

I've removed the lambda/proc feature, I believe, as you said, that passing a block is easier in such cases. And we are just adding this feature to avoid the block way in some use cases, so I think it may get people confused on when to use the block and when to use :value with lambda/proc =).


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