這段代碼是否有更漂亮的版本?Ruby中更漂亮的代碼
@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'
謝謝!
這段代碼是否有更漂亮的版本?Ruby中更漂亮的代碼
@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'
謝謝!
@available_option_types.delete_if { |ot|
ot.retailer.present? ? (current_user.retailer.id != ot.retailer.id) : true
} unless current_user.has_role? 'admin'
或者,如果你把一些邏輯到模型中,將會變得更漂亮:的has_many option_types
@available_option_types = current_user.retailer.options_types unless current_user.has_role? 'admin'
unless current_user.has_role? 'admin'
@available_option_types.delete_if do |ot|
!ot.retailer.present? ||
(ot.retailer.present? &&
current_user.retailer.id != ot.retailer.id)
end
end
如果零售商這可能會實現或
@available_option_types.select do |ot|
ot.retailer.present? && current_user.retailer.id == ot.retailer.id
end unless current_user.has_role? 'admin'
@available_option_types.delete_if do |ot|
!ot.retailer.present? || current_user.retailer.id != ot.retailer.id
end unless current_user.has_role? 'admin'
:
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'