Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier code in Ruby

Is there a prettier version for this snippet of code?

@available_option_types.delete_if {|ot|
  result = true 
  result = current_user.retailer.id != ot.retailer.id if ot.retailer.present? 
  result
} unless current_user.has_role? 'admin'

Thank you!

like image 431
mabounassif Avatar asked Jan 20 '26 18:01

mabounassif


1 Answers

@available_option_types.delete_if { |ot|
  ot.retailer.present? ? (current_user.retailer.id != ot.retailer.id) : true
} unless current_user.has_role? 'admin'

Or it would be even prettier if you put some logic into the model:

class User
  def same_retailer_with?(option_type)
    option_type.retailer.present? ? (self.retailer.id != option_type.retailer.id) : true
  end
end

@available_option_types.delete_if { |ot| current_user.same_retailer_with?(ot) } unless current_user.has_role? 'admin'
like image 97
PeterWong Avatar answered Jan 23 '26 10:01

PeterWong