Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot render Rails env variable within cell #407

Open
chrisyeung1121 opened this issue May 26, 2016 · 10 comments
Open

Cannot render Rails env variable within cell #407

chrisyeung1121 opened this issue May 26, 2016 · 10 comments

Comments

@chrisyeung1121
Copy link

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?

@samstickland
Copy link

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
#407

@chrisyeung1121
Copy link
Author

chrisyeung1121 commented May 26, 2016

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

@samstickland
Copy link

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

::Rails.env.development?

@chrisyeung1121
Copy link
Author

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)

@samstickland
Copy link

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
Copy link

trailblazer/cells-haml#4

@chrisyeung1121
Copy link
Author

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

@apotonick
Copy link
Member

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

@chrisyeung1121
Copy link
Author

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

@apotonick
Copy link
Member

Of course, because this is wrong! 😜

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants