views:

43

answers:

2

I need to write a method that returns whether or not a Boolean is set to true/false.

In my case the boolean is an attribute for a product. And to skip a step in the checkout process I need to have this self method work similar to the following:

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.user
end

this example is from the application_controller, and is doing the same thing in the checkout process that I need to do here.

The use for this once I have it working is:

def checkout_steps                                                      
  checkout_steps = %w{registration billing shipping shipping_method payment confirmation}
  checkout_steps.delete "registration" if current_user
  checkout_steps
end

I need a boolean that works the same as the delete item above. I am trying to learn how this works so any explanation is greatly appreciated to.

thoughts?

A: 

Try this:

def current_user
  current_user_session.user if current_user_session
end

def signed_in?
  current_user_session && !current_user_session.user.nil?
end

Then I'd rewrite your check_steps to be:

checkout_steps.delete "registration" if signed_in?

I hope this helps!

jonnii
thanks for the info, that would totally help if that part was broken but it works fine. I am trying to create a similar method that checks on the product model for a boolean "promotion" if promotion is true that it will end up skipping a shipping step.thanks man
TJ Sherrill
A: 

I'm not quite sure I follow your question, but if you're asking how this line works:

checkout_steps.delete "registration" if current_user

It's not the way current_user returns its value that's doing it. It's a built-in syntax of Ruby where you can specify one line if statements in the following form:

<statment> if <condition> # only execute <statement> if <condition> is true

instead of the more traditional way:

if <condition> then
  <statement>
end

As will the full if/end syntax, Ruby will only evaluate the statement if the condition is true, and the condition can be any code you might normally place in an if condition.

In your case, if the product's attribute is something like can_be_shipped, indicating whether the item can be shipped you can simply do

checkout_steps.delete 'shipping' if [email protected]_be_shipped

Rails supports another syntax for testing whether the condition evaluates to false:

<statment> unless <condition> # only execute <statement> if <condition> is false

Which could be used to make the logic clearer:

checkout_steps.delete 'shipping' unless @product.can_be_shipped
meagar
Even more readable would be `checkout_steps.delete 'shipping' unless @product.can_be_shipped`.
John Topley
I think you guys are on the right track, and thanks so much for your help. Been working on this issue for to long (time to stop being a newb). In this case i have a promotions attribute on the Product. If the Promotions boolean is checked I don't want to charge shipping, thus removing the shipping step. doing what you wrote; checkout_steps.delete "shipping_method" if [email protected] returns a no method error on promotion. This is the reason I thought it would be good to pull from a application_controller method so that it would be available no matter where. any other thoughts?
TJ Sherrill
Did you mean `if [email protected]`, plural?
meagar
no the plural isn't it. I am in the checkouts_helper.rb. I get a no method error on anything following @product. That would lead me to believe that @product is fine but it doesn't have access to the attributes for some reason? not sure here, any other thoughts?
TJ Sherrill
@TJ Are you sure that @product is actually defined? And that it's set to an instance of Product and not an array of products? Use `logger.info(@product)` to get some more information about it.
meagar