Cannot render Rails env variable within cell
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?
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
I tried using :: Rails.env.development?
It also results in NoMethodError: undefined method 'all_subclasses' for #<ActiveSupport::SafeBuffer:0x007f846c266360>
Sorry, I put a space into that by mistake. It should had been:
::Rails.env.development?
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)
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.
https://github.com/trailblazer/cells-haml/issues/4
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
What happens when you call ::Rails::env - this will work, that's pure Ruby and no Cells involved.
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
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.