Rails application helper return if false - ruby-on-rails

I'm writing a helper method to determine if current has any pending reviews to write. If there is a pending review, simply print a line in the view.
My helper is putting exactly the right stuff to the console, however I'm struggling with how to simply return it. In this scenario, current user has an id: 4.
My Code:
def review_pending
gallery = current_user.galleries.first
if gallery
if gallery.permissions.accepted
gallery.permissions.accepted.each do |p|
return true if p.user.reviews.find_by_reviewer_id(!current_user)
puts "already written review: #{p.user.reviews.find_by_reviewer_id(4)} - prints correctly"
end
end
end
end
My goal: if there is a user from the list that current user has not yet reviewed return true.
Thanks!!!

Thanks for all your pointers!
I had forgotten/learned 2 things to make it work:
First, if nil is returned, ruby returns the last returned value which in my case was true (if gallery.permissions.accepted).
Secondly, I placed the "!" before current_user, and should have placed it before the entire line.
Corrected Code:
def review_pending
gallery = current_user.galleries.first
if gallery
gallery.permissions.accepted.each do |p|
return !p.user.reviews.find_by_reviewer_id(current_user.id)
end
end
return false
end

Related

How to not cache if certain condition is met on Rails 6.1 +Ruby

I want to make it so if its directReports it shouldn't get it from rails cache. I tried to just return it but that doesnt seem to work.
''' # dont get it from cache if DirectReports
when PerformanceManagement::V0::EmployeeFilter::DirectReports
return
else
filtered_results = loaded_records
end'''
plz don't be mean it's my first time posting.
For such things you can create a method that just acts as a wrapper to handle the cache-or-not condition:
def main_logic_here
... expensive calculation
end
def main_logic_wrapper
if true
main_logic_here
else
Rails.cache.fetch(...) { main_logic_here }
end
end

What is wrong in this ruby on rails method?

My syntax error is solved, but now I've got another problem.
I have a model User and Post. Also i've created the additional model which is called Like(for likes/dislikes system). So, i wrote a method to check if the copy of the model Like has the given 'post_id' and 'user_id' simulteniously, then the given Post was liked by the given User. Here's the code of my method
def licked(p,u)#p - post, u - user
if Like.all.empty? then return false
else
if (Like.where(post_id: p.id).empty?)
return false
else
posts=Like.where(post_id: p.id)
if ((posts.length<2) && (posts[0].user_id==u.id) && (posts[0].action!=nil))==true then return true
else
if ((posts.length<2) && (posts[0].user_id!=u.id)&& (posts[0].action!=nil)) then return false
else posts.each do |i|
if (i.user_id==u.id&& (posts[0].action!=nil)) then return true
end
end
end
end
end
end
end
Sorry, if there are many if's, i did not find any other way to relize it, and anyway it's not working. So i'm asking for your help: either to hange the existing code or making it other way, thanks.
You don't need ANY of that logic.
def liked(post, user)
Like.where(user_id: user.id, post_id: post.id).where.not(action: nil).exists?
end

Return all user profile except current users'

I am iterating over all user profiles to get their country. I wish not to include the current user's profile. If I have a total of 10 users, I want only 9 user's country:
# helper function
def method_name
Profile.all.select do |m|
n = m.country.class == String # Because most countries will be nil. A shorter way to not include nil profiles?
return n.reject! {|x| x == current_user.profile.user_id }
end
end
The method above should return all user's profile except for the current user.
The error I got is
undefined method `reject!' for false:FalseClass
The reason? In my views I could:
<%= method_name.map do |p| p.country end %>
Beware of using Profile.all here, because you will potentially use a lot of memory instantiating a profile object for every row in your database.
I think the best way to do this is to create a method within your Profile model, which performs the query, and uses pluck() to return an array containing just the countries you want.
def self.countries_except_for(user)
where.not(user_id: user.id, country: nil).pluck(:country)
end
Now you can just call:
Profile.countries_except(current_user)
To get the array of countries. This approach is much more efficient than looping through as in your question.
The line n = m.country.class == String sets n to true or false, thefore the next line fails, because you try to call reject! {|x| x == current_user.profile.user_id } on that boolean value.
One way to solve the problem might be to fix your code like this:
def method_name
Profile.all.select do |p|
p.country.present? && p != current_user.profile
end
end
Another way - and my preferred one - would be to only load matching record from the database:
def method_name
Profile.where.not(user_id: current_user.id).where.not(country: nil)
end

Rails : Return an object to show user's profile completeness

I have a User Model and would like to return to the controller and view what parts of the user's profile are complete and what parts are not.
So it would return for example an object "profile_completeness" which can be used like so :
#user.profile_completeness.personal_info
Which would be true if the particular part of the profile was filled.
What I tried :
def profile_completeness
if self.name.present? && self.email.present?
profile_completeness.personal_info = true
end
return profile_completeness
end
I got the error :
SystemStackError: stack level too deep
Am I going about this the right way? If not, what do you think is the best
You can't call set the .personal_info=true on the method itself. In such situation I'd just use either Hashes.
def profile_completeness
ret = Hash.new
if self.name.present? && self.email.present?
ret[:personal_info] = true
end
ret
end
Or even better I'd prefer different method for each of the 'completeness' part:
def personal_info_complete?
....
end

Why would this not work in Spree

I am having problems with modifying a function in spree. The function is called copy_price
The original version is something like this:
def copy_price
if variant
self.price = variant.price if price.nil?
self.currency = variant.currency if currency.nil?
end
end
which if I understand right will update the line_item's unit price only if the price is null, which I believe it shouldn't be inside the orders page (after the order is completed).
I noticed that order changes if the master price is changed inside the admin section even after the order is complete.
So i thought that the copy_price function was to blame, but each time i try to modify it there is no change.
E.g.
def copy_price
#price_run = true
self.price = 30.00
end
def get_price_run
if #price_run == true
return "true"
else
return "false"
end
end
and call get_price_run inside my view to print out if the price run was actually run. and It keeps outputting false. Does anyone know why that would be.
I have figured out the problem. The function copy_price is only called when the line item is first created (e.g. when you put it into the cart). So when I was trying to find out if it was called while looking at the admin orders page it was never getting called.

Resources