How can I customize this if statement? - ruby-on-rails

I want to show "not selected" when #user.user_profile.prefecture.name if !#user.user_profile.prefecture.blank? was blank.
How can I customize this code?
controller
#user.profile.prefecture.name if !#user.profile.prefecture.blank?

You could use the ternary operator, which is slightly verbose:
#user.profile.prefecture.blank? ? "Not selected" : #user.profile.prefecture.name
Or, if prefecture is actually nil/not-nil, get rid of the blank?:
#user.profile.prefecture ? #user.profile.prefecture.name : "Not selected"
Finally, you could get slightly more fancy with try and ||, which most proficient Ruby developers will find quite readable:
#user.profile.prefecture.try(:name) || "Not selected"

Like this
if #user.profile.prefecture.blank?
'not selected'
else
#user.profile.prefecture.name
end

Updated:
1- An object is blank if it’s false, empty, or a whitespace string
2- Unless statement code to execute unless condition false
Answer:
"Not selected" unless !#user.profile.prefecture.blank?
Explain: #user.profile.prefecture.blank? will return true everytime that #user.profile.prefecture it’s false, empty, or a whitespace string for what the negation operator transforms that into false so the unless code section can be executed.
This approach it's very 'Ruby' to me and should be pretty easy to understand.

Related

How to find a specific word in string with Ruby/Rails

I got a few string like so:
TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk
AND
TFjyg9780878_867978-DGB097908-78679iuhi698_sky_light_87689uiyhk
AND
TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_dark_87689uiyhk
AND
TFjyg9780878_867978-DGB097908-78679iuhi698_sky_dark_87689uiyhk
I need to check whether the strings above has one of the widesky_light, sky_light, widesky_dark and sky_dark with exactitude so I wrote this:
if my_string.match("widesky_light")
...
end
For each variant, but the problem I'm having is because sky_light and widesky_light are similar, my code is not working properly. I believe the solution to the above would be a regex, but I've spend the afternoon yesterday trying to get it to work without much success.
Any suggestions?
EDIT
A caveat: in this string (as example): TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk, the part after widesky_light, which is _87689uiyhk is optional, meaning that sometimes I have it, sometimes I don't, so a solution would not be able to count on _string_.
Looks like you just need to reorder your if statements
if my_string.match(/widesky_light/)
return 'something'
end
if my_string.match(/sky_light/)
return 'something'
end
Regex
1st regex : extract word for further checking
Here's a regex which only matches the interesting part :
(?<=_)[a-z_]+(?=(?:_|\b))
It means lowercase word with possible underscore inside, between 2 underscores or after 1 underscore and before a word boundary.
If you need some logic depending on the case (widesky, sky, light or dark), you could use this solution.
Here in action.
2nd regex : direct check if one of 4 words is present
If you just want to know if any of the 4 cases is present :
(?<=_)(?:wide)?sky_(?:dark|light)(?=(?:_|\b))
Here in action, with either _something_after or nothing.
Case statement
list = %w(
TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk
TFjyg9780878_867978-DGB097908-78679iuhi698_sky_light_87689uiyhk
TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_dark_87689uiyhk
TFjyg9780878_867978-DGB097908-78679iuhi698_sky_dark_87689uiyhk
TFjyg9780878_867978-DGB097908-78679iuhi698_trash_dark_87689uiyhk
)
list.each do |string|
case string
when /widesky_light/ then puts "widesky light found!"
when /sky_light/ then puts "sky light found!"
when /widesky_dark/ then puts "widesky dark found!"
when /sky_dark/ then puts "sky dark found!"
else puts "Nothing found!"
end
end
In this order, the case statement should be fine. widesky_dark won't match twice, for example.
Maybe something like this:
case my_string
when /_(sky_light)/
# just sky_light
when /sky_light/
# widesky_light
when /_(sky_dark)/
# just sky_dark
when /sky_dark/
# widesky_dark
else
puts "I don't like"
end

Simple if else in rails

How to implement simple if else condition in rails
PHP : echo $params = isset($_POST['some_params']) ? $_POST['some_params'] : "";
RAILS : ??
Thanks,
You may want to look into Ruby Ternary operator: A good source is http://www.tutorialspoint.com/ruby/ruby_operators.htm
params[:some_params].present? ? 'a' : 'b'
another way of doing this is:
if params[:some_params].present?
....
else
....
end
There is one more operator called Ternary Operator. This first
evaluates an expression for a true or false value and then execute one
of the two given statements depending upon the result of the
evaluation. The conditional operator has this syntax:

