Genius / perspectives

Render shared views on the client OR on the server
https://github.com/RapGenius/perspectives
MIT License
77 stars 5 forks source link

Unexpected behavior in calculation of default `as` option for nested collections #3

Open emerson-h opened 9 years ago

emerson-h commented 9 years ago

Specifically, when creating a perspective for a plain old ruby object, it invokes .base_class on the class of the first collection member.

Additionally, for namespaced classes, the default has an unexpected value when used to construct the Perspectives for the Collection. In many cases the behavior is counterintuitive and can result in an ArgumentError being raised by asset_valid_params!.

For instance given these classes and perspectives:

module Foo
  class FooClass
    def bazes
      [
        Foo::Bar::Baz.new,
        Foo::Bar::Baz.new
      ]
    end
  end
  module Bar
    class Baz 
       ...
    end
  end
end

# Foo Perspective
module Foo
  module FooClasses
    class Show
      param :foo

      nested_collection '/foo/bar/baz/show',
        collection: proc { foo.bazes },
        property: :bazes
    end
  end
end

# Baz Perspective
module Foo
  module Bar
    module Baz
      class Show
        param :baz

        ....
      end
    end
  end
end

Trying to render the foo/foo_classes/show perspective will raise an ArgumentError because it does not instantiate the baz show perspective with a :baz param. The default param key in this case is :foo::bar::baz.

It seems more advantageous to default to the demodulized class name, and to only use the base_class method if it is available so that nested collections can be used with POROs.

I've created a fix that does this in my fork of the project. When you have a chance can you take a look, and add your thoughts about what this behavior should be.

Thanks!