From a286b9e17ec18dc42b7ffadee14636b20700c732 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 27 Jul 2015 17:44:02 +0200 Subject: [PATCH] Improved code layout. --- app/models/application_model.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/app/models/application_model.rb b/app/models/application_model.rb index f5e4fd9c6..14c912fd4 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -340,25 +340,17 @@ returns =end def self.create_if_not_exists(data) + record = nil if data[:id] record = find_by( id: data[:id] ) - return record if record elsif data[:name] - records = where( name: data[:name] ) - records.each {|loop_record| - return loop_record if loop_record.name == data[:name] - } + record = find_by( name: data[:name] ) elsif data[:login] - records = where( login: data[:login] ) - records.each {|loop_record| - return loop_record if loop_record.login == data[:login] - } + record = find_by( login: data[:login] ) elsif data[:locale] && data[:source] - records = where( locale: data[:locale], source: data[:source] ) - records.each {|loop_record| - return loop_record if loop_record.source == data[:source] - } + record = find_by( locale: data[:locale], source: data[:source] ) end + return record if record create(data) end