user_preference.rb
134 lines
| 3.6 KiB
| text/x-ruby
|
RubyLexer
|
r6389 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r60 | # | ||
# 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. | ||||
|
r6389 | # | ||
|
r60 | # 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. | ||||
|
r6389 | # | ||
|
r60 | # 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. | ||||
class UserPreference < ActiveRecord::Base | ||||
|
r15306 | include Redmine::SafeAttributes | ||
|
r60 | belongs_to :user | ||
|
r171 | serialize :others | ||
|
r6389 | |||
|
r9019 | attr_protected :others, :user_id | ||
|
r6389 | |||
|
r7950 | before_save :set_others_hash | ||
|
r11440 | |||
|
r15306 | safe_attributes 'hide_mail', | ||
'time_zone', | ||||
'comments_sorting', | ||||
'warn_on_leaving_unsaved', | ||||
|
r15371 | 'no_self_notified', | ||
'textarea_font' | ||||
TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional'] | ||||
|
r15306 | |||
|
r8167 | def initialize(attributes=nil, *args) | ||
|
r60 | super | ||
|
r15819 | 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) | ||||
self.no_self_notified = true | ||||
end | ||||
|
r14906 | end | ||
|
r60 | self.others ||= {} | ||
end | ||||
|
r6389 | |||
|
r7950 | def set_others_hash | ||
|
r476 | self.others ||= {} | ||
end | ||||
|
r6389 | |||
|
r60 | def [](attr_name) | ||
|
r11896 | if has_attribute? attr_name | ||
|
r60 | super | ||
else | ||||
|
r476 | others ? others[attr_name] : nil | ||
|
r60 | end | ||
end | ||||
|
r6389 | |||
|
r60 | def []=(attr_name, value) | ||
|
r11896 | if has_attribute? attr_name | ||
|
r60 | super | ||
else | ||||
|
r10255 | h = (read_attribute(:others) || {}).dup | ||
|
r1609 | h.update(attr_name => value) | ||
write_attribute(:others, h) | ||||
value | ||||
|
r60 | end | ||
end | ||||
|
r6389 | |||
|
r1183 | def comments_sorting; self[:comments_sorting] end | ||
def comments_sorting=(order); self[:comments_sorting]=order end | ||||
|
r6389 | |||
|
r4780 | 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 | ||||
|
r11609 | |||
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 | ||||
|
r14296 | |||
def activity_scope; Array(self[:activity_scope]) ; end | ||||
def activity_scope=(value); self[:activity_scope]=value ; end | ||||
|
r15371 | |||
def textarea_font; self[:textarea_font] end | ||||
def textarea_font=(value); self[:textarea_font]=value; end | ||||
|
r15548 | |||
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 | ||||
|
r15550 | |||
|
r15560 | def my_page_settings(block=nil) | ||
s = self[:my_page_settings] ||= {} | ||||
if block | ||||
|
r15694 | s[block] ||= {} | ||
|
r15560 | else | ||
s | ||||
end | ||||
end | ||||
def my_page_settings=(arg) | ||||
self[:my_page_settings] = arg | ||||
end | ||||
|
r15550 | def remove_block(block) | ||
block = block.to_s.underscore | ||||
%w(top left right).each do |f| | ||||
(my_page_layout[f] ||= []).delete(block) | ||||
end | ||||
my_page_layout | ||||
end | ||||
def add_block(block) | ||||
block = block.to_s.underscore | ||||
return unless Redmine::MyPage.blocks.key?(block) | ||||
remove_block(block) | ||||
# add it on top | ||||
my_page_layout['top'] ||= [] | ||||
my_page_layout['top'].unshift(block) | ||||
end | ||||
|
r15560 | |||
def update_block_settings(block, settings) | ||||
block_settings = my_page_settings(block).merge(settings.symbolize_keys) | ||||
my_page_settings[block] = block_settings | ||||
end | ||||
|
r60 | end | ||