Do away with eval

Eval is unnecessary here. Sure the code has slighly more duplication, but no where
near enough to warrant an eval.
pull/3752/head
Mohamed Wael Khobalatte 9 years ago
parent 46eed651ed
commit 80192f6986
  1. 2
      app/controllers/settings_controller.rb
  2. 36
      app/models/user.rb
  3. 2
      lib/open_project/object_linking.rb

@ -60,7 +60,7 @@ class SettingsController < ApplicationController
redirect_to action: 'edit', tab: params[:tab]
else
@options = {}
@options[:user_format] = User::USER_FORMATS.keys.map { |f| [User.current.name(f), f.to_s] }
@options[:user_format] = User::USER_FORMATS_STRUCTURE.keys.map { |f| [User.current.name(f), f.to_s] }
@deliveries = ActionMailer::Base.perform_deliveries
@guessed_host = request.host_with_port.dup

@ -33,23 +33,11 @@ class User < Principal
include User::Authorization
USER_FORMATS_STRUCTURE = {
firstname_lastname: [:firstname, :lastname],
firstname: [:firstname],
lastname_firstname: [:lastname, :firstname],
lastname_coma_firstname: [:lastname, :firstname],
username: [:login]
}
def self.user_format_structure_to_format(key, delimiter = ' ')
USER_FORMATS_STRUCTURE[key].map { |elem| "\#{#{elem}}" }.join(delimiter)
end
USER_FORMATS = {
firstname_lastname: User.user_format_structure_to_format(:firstname_lastname),
firstname: User.user_format_structure_to_format(:firstname),
lastname_firstname: User.user_format_structure_to_format(:lastname_firstname),
lastname_coma_firstname: User.user_format_structure_to_format(:lastname_coma_firstname, ', '),
username: User.user_format_structure_to_format(:username)
firstname_lastname: [:firstname, :lastname],
firstname: [:firstname],
lastname_firstname: [:lastname, :firstname],
lastname_coma_firstname: [:lastname, :firstname],
username: [:login]
}
USER_MAIL_OPTION_ALL = ['all', :label_user_mail_option_all]
@ -294,12 +282,18 @@ class User < Principal
end
end
# Return user's full name for display
# Formats the user's name.
def name(formatter = nil)
if formatter
eval ('"' + (User::USER_FORMATS[formatter] || User::USER_FORMATS[:firstname_lastname]) + '"')
case formatter || Setting.user_format
when :firstname_lastname then "#{firstname} #{lastname}"
when :lastname_firstname then "#{lastname} #{firstname}"
when :lastname_coma_firstname then "#{lastname}, #{firstname}"
when :firstname then firstname
when :username then login
else
@name ||= eval ('"' + (User::USER_FORMATS[Setting.user_format] || User::USER_FORMATS[:firstname_lastname]) + '"')
"#{firstname} #{lastname}"
end
end

@ -43,7 +43,7 @@ module OpenProject
# Displays a link to user's account page if active or registered
def link_to_user(user, options = {})
if user.is_a?(User)
name = user.name(options.delete(:format))
name = user.name
if user.active? || user.registered? || user.invited?
link_to(name, user, options)
else

Loading…
Cancel
Save