2019-03-16 09:37:35 +00:00
# frozen_string_literal: true
2019-03-15 01:32:57 +00:00
2011-08-21 01:58:55 +00:00
# Redmine - project management software
2022-01-02 05:29:10 +00:00
# Copyright (C) 2006-2022 Jean-Philippe Lang
2006-12-03 19:55:45 +00:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
2011-08-21 01:58:55 +00:00
#
2006-12-03 19:55:45 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2011-08-21 01:58:55 +00:00
#
2006-12-03 19:55:45 +00:00
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2017-07-25 15:38:56 +00:00
require 'redmine/my_page'
2006-12-03 19:55:45 +00:00
class UserPreference < ActiveRecord :: Base
2016-07-17 06:43:12 +00:00
include Redmine :: SafeAttributes
2006-12-03 19:55:45 +00:00
belongs_to :user
2007-01-25 18:25:57 +00:00
serialize :others
2011-08-21 01:58:55 +00:00
2017-03-15 17:52:41 +00:00
before_save :set_others_hash , :clear_unused_block_settings
2013-03-20 08:48:03 +00:00
2019-11-09 09:17:56 +00:00
safe_attributes (
'hide_mail' ,
2016-07-17 06:43:12 +00:00
'time_zone' ,
'comments_sorting' ,
'warn_on_leaving_unsaved' ,
2016-08-30 19:59:29 +00:00
'no_self_notified' ,
2020-01-21 04:23:26 +00:00
'notify_about_high_priority_issues' ,
2019-05-20 22:26:30 +00:00
'textarea_font' ,
2019-06-20 07:13:54 +00:00
'recently_used_projects' ,
2020-01-14 14:38:48 +00:00
'history_default_tab' ,
2021-07-06 06:01:50 +00:00
'default_issue_query' ,
2021-11-15 21:58:39 +00:00
'default_project_query' ,
2020-01-14 14:38:48 +00:00
'toolbar_language_options' )
2016-08-30 19:59:29 +00:00
TEXTAREA_FONT_OPTIONS = [ 'monospace' , 'proportional' ]
2020-01-14 14:38:48 +00:00
DEFAULT_TOOLBAR_LANGUAGE_OPTIONS = %w[ c cpp csharp css diff go groovy html java javascript objc perl php python r ruby sass scala shell sql swift xml yaml ]
2016-07-17 06:43:12 +00:00
2011-12-18 13:26:20 +00:00
def initialize ( attributes = nil , * args )
2006-12-03 19:55:45 +00:00
super
2017-01-15 12:58:07 +00:00
if new_record?
unless attributes && attributes . key? ( :hide_mail )
self . hide_mail = Setting . default_users_hide_mail?
end
unless attributes && attributes . key? ( :time_zone )
self . time_zone = Setting . default_users_time_zone
end
unless attributes && attributes . key? ( :no_self_notified )
2021-03-10 03:35:22 +00:00
self . no_self_notified = Setting . default_users_no_self_notified
2017-01-15 12:58:07 +00:00
end
2016-04-03 11:33:28 +00:00
end
2006-12-03 19:55:45 +00:00
self . others || = { }
end
2011-08-21 01:58:55 +00:00
2011-12-04 06:44:39 +00:00
def set_others_hash
2007-04-25 15:06:20 +00:00
self . others || = { }
end
2011-08-21 01:58:55 +00:00
2006-12-03 19:55:45 +00:00
def [] ( attr_name )
2013-09-10 17:39:49 +00:00
if has_attribute? attr_name
2006-12-03 19:55:45 +00:00
super
else
2007-04-25 15:06:20 +00:00
others ? others [ attr_name ] : nil
2006-12-03 19:55:45 +00:00
end
end
2011-08-21 01:58:55 +00:00
2006-12-03 19:55:45 +00:00
def []= ( attr_name , value )
2013-09-10 17:39:49 +00:00
if has_attribute? attr_name
2006-12-03 19:55:45 +00:00
super
else
2012-09-22 06:16:53 +00:00
h = ( read_attribute ( :others ) || { } ) . dup
2008-07-04 17:58:14 +00:00
h . update ( attr_name = > value )
write_attribute ( :others , h )
value
2006-12-03 19:55:45 +00:00
end
end
2011-08-21 01:58:55 +00:00
2020-11-21 13:53:05 +00:00
def comments_sorting ; self [ :comments_sorting ] ; end
def comments_sorting = ( order ) ; self [ :comments_sorting ] = order ; end
2011-08-21 01:58:55 +00:00
2011-02-21 09:53:29 +00:00
def warn_on_leaving_unsaved ; self [ :warn_on_leaving_unsaved ] || '1' ; end
def warn_on_leaving_unsaved = ( value ) ; self [ :warn_on_leaving_unsaved ] = value ; end
2013-05-13 16:38:08 +00:00
def no_self_notified ; ( self [ :no_self_notified ] == true || self [ :no_self_notified ] == '1' ) ; end
def no_self_notified = ( value ) ; self [ :no_self_notified ] = value ; end
2015-10-15 19:06:22 +00:00
2020-01-21 04:23:26 +00:00
def notify_about_high_priority_issues ; ( self [ :notify_about_high_priority_issues ] == true || self [ :notify_about_high_priority_issues ] == '1' ) ; end
def notify_about_high_priority_issues = ( value ) ; self [ :notify_about_high_priority_issues ] = value ; end
2020-11-21 13:53:05 +00:00
def activity_scope ; Array ( self [ :activity_scope ] ) ; end
def activity_scope = ( value ) ; self [ :activity_scope ] = value ; end
2016-08-30 19:59:29 +00:00
2020-11-21 13:53:05 +00:00
def textarea_font ; self [ :textarea_font ] ; end
2016-08-30 19:59:29 +00:00
def textarea_font = ( value ) ; self [ :textarea_font ] = value ; end
2016-10-29 10:10:31 +00:00
2019-05-20 22:26:30 +00:00
def recently_used_projects ; ( self [ :recently_used_projects ] || 3 ) . to_i ; end
def recently_used_projects = ( value ) ; self [ :recently_used_projects ] = value . to_i ; end
2019-06-20 07:13:54 +00:00
def history_default_tab ; self [ :history_default_tab ] ; end
def history_default_tab = ( value ) ; self [ :history_default_tab ] = value ; end
2019-05-20 22:26:30 +00:00
2020-01-14 14:38:48 +00:00
def toolbar_language_options
self [ :toolbar_language_options ] . presence || DEFAULT_TOOLBAR_LANGUAGE_OPTIONS . join ( ',' )
end
def toolbar_language_options = ( value )
2020-10-11 12:57:00 +00:00
languages =
value . to_s . delete ( ' ' ) . split ( ',' ) . select do | lang |
Redmine :: SyntaxHighlighting . language_supported? ( lang )
end . compact
2020-01-14 14:38:48 +00:00
self [ :toolbar_language_options ] = languages . join ( ',' )
end
2021-07-06 06:01:50 +00:00
def default_issue_query ; self [ :default_issue_query ] end
def default_issue_query = ( value ) ; self [ :default_issue_query ] = value ; end
2021-11-15 21:58:39 +00:00
def default_project_query ; self [ :default_project_query ] end
def default_project_query = ( value ) ; self [ :default_project_query ] = value ; end
2017-04-04 17:17:47 +00:00
# Returns the names of groups that are displayed on user's page
# Example:
# preferences.my_page_groups
# # => ['top', 'left, 'right']
def my_page_groups
Redmine :: MyPage . groups
end
2016-10-29 10:10:31 +00:00
def my_page_layout
self [ :my_page_layout ] || = Redmine :: MyPage . default_layout . deep_dup
end
def my_page_layout = ( arg )
self [ :my_page_layout ] = arg
end
2016-10-29 10:25:55 +00:00
2016-10-30 08:32:31 +00:00
def my_page_settings ( block = nil )
s = self [ :my_page_settings ] || = { }
if block
2016-12-14 12:28:47 +00:00
s [ block ] || = { }
2016-10-30 08:32:31 +00:00
else
s
end
end
def my_page_settings = ( arg )
self [ :my_page_settings ] = arg
end
2017-03-16 18:02:43 +00:00
# Removes block from the user page layout
2017-04-04 17:17:47 +00:00
# Example:
# preferences.remove_block('news')
2016-10-29 10:25:55 +00:00
def remove_block ( block )
block = block . to_s . underscore
2018-08-01 14:28:39 +00:00
my_page_layout . each_key do | group |
2017-04-04 17:17:47 +00:00
my_page_layout [ group ] . delete ( block )
2016-10-29 10:25:55 +00:00
end
my_page_layout
end
2017-03-16 18:02:43 +00:00
# Adds block to the user page layout
# Returns nil if block is not valid or if it's already
# present in the user page layout
2016-10-29 10:25:55 +00:00
def add_block ( block )
block = block . to_s . underscore
2017-03-16 18:02:43 +00:00
return unless Redmine :: MyPage . valid_block? ( block , my_page_layout . values . flatten )
2016-10-29 10:25:55 +00:00
remove_block ( block )
2017-04-04 17:17:47 +00:00
# add it to the first group
group = my_page_groups . first
my_page_layout [ group ] || = [ ]
my_page_layout [ group ] . unshift ( block )
end
# Sets the block order for the given group.
# Example:
# preferences.order_blocks('left', ['issueswatched', 'news'])
def order_blocks ( group , blocks )
group = group . to_s
if Redmine :: MyPage . groups . include? ( group ) && blocks . present?
blocks = blocks . map ( & :underscore ) & my_page_layout . values . flatten
blocks . each { | block | remove_block ( block ) }
my_page_layout [ group ] = blocks
end
2016-10-29 10:25:55 +00:00
end
2016-10-30 08:32:31 +00:00
def update_block_settings ( block , settings )
2017-03-14 18:18:19 +00:00
block = block . to_s
2016-10-30 08:32:31 +00:00
block_settings = my_page_settings ( block ) . merge ( settings . symbolize_keys )
my_page_settings [ block ] = block_settings
end
2017-03-15 17:52:41 +00:00
def clear_unused_block_settings
blocks = my_page_layout . values . flatten
my_page_settings . keep_if { | block , settings | blocks . include? ( block ) }
end
private :clear_unused_block_settings
2006-12-03 19:55:45 +00:00
end