Include? condition in haml view - ruby-on-rails-3

I try to make something like this in haml view :
%li{:class => #taxon and ([#taxon] + #taxon.ancestors).include?(taxon) : "current"}
what the correct syntax ?

I'm guessing the value of #taxon is the class name? If so this should work for you.
- taxon_class = (#taxon && ([#taxon] + #taxon.ancestors).include?(taxon)) ? #taxon : "current"
%li{:class => taxon_class}
I always find it easier to do the ruby logic outside of the haml {} brackets.

Related

How to chain multiple 'And' when checking exception with FluentAssertions

I have a unit test that validates that some code throws an exception and that two properties have the expected value. Here is how I do it:
var exception = target.Invoking(t => t.CallSomethingThatThrows())
.ShouldThrow<WebServiceException>()
.And;
exception.StatusCode.Should().Be(400);
exception.ErrorMessage.Should().Be("Bla bla...");
I don't like the look of the assertion that must be done in three statements. Is there an elegant way to do it in a single statement? My first intuition was to use something like this:
target.Invoking(t => t.CallSomethingThatThrows())
.ShouldThrow<WebServiceException>()
.And.StatusCode.Should().Be(400)
.And.ErrorMessage.Should().Be("Bla bla...");
Unfortunately, this doesn't compile.
As said here:
target.Invoking(t => t.CallSomethingThatThrows())
.ShouldThrow<WebServiceException>()
.Where(e => e.StatusCode == 400)
.Where(e => e.ErrorMessage == "Bla bla...");
Not really a direct answer but I note that, if you only have one property of the exception to check, you can use a more fluid syntax like this:
target.Invoking(t => t.CallSomethingThatThrows())
.ShouldThrow<WebServiceException>()
.Which.StatusCode.Should().Be(400);

Rails - Simplify WHERE condition IS NOT NULL

Working on a webpage I used the next line:
Model.select(:column).where("column IS NOT NULL")
I was wondering if there was a more Rails-ish way to do this, like using a hash for example
Model.select(:column).where(column: !nil)
The Squeel gem will allow you to use != nil type syntax, but natively rails will not.
Example: Model.where{column != nil}
I would prefer to use a scope as its more readable as well as its more manageable later (like merging with other scopes)
class Model < ActiveRecord::Base
scope :not_null, lambda { |column|
{:select => column,
:conditions => "#{column} NOT NULL"
}
}
end
then use
Model.not_null("column_name")
In the Rails 4 you can do this:
Model.select(:column).where.not(column: nil)

Ternary operator not working in Haml

This question has been asked but the answers have not worked. The problem I am having is this hamlc code:
.UI_feed_item.deletable.clearfix{ :class => #feed.fav_post ? 'favorited' : '', feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
*a lot more haml that doesn't have to do with this question*
the indentation is correct - it shows up weird on here
I want an extra class added to say favorited if feed.fav_post is true. for some reason it added a class 'true' or 'false' instead. I have also tried this:
.UI_feed_item.deletable.clearfix{ :class => (#feed.fav_post ? 'favorited' : ''), feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
same result
I cannot do an if/else thing because there is no end in haml and I would have to rewrite a hundred lines of indented code. please help! none of the other solutions on the web have worked
Your second shot should work fine. The first variant returns true/false because the hash rocket wins over the ternary operator in terms of precedence - but shouldn't it break on syntax error after that?
You can do if-else in haml.
- if true
some stuff here
- else
some other stuff here
Indentation is used instead of end.
HAMLC doesn't support the ?/: ternary operators, but you can still achieve what you want using inline if/then/else. Try this:
.UI_feed_item.deletable.clearfix{ :class => "#{ if #feed.fav_post then 'favorited' else '' }", feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }

scaml interpolation in html attributes

I have something like this:
-# var id:String
%div{:dojoType => 'dojo.data.ItemFileReadStore', :jsType => 'store', :url => "/path/to/resource?id=#{id}"}
I was hoping variable interpolation would work here, but it just puts #{id} into the html. I also tried:
%div{:url => 'path/to/resource?id='+id}
And that doesn't even compile. What is the right way to do this?
The correct syntax is:
%div{:url => {"/path/to/resource?id="+id}}

Giving 'TemplateError' can't convert String into Integer

I recently transfered my app from Rails2 to Rails3.
The code in 'app/views/distribution/index.html.erb' is like :-
<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][#artist.id.to_s].empty? && !session[:album][#artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled">
<%= link_to 'Make Payments',{:action => 'pay', :album=>#album.id}, :class => "button" %>
</div>
It's giving me TemplateError on line :-
<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][#artist.id.to_s].empty? && !session[:album][#artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled">
How to resolve the problem ?
Solution 1: In the ERB tag, try putting spaces around the 'or' question mark, i.e. ....nil?) ? 'block....
Solution Better: Do step one, then put that code in a helper. Will really help to clean up your views.
UPDATE:
A few other tips: you will want to switch the order of the conditions, because you will want to see if the value is nil before checking if it's an empty string.
Calling obj.blank? is the equivalent of calling obj.nil? && obj.empty?, so that could make the code a bit shorter. Even better, obj.present? is the same as !obj.blank?.
Therefore, that line could be simplified to:
session[:album][#artist.id.to_s].present? ? 'block' : 'none'
Happy Rails-ing!