##// END OF EJS Templates
Wrap conditions with a single if new_record?....
Jean-Philippe Lang -
r15819:ae99ecd54fce
parent child
Show More
@@ -1,132 +1,134
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class UserPreference < ActiveRecord::Base
18 class UserPreference < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 include Redmine::SafeAttributes
20
20
21 belongs_to :user
21 belongs_to :user
22 serialize :others
22 serialize :others
23
23
24 attr_protected :others, :user_id
24 attr_protected :others, :user_id
25
25
26 before_save :set_others_hash
26 before_save :set_others_hash
27
27
28 safe_attributes 'hide_mail',
28 safe_attributes 'hide_mail',
29 'time_zone',
29 'time_zone',
30 'comments_sorting',
30 'comments_sorting',
31 'warn_on_leaving_unsaved',
31 'warn_on_leaving_unsaved',
32 'no_self_notified',
32 'no_self_notified',
33 'textarea_font'
33 'textarea_font'
34
34
35 TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
35 TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
36
36
37 def initialize(attributes=nil, *args)
37 def initialize(attributes=nil, *args)
38 super
38 super
39 if new_record? && !(attributes && attributes.key?(:hide_mail))
39 if new_record?
40 self.hide_mail = Setting.default_users_hide_mail?
40 unless attributes && attributes.key?(:hide_mail)
41 end
41 self.hide_mail = Setting.default_users_hide_mail?
42 if new_record? && !(attributes && attributes.key?(:time_zone))
42 end
43 self.time_zone = Setting.default_users_time_zone
43 unless attributes && attributes.key?(:time_zone)
44 end
44 self.time_zone = Setting.default_users_time_zone
45 if new_record? && !(attributes && attributes.key?(:no_self_notified))
45 end
46 self.no_self_notified = true
46 unless attributes && attributes.key?(:no_self_notified)
47 self.no_self_notified = true
48 end
47 end
49 end
48 self.others ||= {}
50 self.others ||= {}
49 end
51 end
50
52
51 def set_others_hash
53 def set_others_hash
52 self.others ||= {}
54 self.others ||= {}
53 end
55 end
54
56
55 def [](attr_name)
57 def [](attr_name)
56 if has_attribute? attr_name
58 if has_attribute? attr_name
57 super
59 super
58 else
60 else
59 others ? others[attr_name] : nil
61 others ? others[attr_name] : nil
60 end
62 end
61 end
63 end
62
64
63 def []=(attr_name, value)
65 def []=(attr_name, value)
64 if has_attribute? attr_name
66 if has_attribute? attr_name
65 super
67 super
66 else
68 else
67 h = (read_attribute(:others) || {}).dup
69 h = (read_attribute(:others) || {}).dup
68 h.update(attr_name => value)
70 h.update(attr_name => value)
69 write_attribute(:others, h)
71 write_attribute(:others, h)
70 value
72 value
71 end
73 end
72 end
74 end
73
75
74 def comments_sorting; self[:comments_sorting] end
76 def comments_sorting; self[:comments_sorting] end
75 def comments_sorting=(order); self[:comments_sorting]=order end
77 def comments_sorting=(order); self[:comments_sorting]=order end
76
78
77 def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
79 def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
78 def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
80 def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
79
81
80 def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
82 def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
81 def no_self_notified=(value); self[:no_self_notified]=value; end
83 def no_self_notified=(value); self[:no_self_notified]=value; end
82
84
83 def activity_scope; Array(self[:activity_scope]) ; end
85 def activity_scope; Array(self[:activity_scope]) ; end
84 def activity_scope=(value); self[:activity_scope]=value ; end
86 def activity_scope=(value); self[:activity_scope]=value ; end
85
87
86 def textarea_font; self[:textarea_font] end
88 def textarea_font; self[:textarea_font] end
87 def textarea_font=(value); self[:textarea_font]=value; end
89 def textarea_font=(value); self[:textarea_font]=value; end
88
90
89 def my_page_layout
91 def my_page_layout
90 self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
92 self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
91 end
93 end
92
94
93 def my_page_layout=(arg)
95 def my_page_layout=(arg)
94 self[:my_page_layout] = arg
96 self[:my_page_layout] = arg
95 end
97 end
96
98
97 def my_page_settings(block=nil)
99 def my_page_settings(block=nil)
98 s = self[:my_page_settings] ||= {}
100 s = self[:my_page_settings] ||= {}
99 if block
101 if block
100 s[block] ||= {}
102 s[block] ||= {}
101 else
103 else
102 s
104 s
103 end
105 end
104 end
106 end
105
107
106 def my_page_settings=(arg)
108 def my_page_settings=(arg)
107 self[:my_page_settings] = arg
109 self[:my_page_settings] = arg
108 end
110 end
109
111
110 def remove_block(block)
112 def remove_block(block)
111 block = block.to_s.underscore
113 block = block.to_s.underscore
112 %w(top left right).each do |f|
114 %w(top left right).each do |f|
113 (my_page_layout[f] ||= []).delete(block)
115 (my_page_layout[f] ||= []).delete(block)
114 end
116 end
115 my_page_layout
117 my_page_layout
116 end
118 end
117
119
118 def add_block(block)
120 def add_block(block)
119 block = block.to_s.underscore
121 block = block.to_s.underscore
120 return unless Redmine::MyPage.blocks.key?(block)
122 return unless Redmine::MyPage.blocks.key?(block)
121
123
122 remove_block(block)
124 remove_block(block)
123 # add it on top
125 # add it on top
124 my_page_layout['top'] ||= []
126 my_page_layout['top'] ||= []
125 my_page_layout['top'].unshift(block)
127 my_page_layout['top'].unshift(block)
126 end
128 end
127
129
128 def update_block_settings(block, settings)
130 def update_block_settings(block, settings)
129 block_settings = my_page_settings(block).merge(settings.symbolize_keys)
131 block_settings = my_page_settings(block).merge(settings.symbolize_keys)
130 my_page_settings[block] = block_settings
132 my_page_settings[block] = block_settings
131 end
133 end
132 end
134 end
General Comments 0
You need to be logged in to leave comments. Login now