Translation in .yml with optional parameter

I want to make a translation my_translation with an optional parameter. For example:
> I18n.t('my_translation')
=> "This is my translation"
> I18n.t('my_translation', parameter: 1)
=> "This is my translation with an optional parameter which value is 1"
Is this possible?
Yes, definitely. You just write the translations like this:
my_translation: This is my translation with an optional parameter which value is %{parameter}
Is the parameter really optional? In above translation, you have to provide all parameters.
UPDATE: Sorry, I answered too soon. I don't think it's easy to do. Maybe the easiest way is like this:
> I18n.t('my_translation1')
=> "This is my translation"
> I18n.t('my_translation2', parameter: 1)
=> "This is my translation with an optional parameter which value is 1"
I would say it is possible, though not recommended. You have two completely separate strings, based on your comments in #Yanhao's answer, and I would say they should be two separate entries in your yaml file:
report_name: My report
report_name_with_date: My report on %{date}
Since the existence of the date determines which string to display, you could perhaps test for its existence in in the params hash in a controller method, assign the title to a variable, and then use it in a view. Perhaps something like:
report_date = params[:report_date]
if report_date && report_date.is_a?(Date)
#report_name = I18n.t('report_name_with_date', date: report_date.to_s)
else
#report_name = I18n.t('report_name')
end
If you want behaviour exactly as you have described, you'd need two yaml entries anyway, and you'd have extra convolution, and you'd be doing a I18n no-no by creating a string by concatenating two strings together, which assumes a fixed grammatical sentence structure (not to mention this drives translators up the wall):
report_name_with_date: My report%{on_date}
on_date: on %{date}
with code something like this:
report_date = params[:report_date]
if report_date && report_date.is_a?(Date)
on_date = I18n.t('on_date', date: report_date.to_s)
#report_name = I18n.t('report_name_with_date', on_date: " #{on_date}")
else
#report_name = I18n.t('report_name_with_date', on_date: nil)
end
So, in summary, I'd say go with two separate whole strings, like in the first example.
This is the way i did it!
First set my translation
I18n.t('my_translation', parameter: optional_parameter)
Check if value is nil
optional_parameter = value.nil? "" : "with an optional parameter which value is #{value}"
I18n.t('my_translation', parameter: optional_parameter)
if value is nil =>"This is my translation"
if value is 1 => "This is my translation with an optional parameter which value is 1"
If you're using a number as an optional argument, Rails provides a better way to handle it.
e.g.
invoice:
zero: "Great! You have no pending invoices."
one: "You have only 1 pending invoice."
other: "You have %{count} pending invoices."
>> t("invoice", count: 0)
=> Great! You have no pending invoices.
>> t("invoice", count: 1)
=> You have only 1 pending invoice.
>> t("invoice", count: 5)
=> You have 5 pending invoices.

grails gsp test evaluates to false, but block is still rendered. Why?

I'm baffled with Grails test operator.
This expression:
<g:if test="${!(preferences.displayOption.equals('ANA') || preferences.displayOption.equals('FLOP'))} ">
${!(preferences.displayOption.equals('ANA') || preferences.displayOption.equals('FLOP'))}
</g:if>
prints
false
How can that be? I'm printing the exact same condition I'm testing for!
even though I'm certain the test condition evaluates to 'false' because it prints false in the very next line, the statements inside the g:if are being rendered.
Anu ideas as to what's going on.
I think the problem is the trailing space after the final } in your test. The string "false " evaluates to true, whereas "false" will appropriately interpreted as false by the if tag.
Try removing the extra space after the closing } in the test attribute. You can view the generated groovy source by adding "?showSource=true" to the url in development mode.
The extra space causes it to create a String "false " which evaluates to true.

If else or case statement help!

