Created
October 14, 2012 01:07
-
-
Save steveklabnik/3886837 to your computer and use it in GitHub Desktop.
Some thoughts on real ViewModels for Draper 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeController < AC::Base | |
def show | |
foo = Foo.first | |
bar = Bar.first | |
show_model = ShowViewModel.new(foo, bar) | |
render show_model | |
end | |
end | |
# the view would have exactly one ivar defined, @model. it'd contain the same thing as show_model. No other ivars would be exported. | |
# by defualt, this would still render app/views/somes/show.html.erb, but you could override it: | |
class ShowViewModel < Draper::ViewModel | |
def template_location | |
"foo/bar" | |
end | |
end | |
# this would then render app/views/foo/bar.html.erb |
Why not just use Cells or Apotomo widgets for this!? I plan to use Focused Controller, Draper and Cells/Apotomo in combintion myself. Perhaps even with decent exposure. A beautiful, encapsulated fully OO approach as I see it. We just need a few full examples of this stack in action... no need to reinvent the whole thing again and again IMO.
Cells is about multiple controllers and views per page. This is just one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On a related, but exactly the same, note, I've been "presenter-izing" Draper models lately. Seems to work okay, but I'd image we could come up with a clean API to make it more simple. https://gist.github.com/3887381