cells icon indicating copy to clipboard operation
cells copied to clipboard

Cannot render Rails env variable within cell

Open chrisyeung1121 opened this issue 9 years ago • 10 comments

I have a following cell class


class PromotionRuleCell < Cell::ViewModel

  def selector
    render
  end

  def all_subclasses
    Rails.env.development? ? [
        Promotion::Rules::RentTime,
        Promotion::Rules::RequestTime,
        Promotion::Rules::RentDuration,
        Promotion::Rules::TargetCar
        ] : PromotionRule.subclasses
  end

end

I basically have to access Rails.env variable but since Rails has become a module under Cell::Slim::Rails:Module I can't access Rails.env now. Is there any way to work around this?

chrisyeung1121 avatar May 26 '16 15:05 chrisyeung1121

Use :: Rails.env.development?

2016-05-26 16:00 GMT+01:00 chrisyeung1121 [email protected]:

I have a following cell class

class PromotionRuleCell < Cell::ViewModel

def selector render end

def all_subclasses Rails.env.development? ? [ Promotion::Rules::RentTime, Promotion::Rules::RequestTime, Promotion::Rules::RentDuration, Promotion::Rules::TargetCar ] : PromotionRule.subclasses end end

I basically have to access Rails.env variable but since Rails has become a module under Cell::Slim::Rails:Module I can't access Rails.env now. Is there any way to work around this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/apotonick/cells/issues/407

samstickland avatar May 26 '16 15:05 samstickland

I tried using :: Rails.env.development? It also results in NoMethodError: undefined method 'all_subclasses' for #<ActiveSupport::SafeBuffer:0x007f846c266360>

chrisyeung1121 avatar May 26 '16 15:05 chrisyeung1121

Sorry, I put a space into that by mistake. It should had been:

::Rails.env.development?

samstickland avatar May 26 '16 15:05 samstickland

I tried that as well. It all result in the same error :(

On May 26 2016, at 11:37 pm, Sam Stickland <[email protected]> wrote:

Sorry, I put a space into that by mistake. It should had been:

::Rails.env.development?


You are receiving this because you authored the thread.
Reply to this email directly or [view it on GitHub](https://github.com/apotoni ck/cells/issues/407#issuecomment-221908188)![](https://github.com/notification s/beacon/ADU7R4oH5f2zzkf5VMtaqnF5XWan-Gk6ks5qFb5AgaJpZM4Inogy.gif)

chrisyeung1121 avatar May 26 '16 15:05 chrisyeung1121

Wait, I didn't read your error message properly before.. Where are you calling all_subclasses from?

The ::Rails syntax is definitely correct, I'm using that in my own project.

samstickland avatar May 26 '16 16:05 samstickland

https://github.com/trailblazer/cells-haml/issues/4

samstickland avatar May 26 '16 16:05 samstickland

I am calling it from spec/cell/. Is it not included in test ?

Sam Stickland [email protected] 於 2016年5月27日 上午12:19 寫道:

Wait, I didn't read your error message properly before.. Where are you calling all_subclasses from?

The ::Rails syntax is definitely correct, I'm using that in my own project.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub

chrisyeung1121 avatar May 27 '16 03:05 chrisyeung1121

What happens when you call ::Rails::env - this will work, that's pure Ruby and no Cells involved.

apotonick avatar May 27 '16 07:05 apotonick

If I just run ::Rails::env in console it works fine. If I run it from bundle exec spec It just gives me the same error.


Failures:

  1) PromotionRuleCell
     Failure/Error: it { expect(cell(:promotion_rule).(:selector).all_subclasses).to eq(4) }

     NoMethodError:
       undefined method `all_subclasses' for #<ActiveSupport::SafeBuffer:0x007fc63be6f8a8>
     # ./spec/cells/promotion_rule_cell_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.3323 seconds (files took 6.01 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/cells/promotion_rule_cell_spec.rb:6 # PromotionRuleCell

This is my spec file if it helps

require 'rails_helper'

RSpec.describe PromotionRuleCell, type: :cell do
    subject { cell(:promotion_rule).(:selector) }

    it { expect(cell(:promotion_rule).(:selector).all_subclasses).to eq(4) }
end

chrisyeung1121 avatar May 27 '16 08:05 chrisyeung1121

Of course, because this is wrong! :stuck_out_tongue_winking_eye:

cell(:promotion_rule).(:selector) will return a Capybara-assertable string. cell(:promotion_rule) returns the cell instance. That's what you want.

apotonick avatar May 27 '16 08:05 apotonick