Reputation: 423
In other programming languages, I can separate into lines the conditions by breaking it down in the logical operator, but in ruby (I'm using rails) it seems that not. Is a thing of the code or about the syntax?:
<% if (current_user && current_user.id == @user.id)
|| @user.profile_privacy === 0
|| (@user.profile_privacy === 1
&& current_user
&& current_user.user_friendships.find_by_friend_id(@user.id)
&& current_user.user_friendships.find_by_friend_id(@user.id).state == true ) %>
Upvotes: 2
Views: 588
Reputation: 5378
You should try and keep as much logic out of your template as possible.
For example, you could move some of the logic into your User
model:
class User < ActiveRecord::Base
def has_user_as_friend user
user_friendships.find_by_friend_id(user).state == true
end
def is_private
profile_privacy === 0
end
end
And you could move some of it into your controller:
def show
@user = User.find(params[:id])
@is_current_user = current_user.id == @user.id
end
Then, your condition may become a little simpler:
<% if (@is_current_user || @user.is_private) %>
I haven't replicated your conditions exactly, but hopefully this gives you a couple of ideas as to how you could simplify your if
statement.
Upvotes: 2
Reputation: 24367
You shouldn't have all that logic in your view, but to fix your code move the operators to the end of the lines:
<% if (current_user && current_user.id == @user.id) ||
@user.profile_privacy === 0 ||
(@user.profile_privacy === 1 &&
current_user &&
current_user.user_friendships.find_by_friend_id(@user.id) &&
current_user.user_friendships.find_by_friend_id(@user.id).state == true )
%>
Upvotes: 3