Laravel 5.1 blade, compare a var with many values - laravel

I want to compare a var with many values, like this:
{{(
((Request::segment(1) == 'A' || Request::segment(1) == 'B' || Request::segment(1) == 'ETC' || ... ) && $menu->link == 'XXX') ? 'visible' : ''
)}}
Is there a way to make the comparission with something like this?
{{(
((Request::segment(1) == my_values(A,B,C,E,...,ETC) && $menu->link == 'XXX') ? 'visible' : ''
)}}
I can't edit the values from Controller

What about using the PHP in_array as explained here ?
{{(
((in_array(Request::segment(1), ['A','B','C','E',...,'ETC']) && $menu->link == 'XXX') ? 'visible' : ''
)}}

you can use contains() function of laravel collection.
{{(
((collect(my_values(A,B,C,E,...,ETC))->contains(Request::segment(1)) && $menu->link == 'XXX') ? 'visible' : ''
)}}

Or you can use in_array method in PHP.
{{(
((in_array(Request::segment(1), ['A', 'B', 'C', 'ETC']) && $menu->link == 'XXX') ? 'visible' : ''
)) }}

Related

how can we retrieve two variables in one line if else

("string" != "string") ? (article = '-' , contact_info = '-') : (article = '' , contact_info = '')
How can I update this condition then it will return two variables now its output is an array like ["-" , "-"]
I believe this should work:
article, contact_info = ("string" != "string") ? ['-', '-'] : ['' , '']
In ruby you can assign multiple variables from an array using:
Multiple Assignement
Just return arrays?
("string" != "string") ? ['-', '-'] : ['' , '']

Ruby if else statement refactoring

The current code works:
def launched_city(country, city, city_link)
return 'current' if country == 'Malaysia' && ('Kuala Lumpur' == city_link)
return 'current' if country == 'Philippines' && ('Manila' == city_link)
if country == 'Australia'
return 'current' if city == 'Melbourne' && ('Melbourne' == city_link)
return 'current' if city == 'Sydney' && ('Sydney' == city_link)
return 'current' if city == 'Perth' && ('Perth' == city_link)
end
nil
end
but I think it's ugly. Any help?
I tried with case block. It failed with the case statement because I need to check the second statement. I also tried with if elsif else block. It's the same in this case.
COUNTRY_LINKS = { 'Malaysia'=>['Kuala Lumpur'],
'Philippines'=>['Manila'],
'Australia'=>['Melbourne', 'Sydney', 'Perth'] }
def launched_city(country, city, city_link)
if COUNTRY_LINKS.has_key?(country) && COUNTRY_LINKS[country].include? city_link) &&
(country != 'Australia' || city == city_link)
'current'
end
end

Write a function to return "true" or "false" if passed "1"/"0" or "true"/"false"

I need a simple function to return "true" or "false" the argument passed to it is:
1 or 0, or true or false
I currently had something like this, so the answer, if possible, should be concise as per below:
def boolean(value); return value ? ( value == 1 ? "true" : "false) : nil; end
Thanks.
Some ideas:
def boolean(x)
%w{1 true}.include?(x).to_s
end
def boolean(x)
(x == '1' || x == 'true').to_s
end
There's also the wannabe bool gem:
require 'wannabe_bool'
'1'.to_b # => true
'0'.to_b # => false
'true'.to_b # => true
'false'.to_b # => false
You might want to have a look how Rails does this typecasting in its database connection adapter:
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
# convert something to a boolean
def value_to_boolean(value)
if value.is_a?(String) && value.empty?
nil
else
TRUE_VALUES.include?(value)
end
end
See: docs for ActiveRecord::ConnectionAdapters::Column
I think this'll work, comments are welcome though:
def b(k); return k ? ( (k == "1" || k == "true") ? "true" : "false" ) : nil; end
puts b("1")
puts b("0")
puts b("true")
puts b("false")
Result:
true
false
true
false

Multiple results in ? style if clause

I have the following code: i ? "x" : "y" But instead of only returning either "x" or "y" I also want to set i either true or false. i ? ("x"; i = false) : ("y"; i = true) however does not work.
(i ? "x" : "y").tap{i = !i}
or
(i = !i) ? "y" : "x"
But if this turns out to be an XY-situation (I don't write "XY-question" here because the OP has not asked any question), then this might be more elegant:
letter = ["x", "y"].cycle
letter.next #=> "x"
letter.next #=> "y"
letter.next #=> "x"
letter.next #=> "y"
...
If you want to return 'x' or y', then 'x' or 'y' needs to be the last statement:
i ? (i = false; 'x') : (i = true; 'y')
If you think of it like this, maybe it would make more sense:
if i
i = false
'x'
else
i = true
'y'
end
Keep in mind that setters in Ruby (and many other languages) return the value being set. For example, i = false returns false.
You should use ternary operator only in the simplest cases because readability and side-effect issues.
Ruby Code Style
But if you really want it you could do something like this:
!(i = !i) ? 'x' : 'y'

Rails 3.2 CRUD : .where with 'or' conditional

With ruby on rails, I want to do something like:
#tasks = Task.where(:def => true || :house_id => current_user.house_id)
What is the most efficient/clean way to do this?
You can do it like this:
Task.where("def = ? or house_id = ?", true, current_user.house_id)
The general case is:
Model.where("column = ? or other_column = ?", value, other_value)
You can also leverage Arel:
t = Task.arel_table
#tasks = Task.where(
t[:def].eq(true).
or(t[:house_id].eq(current_user.house_id))
)

Resources