Can't use cells from an engine inside Rails application
Asked Answered
G

1

8

I created an engine which provides an ui component as a cell. The corresponding gem (criteria_operator-ui_component) contains nearly no code inside the lib folder, because for cells to function properly I had to work inside the assets path. The base file of the gem looks like this:

require 'criteria_operator/ui_component/engine'
require 'cells/rails'

module CriteriaOperator
  module UiComponent
    # Your code goes here...
  end
end

The engine doesn't contain much, either:

module CriteriaOperator
  module UiComponent
    class Engine < ::Rails::Engine
      require 'jquery-rails'
      require 'criteria_operator'
      isolate_namespace CriteriaOperator::UiComponent
    end
  end
end

To me, it looks like the gem couldn't even know about the cell, but as far as I know I'm not allowed to include anything from outside the lib folder. Also, testing the cell in the dummy application within the project is working fine.

Now I'm using this engine inside a real Rails application. In the gemfile, I included the following:

gem 'criteria_operator' 
gem 'cells'       # i added these three, because `bundler list` didn't show me
gem 'cells-rails' # `cells-rails` and `cells-erb` even though they are listed
gem 'cells-erb'   # as dependencies for the engine
gem 'criteria_operator-ui_component'

I mounted the routes

mount CriteriaOperator::UiComponent::Engine => '/criteria_operator-ui_component'

and tried using the cell CriteriaOperator::UiComponent::CriteriaEditor like I did in the dummy application. Inside erb:

cell('criteria_operator/ui_component/criteria_editor', @op)

or from code:

include Cell::RailsExtensions::ActionController    

def whatever
  cell(CriteriaOperator::UiComponent::CriteriaEditor, @op).call()
end

The error is ActionView::Template::Error (uninitialized constant CriteriaOperator::UiComponent::CriteriaEditor).

What am I doing wrong? Am I just missing something when using the engine, or is the engine itself implemented the wrong way? And if that's the case, why does the dummy application work? I'm totally stuck, this is my first time creating a Rails Engine as well as my fist time working with cells...

The full code of the engine (including the dummy application) can be found on GitHub (this isn't supposed to be any advertisement, it's just in case anyone needs additional information).

Guyer answered 7/7, 2017 at 8:29 Comment(0)
D
0

You're calling CriteriaOperator::UiComponent::CriteriaEditor but that class/module does not seem to exist.

CriteriaOperator::UiComponent::Engine works OK because it's defined in the engine itself.

I'm guessing that your sample application works because it's using the view-based invocation like cell('criteria_operator/ui_component/criteria_editor') which presumably works with the javascript? You can't use the "code" version without defining the cell as a class like this: https://github.com/trailblazer/cells#cell-class

Dynamotor answered 21/8, 2018 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.