Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builders don't cascade #127

Open
the8472 opened this issue Jun 25, 2012 · 5 comments
Open

Builders don't cascade #127

the8472 opened this issue Jun 25, 2012 · 5 comments

Comments

@the8472
Copy link

the8472 commented Jun 25, 2012

class TestCell < Cell::Rails
  build do
    SubCell
  end

  def my_action
     render :text => "foo"
  end
end

class SubCell < Cell::Rails
  build do
    SubSubCell
  end
end

class SubSubCell < Cell::Rails
  def my_action
     render :text => "bar"
  end
end

render_cell :test, :my_action should return "bar"

@apotonick
Copy link
Member

That is a valid point! Should the found class be queried again for buliders, making the finding recursive... Hm. Shouldn't be required in most cases but definitely makes sense. Can you give an example how you're using that?

@the8472
Copy link
Author

the8472 commented Jun 26, 2012

I basically have items that need specific behavior, so - in a gem - I just pass them into a basic cell that delegates down to some default behavior for the specific class of the item. If the gem is included in an application the application itself can override the behavior simply by having the builders of the default behavior locating the cell based on conventions specific to the item's class.

But it's just an observation of what would seem intuitive. In my case other requirements forced me to initialize the cells in the parent controller anyway and then pass them into the view instead of using render_cell, so that problem doesn't apply anymore.

@apotonick
Copy link
Member

Why does Github say you posted this in 2012?

@thedanielhanke
Copy link

Wrong Timezone :D ( sorry, just had to laught about that comment :) )

@Startouf
Copy link

Hey I was actually expecting this cascade behavior to be available already and I'm quite missing it for my application.

For big inheritance scheme, it made more sense to me to split the builder into multiple builders instead of one giant builder at the root.

In some case it might event make sense to have recursive builders cause it would let us select better "cell routing". Here's an example

class StatusCell < DefaultCell
  builds do |model|
    model.vip? ? VIPStatusCell::Cell : NormalStatusCell
  end
end

class VIPStatusCell < StatusCell
  builds do |model|
    case model.status
      when :hungry; VIPStatusCell::Hungry # A cell with lots of tasty and expensive food
      when :sleepy, :sleeping; VIPStatusCell::Sleepy # A cell with a very confortable bed
    end
  end
end
class VIPStatusCell::Hungry < VIPStatusCell; ...; end
class VIPStatusCell::Sleepy < VIPStatusCell; ...; end

class NormalStatusCell < StatusCell
  build do |model|
    case model.status
      when :hungry; NormalStatusCell::Hungry    # A cell with affordable food
      when :sleepy, :sleeping; SleepyStatusCell::Sleepy # A cell with a simple bed
  end
end
class NormalStatusCell::Hungry < NormalStatusCell; ...; end
class NormalStatusCell::Sleepy < NormalStatusCell; ...; end

With recursive builders there would be a difference between calling :normal_status_cell and status_cell in the following example

# Suppose the current_user might be a VIP and might want to travel incognito

# incognito_travel.html.erb
<%= cell(:normal_status_cell, current_user) %>

# normal_travel.html.erb
<%= cell(:status_cell, current_user %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants