trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

Default show/call/to_s/render method #324

Closed firedev closed 9 years ago

firedev commented 9 years ago

Instead of having...

def show
  render
end

...in each cell I suggest to have...

def to_s
  call.html_safe
end

... in the super class.

Profits?

  1. You don't need to have extra method in your cell. Besides what would the test look like?
specify '#show' do
  expect(subject).to receive(:render)
  subject.show
end
  1. You don't have inconsistency between = cell, =cell.call and =cell.(collection: ...) that is mentioned in https://github.com/apotonick/cells/issues/305#issuecomment-144010183
  2. !!!!
apotonick commented 9 years ago

I still don't understand how

def to_s
  call.html_safe
end

will save you from having to define a #show method, because to_s will call call which will call show, so your actual problem is the "missing" show method.

As discussed, this is a design decision I made to avoid confusion for new users who will have trouble understanding that they

  1. can actually override the render process (e.g. with render :another_template)
  2. can add logic to #show (def show; @bla = Whatever.(); end)
  3. can extend the rendering (render(:item) + render + "hello").

You can easily add a show method to your "base class".

Cell::Concept.class_eval do
  def show; render; end
end

Also, the to_s method is already aliased to call, which, in Rails, will call show.html_safe.