1

I might be approaching this all wrong (i'm fairly new to Ruby), but can someone tell me how to check for certain values in an Object?

I want to check if any of the taxons in product.taxons contains any of 'rush_delivery' or 'customization_possible' in their machine_names.

My template code:

<div class="expanded">
    <% product.taxons.each do |t| %>
      <% if t.machine_name == 'rush_delivery' %>
        <p class="icon icon-urgent">
          <span class="tooltip"><%= Spree.t('usp.rush_delivery') %></span>
        </p>
      <% end %>
      <% if t.machine_name == 'customization_possible' %>
        <p class="icon icon-customize">
          <span class="tooltip"><%= Spree.t('usp.customization_possible') %></span>
        </p>
      <% end %>
    <% end %>
  </div>

The goal is to add a hide class on the parent div if they do not exist.

6
  • The best way to write the helper which will have some condition(will verify you case t.machine_name == 'your_value' && t.machine_name == 'another_value') and return the content_tag with some data attribute which will have display: none or visible. Commented Feb 25, 2015 at 10:24
  • I guess that's a good idea, but what exactly would you check in your helper? I understand the '&&' statement. Do i have to loop through the object again? Commented Feb 25, 2015 at 10:31
  • Do you want to check if all the taxons of product.taxons contains any of 'rush_delivery' or 'customization_possible' in their machine_names ? Commented Feb 25, 2015 at 10:34
  • Hi limekin, Yes that is correct. Thanks for pronouncing it right ;) Commented Feb 25, 2015 at 10:38
  • Np. It wasn't clear in the question. I think you can make that part clearer. Commented Feb 25, 2015 at 10:39

1 Answer 1

1

So your conditions would look like this:

product.taxons.any? { |taxon| 
  ['rush_delivery', 'customization_possible'].include?(taxon.machine_name)
}

I would argue that this kind of logic doesn't belong into a view. There a different ways to clean that up, it depends on your needs which way to choose: You could only load products with a matching machine_way in your controller. You may want to use a presenter object that encapsulate the logic or just more it into a helper.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi spickerman, Thanks for you response! This seems to be the statement i was looking for. I agree its not best to place it in the template, so i will try to move this to the controller. But aside from that, i'm glad to've discovered this syntax.
Might want to declare the array outside the block to prevent creating it repeatedly.
@Humza: Sure thing. As I wrote, it makes sense to extract that logic into something - probably a presenter. In that case it is worth it to declare a for example constant that holds the list of special machine names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.