I could really use some help with the best way to accomplish the following:
I have the below in my controller (and I know this should not be here and needs to move to the model)
This is an email messaging system so according to what position you hold you are able to email out to set groups of people. So if you are Battalion Commander etc you can choose to message out to one of the 5 groups defined below. If you are a Company Commander your groups change. In the view there is a drop down menu and you choose the group your message goes out to. The select menu is populated depending on the position of the signed in user.
The problem seems that the "elsif" portion does not populate the message correctly. It shows the right drop down list and acts as if the email is sent but the emails are not being populated. However the first value (Battalion Commander) works fine.
Do I have something written incorrectly in the if else statement? It seems like it should be pretty simple. The user position always reflects correctly so that is not it.
if (#position == "Battalion Commander" or "Command Sergeant Major" or "FRSA" or "Battalion FRG Leader")
#bnok = (#user.battalion.primaries).collect(&:email).select{|s| !s.blank?}.join(", ")
#bspouses = (#user.battalion.primaries(:conditions => ["relationship = 'spouse'"])).collect(&:email).select{|s| !s.blank?}.join(", ")
#bsoldiers= (#user.battalion.soldiers).collect(&:email).select{|s| !s.blank?}.join(", ")
#bsoldierspouse=((#user.battalion.soldiers)+(#user.battalion.primaries(:conditions => ["relationship = 'spouse'"]))).collect(&:email).select{|s| !s.blank?}.join(", ")
#ballcontacts=((#user.battalion.soldiers)+(#user.battalion.primaries)+(#user.battalion.additionals)).collect(&:email).select{|s| !s.blank?}.join(", ")
elsif
#position == ("Company Commander" or "First Sergeant" or "FRG Leader")
#nok = (#user.company.primaries).collect(&:email).select{|s| !s.blank?}.join(", ")
#spouses = (#user.company.primaries(:conditions => ["relationship = 'spouse'"])).collect(&:email).select{|s| !s.blank?}.join(", ")
#soldiers= (#user.company.soldiers).collect(&:email).select{|s| !s.blank?}.join(", ")
#soldierspouse=((#user.company.soldiers)+(#user.company.primaries(:conditions => ["relationship = 'spouse'"]))).collect(&:email).select{|s| !s.blank?}.join(", ")
#allcontacts=((#user.company.soldiers)+(#user.company.primaries)+(#user.company.additionals)).collect(&:email).select{|s| !s.blank?}.join(", ")
end
So this does not work, it work for either one set of positions or the other but not both. This correlates to a select menu in the view and depending on what position you hold the query on certain groups of people change.
So in the view I have this:
<% if #position == "Battalion Commander" %>
<%= f.select(:bcc_email, [["Please Select", ""], ["Battalion NOK", #bnok], ["Battalion Spouses", #bspouses], ["Battalion Soldiers Only", #bsoldiers], ["Battalion Soldiers & Spouses", #bsoldierspouse], ["All Battalion Contacts", #ballcontacts]]) %></h1><br />
I am still learning rails and I am not sure if a case statement would be better but then I am confused on where that goes and how that case statement fits into the view.
Any guidance would be great, I am trying to chip away at this and figure it out, but I would really appreciate some help.
Firstly it may help if you can format that a bit clearer - for us and yourself, often simple formatting will help identify issues.
Secondly, what is your goal here? I gather it's some sort of war simulator or something? And assume you realise that in an if statement, only one of them will be executed:
if (xxx)
aaaaaaaaaaaaaaaaaa
elseif (yyyy)
bbbbbbbbbbbbbb
end
in the case that xxx is true, the aaaaaaaa will be executed, and then it'll jump to end. The bbbbbbbbb part will not be executed - even if true, because the first statement was. If however xxx is not true, yyyy will be evaluated, and if true, bbbbbbbbbbbbb will happen.
I hope that helps a bit?
disclaimer: I don't know ruby. I have been told it is similar to python and LISP in some regards, so this answer makes that assumption.
What I would do is maintain these condition variables in a dictionary (or map, or hash table, whatever your language calls it). The "rank" of the person would be a key, and the value would correspond to the function you want to execute for that rank. for instance,
#the following example is python-esque. you'll have to port it to ruby.
def some_funct1(): #pass parameters if you need to. i don't, here.
sql_stuff_here
def some_funct2():
sql_stuff_here
map['important dude'] = some_funct1;
map['another important dude'] = some_funct1;
map['unimportant dude'] = some_funct2;
map['another_unimportant dude'] = some_funct2;
#after some time, you have a person whose rank is rank.
map[rank]() #this will execute the function
You wrote
if (... a big test ...)
..do some work..
elsif
..do something else..
end
but that is wrong! You should just write else instead of elsif unless you need to check another condition.
[I assume elsif followed by nothing=nil is always false]

Resources