association prints #<Enumerator:0x007fa2e2a9f2e0> #40

Closed
tomchentw opened this Issue May 22, 2012 · 6 comments

Comments

Projects
None yet
4 participants
@tomchentw

Hi everyone,
I'm appreciated all of u for creating such a great work. However, there's a bug in
"/lib/show_for/association.rb" HERE

starting with line 40,

def values_from_association(association, options) #:nodoc:
      sample = association.is_a?(Array) ? association.first : association

      if options[:method]
        options[:using] = options.delete(:method)
        ActiveSupport::Deprecation.warn ":method is deprecated. Please use :using instead", caller
      end

      method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
      association.is_a?(Array) ? association.map(&method) : association.try(method)
    end

lets assume association = [], so sample = is NIL!!!!
in line 48, if no :using is provided, then ShowFor.association_methods.find would return nil due to sample == nil
We get method == nil, then next line, we call association.map(&method),
ie, [].map(:nil) is called, returning #<Enumerator: []:map>
so it went wrong.

@carlosantoniodasilva

This comment has been minimized.

Show comment
Hide comment
@carlosantoniodasilva

carlosantoniodasilva May 22, 2012

Collaborator

Are you getting an error or it's just the output? This works because it'll never call :nil due to the empty array:

>> [].map &:nil
=> []

Please post your entire show_for code, exactly how it is on your view, and the entire output as well. Thanks.

Collaborator

carlosantoniodasilva commented May 22, 2012

Are you getting an error or it's just the output? This works because it'll never call :nil due to the empty array:

>> [].map &:nil
=> []

Please post your entire show_for code, exactly how it is on your view, and the entire output as well. Thanks.

@nashby

This comment has been minimized.

Show comment
Hide comment
@nashby

nashby May 22, 2012

Collaborator

@carlosantoniodasilva I think @tomchentw means that method is nil (not a Symbol) so:

> [].map &nil
 => #<Enumerator: []:map>
Collaborator

nashby commented May 22, 2012

@carlosantoniodasilva I think @tomchentw means that method is nil (not a Symbol) so:

> [].map &nil
 => #<Enumerator: []:map>
@carlosantoniodasilva

This comment has been minimized.

Show comment
Hide comment
@carlosantoniodasilva

carlosantoniodasilva May 22, 2012

Collaborator

@nashby wrong Carlos ;). Hehe ok, I missed the final part, thanks for claryfing.

I don't have time to look into this anytime soon, so if anyone wants to try out a pull request, would be more than welcome. Thanks!

Collaborator

carlosantoniodasilva commented May 22, 2012

@nashby wrong Carlos ;). Hehe ok, I missed the final part, thanks for claryfing.

I don't have time to look into this anytime soon, so if anyone wants to try out a pull request, would be more than welcome. Thanks!

@nashby

This comment has been minimized.

Show comment
Hide comment
@nashby

nashby May 22, 2012

Collaborator

@carlosantoniodasilva sorry, bro 😓

Collaborator

nashby commented May 22, 2012

@carlosantoniodasilva sorry, bro 😓

@carlosantoniodasilva

This comment has been minimized.

Show comment
Hide comment
@carlosantoniodasilva

carlosantoniodasilva May 23, 2012

Collaborator

@nashby :D thanks for the pull request.

Collaborator

carlosantoniodasilva commented May 23, 2012

@nashby :D thanks for the pull request.

@tomchentw

This comment has been minimized.

Show comment
Hide comment
@tomchentw

tomchentw May 23, 2012

Thanks you all :D

Thanks you all :D

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