I get a warning every time I start my server that I have string literals in my conditions. What does this mean, and how can I improve my code to fix this and stop getting the warning?
def partner_cars
if self.role == 'Garage Employee' || self.role == 'Garage Manager'
self.cars
elsif self.role == 'Company Employee' || 'Company Manager'
self.company.cars
else
nil
end
end
def partner_users
if self.role == 'Garage Employee' || self.role == 'Garage Manager'
allowed_users = self.users
elsif self.role == 'Company Employee' || 'Company Manager'
allowed_users = self.company.users
else
allowed_users = nil
end
allowed_users.uniq
end
Any help on how to refactor this code would be much appreciated, thanks!
The error comes from the line elsif self.role == 'Company Employee' || 'Company Manager'. Instead, it should be elsif self.role == 'Company Employee' || self.role == 'Company Manager'.
However, I would refactor your code to use case statements like this:
def partner_cars
case role
when 'Garage Employee', 'Garage Manager' then cars
when 'Company Employee', 'Company Manager' then company.cars
end
end
def partner_users
allowed_users =
case role
when 'Garage Employee', 'Garage Manager' then users
when 'Company Employee', 'Company Manager' then company.users
end
allowed_users.try(:uniq)
end
Without knowing your data structure, its hard to suggest a refactoring approach.
def partner_cars
if garage_roles.include?(self.role)
...some code
elsif employee_roles.include?(self.role)
..some code
end
end
private
def garage_roles
['Garage Employee', 'Garage Manager']
end
Even better would be to set up these methods in your role model.
class Role
def works_for_garage?
title == 'Garage Manager' || 'Garage Employee'
end
end
Related
So what I am trying to accomplish is if #provider.licenses.where(issuing_state:'CA')' it has to match the #form.state as well which would be 'CA' in this case in order to have access to that page. Now if the provider.licenses.issuing_state does not match #form.state the user would be redirect. Here is what I have
def edit
#user = current_user
#patient = Patient.find(params[:patient_id])
#form_response = FormResponse.find_by_id(params[:id])
#form = #form_response.form
#provider = current_user.try(:provider)
#provider_user = #provider.try(:role).try(:user)
if current_user.is_provider?
if #provider.licenses.where(issuing_state:'CO') && #form.state == 'CA' || #provider.licenses.where(issuing_state:'CA') && #form.state == 'CO'
redirect_to root_path, alert: 'You do not have access to update form'
end
end
end
Something like #provider.licenses.where(..) is always trueish because it returns a ActiveRecord::Relation.
You might want to use #provider.licenses.exist?(issuing_state:'CO') instead.
if #provider.licenses.exist?(issuing_state:'CO') && #form.state == 'CA' ||
#provider.licenses.exist?(issuing_state:'CA') && #form.state == 'CO'
redirect_to root_path, alert: 'You do not have access to update form'
end
I've got two methods in a controller with very similar code. Wondering how I could DRY them up! They both utilize csv-importer gem to parse a csv file.
sales_controller.rb
def import_csv_test
user_id = params[:user_id]
import = ImportSaleCSV.new(file: params[:file]) do
after_build do |sale|
sale.user_id = user_id
skip! if sale.email == nil
skip! if sale.order_date == nil
skip! if sale.amount == nil
end
end
import.run!
redirect_to lifecycle_grid_sales_path, notice: import.report.message
end
def import_ftp
user_id = params[:user_id]
import = ImportSaleCSV.new(path: './public/uploads/gotcha.csv') do
after_build do |sale|
sale.user_id = user_id
skip! if sale.email == nil
skip! if sale.order_date == nil
skip! if sale.amount == nil
end
end
import.run!
redirect_to lifecycle_grid_sales_path, notice: import.report.message
end
Thanks!
I think you can extract a class to do the heavy lifting.
class ImportSaleCSVCreator
def initialize(csv_options = {}, csv_attributes = {})
#csv_options = csv_options
#csv_attributes = csv_attributes
end
def build
ImportSaleCSV.new(csv_options) do
after_build do |sale|
csv_attributes.each { |k, v| sale.public_send("#{k}=", v) }
skip! if sale.email.nil? || sale.order_date.nil? || sale.amount.nil?
end
end
end
private
attr_reader :csv_options, :csv_attributes
end
class Controller
def import_csv
import = ImportSaleCSVCreator.new({ file: params[:file] }, { user_id: params[:user_id] })
import.run!
end
def import_ftp
import = ImportSaleCSVCreator.new({ path: './gotcha.csv' }, { user_id: params[:user_id] })
import.run!
end
end
Make sure you check attributes passed. Especially when dealing with files, paths, etc. You might want to filter the parameters in ImportSaleCSVCreator.
You may refactor both your methods into single one:
def import(hash)
user_id = params[:user_id]
import = ImportSaleCSV.new(hash) do
after_build do |sale|
sale.user_id = user_id
skip! if sale.email == nil
skip! if sale.order_date == nil
skip! if sale.amount == nil
end
end
import.run!
redirect_to lifecycle_grid_sales_path, notice: import.report.message
end
And then call it:
import({file: params[:file]})
import({path: './public/uploads/gotcha.csv'})
It doesn't seem that method belongs to your controller so you may want to extract it somewhere. I encourage you to check this great article and extract your method into brand new Service object.
I want to create custom validation inside The Model.
But nothing in return when i tried to get that value from the variable
This is my model
validate :permanent_event_check
private
def permanent_event_check
param_activity = #activity
# puts "param_activityparam_activityparam_activity"
# puts #activity
# puts param_activity
# puts "param_activityparam_activityparam_activityparam_activity"
if param_activity.permanent == "false"
if param_activity.start_at == "" || param_activity.end_at == ""
#activity.errors[:base] << "You can't leave start and end date blank with Permanent Event"
return false
end
end
end
This is my controller
def create
#activity = admin_current_user.activities.build(activity_param)
if #activity.save
flash[:success] = "Activity Created!"
redirect_to admin_dashboard_url
else
render 'new'
end
end
private
def activity_param
params.require(:activity).permit(:name,:details,:start_at,:end_at,
:activity_image01_url,:activity_image02_url,:activity_image03_url,
:youtube_url,:capacity,:booking_status,:rules,:apply_details,
:payment_price,:payment_need,:avaliable,:rating,:temple_id)
end
But it return nil when i tried to get the value from #activity inside my model.
How can i fix this?
Thanks!
You cannot assign the object like that in the model, instead you van take self.
validates :permanent_event_check
private
def permanent_event_check
if self.permanent == "false"
if self.start_at == "" || self.end_at == ""
self.errors[:base] << "You can't leave start and end date blank with Permanent Event"
return false
end
end
end
I assume that permanent is boolean, start_at and end_at - datetime.
validate :permanent_event_check, unless :permanent
private
def permanent_event_check
# if start_at and end_at are not filled they will be nil which is interpreted as false
unless start_at && end_at
self.errors[:base] << "You can't leave start and end date blank with Permanent Event"
end
end
The goal of this method is to check when a user signs up, if they are on the list of users that should be admins.
I am new to Ruby and think it is just a syntax error:
def set_role
if self[:email] == ("email#gmail.com" || "sample#gmail.com" || "test#gmail.com")
self[:role] = "admin"
else
self[:role] = "customer"
end
end
This would be a good time to use a case statement:
def set_role
self[:role] = case self[:email]
when "email#gmail.com", "sample#gmail.com", "test#gmail.com"
'admin'
else
'customer'
end
end
It's easy to add new values to the when test.
You want to check if an array of emails includes the current email like this:
def set_role
if ["email#gmail.com", "sample#gmail.com", "test#gmail.com"].include?(self[:email])
self[:role] = "admin"
else
self[:role] = "customer"
end
end
This code could also be improved:
def set_role
admin_emails = ["email#gmail.com", "sample#gmail.com", "test#gmail.com"]
self[:role] = if admin_emails.include?(self[:email])
"admin"
else
"customer"
end
end
I have this call in my vote model:
fires :vote_updated, :on => :update,
:actor => :user,
:secondary_subject => :video,
:if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && (vote.video.user != current_user)}
In case you aren't familiar, it works with the timeline_fu plugin.
I do not want the call to be fired if the user who owns the voted up video is the current user. That is where this line comes in:
:if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && (vote.video.user != current_user)}
However, I do not have access to current_user here. How do I get around this?
Here's the create method in my votes controller (there actually is no update method):
def create
#video = Video.find(params[:video_id])
#vote = current_user.video_votes.find_or_create_by_video_id(#video.id)
if #vote.value.nil?
if params[:type] == "up"
#vote.value = 1
else
#vote.value = -1
end
elsif (params[:type] == "up" && #vote.value == 1) || (params[:type] == "down" && #vote.value == -1)
#vote.value = 0
elsif ((params[:type] == "up" && #vote.value == -1) || (params[:type] == "down" && #vote.value == 1)) || (#vote.value == 0)
if params[:type] == "up"
#vote.value = 1
else
#vote.value = -1
end
end
if #vote.save
respond_to do |format|
format.html { redirect_to #video }
format.js
end
else
respond_to do |format|
format.html
format.js
end
end
end
I believe the right thing to do would be validating this in controller. I would create a before filter for this case
UPDATE:
Just as a quick example:
before_filter :valid_vote, :only => :update
def update
#vote.update_attributes(params[:vote]) # or whatever
end
..
private
def valid_vote
#vote = Vote.find params[:id]
unless ( #vote.video.user.id != current_user.id )
render :text => 'You can't vote for your own video', :status => 403
end
end
So #vote is being declared and validated before your 'update' action is proccessed.
If it's not valid then your 'update' action stays untouched
UPDATE 2 :
not sure how you'll like it, but you could also do as follows:
in your Vote model:
attr_accessor :skip_timeline
then use the concept with before filter, but do #vote.skip_timeline = true instead of rendering text
then the statement might look as follows:
:if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && !vote.skip_timeline }
You could also move ((vote.value == 1) || (vote.value == -1)) to your before filter :
def valid_vote
#vote = Vote.find params[:id]
unless ( [1,-1].include? #vote.value && #vote.video.user.id != current_user.id )
#vote.skip_timeline = true
end
end
and
:if => lambda { |vote| !vote.skip_timeline }
You are getting this error because it's typically not recommended to access current_user (or session information) in your model. I am not all that familiar with the timeline_fu gem, so this answer isn't going to be the greatest answer you may get. I'm merely going to show you how to access current_user from any model.
First go to your application controller. You'll want to make a method that sets the current user. You need to call the method in the before filter.
before_filter :loadCurrentUser
def loadCurrentUser
User.currentUser = current_user
end
Then in your User model, you need to define 'currentUser'.
def self.currentUser
Thread.currentUser[:user]
end
You don't necessarily have to declare the current_user in the application controller, but since it's a gem, I'm not sure if it has an easily accessible controller.
Edit: This way may be prone to problems, but I'm not entirely sure if you were asking how to make current_user available in models, or a completely different workaround so you do not have that problem... and reading the responses of the other answer, I'm thinking it's not what you were asking.