trailblazer / cells

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

Filters / Callbacks not working in Cells 4 #311

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi @apotonick!

I've trying to use filters / callbacks in my cells with no success. In my first approach, I just got an "undefined method `before_action'" error, so I first tried including AbstractController::Callbacks and later, your "cells-filters" gem (even though I saw is pretty outdated and probably intended to use with Cells 3 or lower only).

Both solutions make the "undefined method" error dissapear but nothing else. Whether I use "_action" or "_filter" syntax, the result is apparently the same: the callback method is never called.

This is an example of all this:

# test_cell.rb

class TestCell < Cell::ViewModel
  # include Cell::Filters
  include AbstractController::Callbacks

  before_action :add_name
  before_filter :add_name

  def show
    p @name
    p '2222222222222222222222222'
    render
  end

  private

    def add_name
      @name = 'Michael Jackson'
      p '1111111111111111111111111'
    end
end
# show.haml

%p
  Hi! My name is #{@name}

Am I the first one using this on Cells? or what am I doing wrong?

Help please, this is driving me nuts! :(

apotonick commented 9 years ago

This is no longer supported in Cells, because we think that you can also do this with simple method overriding. Soooorry! :beers:

apotonick commented 9 years ago

Oh, and in case you missed that: Cells 4 doesn't use Rails anymore, including AbstractController is not a good idea... :wink:

ghost commented 9 years ago

Hi!

I already know that Cells 4 don't use Rails anymore, but I was still hoping that you kept the good things of the framework... Not all of them are that awful, aren't they? :sweat_smile:

The possibility of using Cells more like the Rails way would be awesome, but I understand that you're focused on abstracting the gem more and more from the framework... Anyway, I'm sure I'm not the only one begging for a "cells-rails" gem!

Thank you anyway! I'll keep prying for it! :sweat_smile: :sweat_smile: :sweat_smile:

apotonick commented 9 years ago

How exactly do you use cells-filters? I don't quite understand how this gem could ever be helpful since controller filters do not really fit into view models anymore. A few examples where this helped you would help me to understand and maybe I can suggest you a better Ruby solution?

A cells-rails gem is redundant, since the railtie in Cells already includes all stuff Rails that we found helpful.

ghost commented 9 years ago

Hi!

The truth is that I'm using Cells in a way that I'm afraid you're not going to like at all. Instead of passing the object or collection to the cell as an argument, I'm retrieving them inside of the cell ifself. I saw in the examples that I should do it the other way round, but I prefer to have this part isolated in the cell rather than having it in all the controllers implicated.

The problem comes when, following this logic, I need to use CanCan inside the cell. Its "load_and_authorize_resource" method, expecting to be in a controller (where this function is available), calls internally "before_filter" in order to verify if you have permission to see the resource of not. I presume that this precise case could be resolved by delegating the method execution to the controller, but I wasn't able to make it work when I tried.

Anyway, with CanCan or not, retrieving model data inside the cell is what's causing the need of using filters in certain cases. Since I'm using Cells in a way different than they were meant to, maybe I should use another gem or whatever. If you have any thoughts, I'm all ears. :relieved:

apotonick commented 9 years ago

Oh, relax, aggregating data in a cell is totally fine - as long as you don't repeat this code anywhere else.

I still don't get how a filter then helps, though. Do you use things like before_filter: :setup_models! ?

ghost commented 9 years ago

Wow, I was afraid of telling you so. I thought I was using your gem in a twisted way, haha. :sweat_smile:

Well, as I told you, the main problem was when I tried to use CanCan inside the cell, as it calls "before_filter" internally. In addition, I suppose I'd need to use filters too if I'd have a cell with more than just the "show" method, maybe 1 or 2 more methods needing the same object data.