Coder Social home page Coder Social logo

Comments (14)

grimen avatar grimen commented on July 30, 2024

Note: In the cell view I need to call current_account.

from cells.

grimen avatar grimen commented on July 30, 2024

Neither this solves the problem: render :locals => {:current_account => current_account}

from cells.

apotonick avatar apotonick commented on July 30, 2024

hey grimen,
it's important to understand that controller != cell. if you define a helper_method in a controller, it is only available in the controller's views, not in the cell views- otherwise we would break proper encapsulation.

when using #helper_method in a cell this method must also exist there. note that you could also use #helper to include files in a cell.

from cells.

grimen avatar grimen commented on July 30, 2024

Right, but a cell is rather a "child" of a controller as a controller render a view that render one or many cells. It's actually also a bit of a problem that cells dont' support controller helpers, as all? authentication solutions defines helpers in controller. This code-duplication make cells bittersweet I must say. I would suggest looking on controller helpers as a "parent".

from cells.

apotonick avatar apotonick commented on July 30, 2024

not sure what you mean with "cells don't support controller helpers"...? are you sure you tried #helper in a cell? that should work and is tested thoroughly. any helper that works in a controller should work in a cell view as well.
did you experience problems here?

i speak against some magic helper inclusion where cells would "inherit" helpers from controllers- cells should never know anything about there parent controller to assure clean encapsulation.
imagine such a cell being plugged into another controller B but relying on a mysterious helper method from controller A. this would cause a name error which would be hard to trace, apart from evil dependencies. what do you think, grimen?

from cells.

grimen avatar grimen commented on July 30, 2024

Yes, but helper won't work because I got loads of helpers that belongs to - and is defined in - controllers, but is used as helpers as well with "helper_method :my_method_1, :my_method_2, ... A pattern used in most plugins I'm aware of, e.g. Devise, Facebooker, etc. As I see it, even if Cells is excellent as a philosophy and plugin; Cells needs to support these globally available controller helpers somehow, otherwise it'll be more pain than gain to use Cells. Btw, helper_methods are usually declared for ActionController::Base so if these helpers could be included somehow.

from cells.

grimen avatar grimen commented on July 30, 2024

Update:

Here's how helper_method works under the hood:

http://www.neeraj.name/blog/articles/718-how-helper_method-brings-methods-to-view-level

I tried to access them using ActionController::Base.helpers.{method}, but for some reason this don't work either. Will continue see if I can solve this...

from cells.

apotonick avatar apotonick commented on July 30, 2024

hi grimen!
thanks for the link.
go and try the following:

class MyCell < Cell::Base
helper WhatEver
helper_method :method_from_whatever

that should really work (has been doing the job for 4 years now ;-)
does that help?
cheers! nick

from cells.

grimen avatar grimen commented on July 30, 2024

I tried that already, but as already described above many helpers are not defined in helpers, rather in controller but added to the master template as helpers (helper modules). If you look into the the article I posted above you see that your code suggestions cannot work for helpers defined in controllers. ActionController::Base.helpers.{method} should work, but there seems to be a Rails bug which I filed in the Rails issue tracker. Anyway, that bug is not the reason for why these helpers are not available in cells.

from cells.

grimen avatar grimen commented on July 30, 2024

Example of controller helpers:

http://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb

from cells.

apotonick avatar apotonick commented on July 30, 2024

grimen! thanks for your post, i start to grasp what you mean :-)
the problem is that those plugins are for controllers only- no cells in mind! the simplest solution i see would be (in your example)

cell MyCell < Cell::Base
  include Devise::Controllers::Helpers

i really don't see any way to automatically distinguish what in a controller is a helper and what not, so we cannot provide a generic mechanism for that in cells. you would have to go by foot here. and plugin developers would have to mix in their code in ActionController::Base and in Cell::Base :-(

from cells.

grimen avatar grimen commented on July 30, 2024

That was just a sample, so won't change much I'm afraid. OK, well that won't happen (other developers to mixin stuf finto Cell::Base) I'm sure unless Cells gets merged into Rails. Thanks for the follow ups though. I'll let u know if I find a best practice on my own to get around it.

from cells.

apotonick avatar apotonick commented on July 30, 2024

maybe there IS a way to handle that! we could extract all helper methods from the controller's MasterHelperModule and mix those methods in the cell's view.

however, i wouldn't do that by default- rather you would have to turn on that evil thing explicitly, like
render_cell :grimen_cell, :shout, :mixin_controller_helpers => true

from cells.

grimen avatar grimen commented on July 30, 2024

My solution/hack that works (but discussable why):

app/helpers/cells_hacks_helper.rb:

module CellsHacksHelper
  def method_missing(method, *args)
    controller.send(method, *args) rescue super(method, *args)
  end
end

app/cells/some_cell.rb:

class SomeCell < Cell::Base
  helper :cells_hacks
  # ...
end

from cells.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.