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