Merge pull request #485 from digineo/logical_refactoring

Small refactoring of logical expressions
This commit is contained in:
Thorsten 2016-12-01 17:13:19 +01:00 committed by GitHub
commit e42042cc1c
14 changed files with 35 additions and 71 deletions

View file

@ -21,8 +21,7 @@ add an avatar based on auto detection (email address)
# return if we run import mode # return if we run import mode
return if Setting.get('import_mode') return if Setting.get('import_mode')
return if !data[:url] return if data[:url].blank?
return if data[:url].empty?
Avatar.add( Avatar.add(
object: data[:object], object: data[:object],
@ -96,7 +95,7 @@ add avatar by url
# check if avatar with url already exists # check if avatar with url already exists
avatar_already_exists = nil avatar_already_exists = nil
if data[:source] && !data[:source].empty? if data[:source].present?
avatar_already_exists = Avatar.find_by( avatar_already_exists = Avatar.find_by(
object_lookup_id: object_id, object_lookup_id: object_id,
o_id: data[:o_id], o_id: data[:o_id],
@ -105,7 +104,7 @@ add avatar by url
end end
# fetch image based on http url # fetch image based on http url
if data[:url] && data[:url] =~ /^http/ if data[:url] =~ /^http/
# check if source ist already updated within last 2 minutes # check if source ist already updated within last 2 minutes
if avatar_already_exists && avatar_already_exists.source_url == data[:url] if avatar_already_exists && avatar_already_exists.source_url == data[:url]

View file

@ -82,8 +82,7 @@ class Channel::Driver::Facebook
@sync['pages'].each { |page_to_sync_id, page_to_sync_params| @sync['pages'].each { |page_to_sync_id, page_to_sync_params|
page = get_page(page_to_sync_id) page = get_page(page_to_sync_id)
next if !page next if !page
next if !page_to_sync_params['group_id'] next if page_to_sync_params['group_id'].blank?
next if page_to_sync_params['group_id'].empty?
page_client = Facebook.new(page['access_token']) page_client = Facebook.new(page['access_token'])
posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}') posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}')

View file

@ -51,7 +51,7 @@ returns
ssl = false ssl = false
port = 110 port = 110
end end
if options.key?(:port) && !options[:port].empty? if options.key?(:port) && options[:port].present?
port = options[:port] port = options[:port]
# disable ssl for non ssl ports # disable ssl for non ssl ports

View file

@ -30,8 +30,8 @@ class Channel::Driver::Smtp
if !options.key?(:port) || options[:port].empty? if !options.key?(:port) || options[:port].empty?
options[:port] = 25 options[:port] = 25
end end
if !options.key?(:domain)
if !options.key?(:domain)
# set fqdn, if local fqdn - use domain of sender # set fqdn, if local fqdn - use domain of sender
fqdn = Setting.get('fqdn') fqdn = Setting.get('fqdn')
if fqdn =~ /(localhost|\.local^|\.loc^)/i && (attr['from'] || attr[:from]) if fqdn =~ /(localhost|\.local^|\.loc^)/i && (attr['from'] || attr[:from])
@ -58,7 +58,7 @@ class Channel::Driver::Smtp
} }
# add authentication only if needed # add authentication only if needed
if options[:user] && !options[:user].empty? if options[:user].present?
smtp_params[:user_name] = options[:user] smtp_params[:user_name] = options[:user]
smtp_params[:password] = options[:password] smtp_params[:password] = options[:password]
smtp_params[:authentication] = options[:authentication] smtp_params[:authentication] = options[:authentication]

View file

@ -269,13 +269,10 @@ returns
private private
def fetch_search def fetch_search
return if !@sync[:search] return if @sync[:search].blank?
return if @sync[:search].empty?
@sync[:search].each { |search| @sync[:search].each { |search|
next if !search[:term] next if search[:term].blank?
next if search[:term].to_s.empty? next if search[:group_id].blank?
next if !search[:group_id]
next if search[:group_id].to_s.empty?
result_type = search[:type] || 'mixed' result_type = search[:type] || 'mixed'
Rails.logger.debug " - searching for '#{search[:term]}'" Rails.logger.debug " - searching for '#{search[:term]}'"
older_import = 0 older_import = 0
@ -296,10 +293,8 @@ returns
end end
def fetch_mentions def fetch_mentions
return if !@sync[:mentions] return if @sync[:mentions].blank?
return if @sync[:mentions].empty? return if @sync[:mentions][:group_id].blank?
return if !@sync[:mentions][:group_id]
return if @sync[:mentions][:group_id].to_s.empty?
Rails.logger.debug ' - searching for mentions' Rails.logger.debug ' - searching for mentions'
older_import = 0 older_import = 0
older_import_max = 20 older_import_max = 20
@ -318,10 +313,8 @@ returns
end end
def fetch_direct_messages def fetch_direct_messages
return if !@sync[:direct_messages] return if @sync[:direct_messages].blank?
return if @sync[:direct_messages].empty? return if @sync[:direct_messages][:group_id].blank?
return if !@sync[:direct_messages][:group_id]
return if @sync[:direct_messages][:group_id].to_s.empty?
Rails.logger.debug ' - searching for direct_messages' Rails.logger.debug ' - searching for direct_messages'
older_import = 0 older_import = 0
older_import_max = 20 older_import_max = 20

View file

@ -74,9 +74,7 @@ class Channel::EmailParser
mail = Mail.new(msg) mail = Mail.new(msg)
# set all headers # set all headers
mail.header.fields.each { |field| mail.header.fields.select(&:name).each { |field|
next if !field.name
# full line, encode, ready for storage # full line, encode, ready for storage
data[field.name.to_s.downcase.to_sym] = Encode.conv('utf8', field.to_s) data[field.name.to_s.downcase.to_sym] = Encode.conv('utf8', field.to_s)
@ -318,12 +316,10 @@ class Channel::EmailParser
end end
# for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5) # for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
if !filename filename ||= file.header[:content_location].to_s
filename = file.header[:content_location].to_s
end
# generate file name # generate file name
if !filename || filename.empty? if filename.blank?
attachment_count = 0 attachment_count = 0
(1..1000).each { |count| (1..1000).each { |count|
filename_exists = false filename_exists = false

View file

@ -7,9 +7,7 @@ class Observer::Ticket::Article::CommunicateFacebook::BackgroundJob
article = Ticket::Article.find(@article_id) article = Ticket::Article.find(@article_id)
# set retry count # set retry count
if !article.preferences['delivery_retry'] article.preferences['delivery_retry'] ||= 0
article.preferences['delivery_retry'] = 0
end
article.preferences['delivery_retry'] += 1 article.preferences['delivery_retry'] += 1
ticket = Ticket.lookup(id: article.ticket_id) ticket = Ticket.lookup(id: article.ticket_id)
@ -24,7 +22,7 @@ class Observer::Ticket::Article::CommunicateFacebook::BackgroundJob
end end
# fill in_reply_to # fill in_reply_to
if !article.in_reply_to || article.in_reply_to.empty? if article.in_reply_to.blank?
article.in_reply_to = ticket.articles.first.message_id article.in_reply_to = ticket.articles.first.message_id
end end

View file

@ -25,8 +25,7 @@ class Organization < ApplicationModel
private private
def domain_cleanup def domain_cleanup
return if !domain return if domain.blank?
return if domain.empty?
domain.gsub!(/@/, '') domain.gsub!(/@/, '')
domain.gsub!(/\s*/, '') domain.gsub!(/\s*/, '')
domain.strip! domain.strip!

View file

@ -163,7 +163,7 @@ returns
} }
} }
config.hours = hours config.hours = hours
if !hours || hours.empty? if hours.blank?
raise "No configured hours found in calendar #{calendar.inspect}" raise "No configured hours found in calendar #{calendar.inspect}"
end end

View file

@ -73,7 +73,7 @@ module Ticket::Number::Date
end end
def check(string) def check(string)
return if !string || string.empty? return if string.blank?
# get config # get config
system_id = Setting.get('system_id') || '' system_id = Setting.get('system_id') || ''

View file

@ -70,7 +70,7 @@ module Ticket::Number::Increment
end end
def check(string) def check(string)
return if !string || string.empty? return if string.blank?
# get config # get config
system_id = Setting.get('system_id') || '' system_id = Setting.get('system_id') || ''

View file

@ -21,7 +21,7 @@ returns
# collect article data # collect article data
# add tags # add tags
tags = Tag.tag_list(object: 'Ticket', o_id: id) tags = Tag.tag_list(object: 'Ticket', o_id: id)
if tags && !tags.empty? if tags.present?
attributes[:tag] = tags attributes[:tag] = tags
end end

View file

@ -154,18 +154,7 @@ returns
=end =end
def role?(role_name) def role?(role_name)
roles.where(name: role_name).any?
result = false
roles.each { |role|
if role_name.class == Array
next if !role_name.include?(role.name)
elsif role.name != role_name
next
end
result = true
break
}
result
end end
=begin =begin
@ -226,16 +215,13 @@ returns
def self.authenticate(username, password) def self.authenticate(username, password)
# do not authenticate with nothing # do not authenticate with nothing
return if !username || username == '' return if username.blank? || password.blank?
return if !password || password == ''
# try to find user based on login # try to find user based on login
user = User.find_by(login: username.downcase, active: true) user = User.find_by(login: username.downcase, active: true)
# try second lookup with email # try second lookup with email
if !user user ||= User.find_by(email: username.downcase, active: true)
user = User.find_by(email: username.downcase, active: true)
end
# check failed logins # check failed logins
max_login_failed = Setting.get('password_max_login_failed').to_i || 10 max_login_failed = Setting.get('password_max_login_failed').to_i || 10
@ -249,7 +235,7 @@ returns
# set login failed +1 # set login failed +1
if !user_auth && user if !user_auth && user
sleep 1 sleep 1
user.login_failed = user.login_failed + 1 user.login_failed += 1
user.save user.save
end end
@ -454,15 +440,13 @@ returns
=end =end
def self.password_reset_new_token(username) def self.password_reset_new_token(username)
return if !username || username == '' return if username.blank?
# try to find user based on login # try to find user based on login
user = User.find_by(login: username.downcase, active: true) user = User.find_by(login: username.downcase, active: true)
# try second lookup with email # try second lookup with email
if !user user ||= User.find_by(email: username.downcase, active: true)
user = User.find_by(email: username.downcase, active: true)
end
# check if email address exists # check if email address exists
return if !user return if !user
@ -737,8 +721,7 @@ returns
end end
def check_preferences_default def check_preferences_default
return if !@preferences_default return if @preferences_default.blank?
return if @preferences_default.empty?
preferences_tmp = @preferences_default.merge(preferences) preferences_tmp = @preferences_default.merge(preferences)
self.preferences = preferences_tmp self.preferences = preferences_tmp
@ -871,8 +854,7 @@ returns
end end
def avatar_for_email_check def avatar_for_email_check
return if !email return if email.blank?
return if email.empty?
return if email !~ /@/ return if email !~ /@/
return if !changes['email'] && updated_at > Time.zone.now - 10.days return if !changes['email'] && updated_at > Time.zone.now - 10.days
@ -900,7 +882,7 @@ returns
def check_password def check_password
# set old password again if not given # set old password again if not given
if password == '' || !password if password.blank?
# get current record # get current record
if id if id

View file

@ -22,9 +22,7 @@ store new device for user if device not already known
def self.add(user_agent, ip, user_id, fingerprint, type) def self.add(user_agent, ip, user_id, fingerprint, type)
# since gem browser 2 is not handling nil for user_agent, set it to '' # since gem browser 2 is not handling nil for user_agent, set it to ''
if user_agent.nil? user_agent ||= ''
user_agent = ''
end
# get location info # get location info
location_details = Service::GeoIp.location(ip) location_details = Service::GeoIp.location(ip)
@ -79,7 +77,7 @@ store new device for user if device not already known
name = browser[:plattform] name = browser[:plattform]
end end
if browser[:name] && browser[:name] != 'Other' if browser[:name] && browser[:name] != 'Other'
if name && !name.empty? if name.present?
name += ', ' name += ', '
end end
name += browser[:name] name += browser[:name]