Class: UserPreference

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/user_preference.rb

Constant Summary collapse

TEXTAREA_FONT_OPTIONS =
['monospace', 'proportional']
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]

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute?, #safe_attribute_names, #safe_attributes=

Constructor Details

#initialize(attributes = nil, *args) ⇒ UserPreference

Returns a new instance of UserPreference.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/user_preference.rb', line 45

def initialize(attributes=nil, *args)
  super
  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 = Setting.default_users_no_self_notified
    end
  end
  self.others ||= {}
end

Instance Method Details

#[](attr_name) ⇒ Object


65
66
67
68
69
70
71
# File 'app/models/user_preference.rb', line 65

def [](attr_name)
  if has_attribute? attr_name
    super
  else
    others ? others[attr_name] : nil
  end
end

#[]=(attr_name, value) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'app/models/user_preference.rb', line 73

def []=(attr_name, value)
  if has_attribute? attr_name
    super
  else
    h = (read_attribute(:others) || {}).dup
    h.update(attr_name => value)
    write_attribute(:others, h)
    value
  end
end

#activity_scopeObject


96
# File 'app/models/user_preference.rb', line 96

def activity_scope; Array(self[:activity_scope]); end

#activity_scope=(value) ⇒ Object


97
# File 'app/models/user_preference.rb', line 97

def activity_scope=(value); self[:activity_scope]=value; end

#add_block(block) ⇒ Object

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


162
163
164
165
166
167
168
169
170
171
# File 'app/models/user_preference.rb', line 162

def add_block(block)
  block = block.to_s.underscore
  return unless Redmine::MyPage.valid_block?(block, my_page_layout.values.flatten)

  remove_block(block)
  # add it to the first group
  group = my_page_groups.first
  my_page_layout[group] ||= []
  my_page_layout[group].unshift(block)
end

#comments_sortingObject


84
# File 'app/models/user_preference.rb', line 84

def comments_sorting; self[:comments_sorting]; end

#comments_sorting=(order) ⇒ Object


85
# File 'app/models/user_preference.rb', line 85

def comments_sorting=(order); self[:comments_sorting]=order; end

#history_default_tabObject


104
# File 'app/models/user_preference.rb', line 104

def history_default_tab; self[:history_default_tab]; end

#history_default_tab=(value) ⇒ Object


105
# File 'app/models/user_preference.rb', line 105

def history_default_tab=(value); self[:history_default_tab]=value; end

#my_page_groupsObject

Returns the names of groups that are displayed on user's page Example:

preferences.my_page_groups
# => ['top', 'left, 'right']

123
124
125
# File 'app/models/user_preference.rb', line 123

def my_page_groups
  Redmine::MyPage.groups
end

#my_page_layoutObject


127
128
129
# File 'app/models/user_preference.rb', line 127

def my_page_layout
  self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
end

#my_page_layout=(arg) ⇒ Object


131
132
133
# File 'app/models/user_preference.rb', line 131

def my_page_layout=(arg)
  self[:my_page_layout] = arg
end

#my_page_settings(block = nil) ⇒ Object


135
136
137
138
139
140
141
142
# File 'app/models/user_preference.rb', line 135

def my_page_settings(block=nil)
  s = self[:my_page_settings] ||= {}
  if block
    s[block] ||= {}
  else
    s
  end
end

#my_page_settings=(arg) ⇒ Object


144
145
146
# File 'app/models/user_preference.rb', line 144

def my_page_settings=(arg)
  self[:my_page_settings] = arg
end

#no_self_notifiedObject


90
# File 'app/models/user_preference.rb', line 90

def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end

#no_self_notified=(value) ⇒ Object


91
# File 'app/models/user_preference.rb', line 91

def no_self_notified=(value); self[:no_self_notified]=value; end

#notify_about_high_priority_issuesObject


93
# File 'app/models/user_preference.rb', line 93

def notify_about_high_priority_issues; (self[:notify_about_high_priority_issues] == true || self[:notify_about_high_priority_issues] == '1'); end

#notify_about_high_priority_issues=(value) ⇒ Object


94
# File 'app/models/user_preference.rb', line 94

def notify_about_high_priority_issues=(value); self[:notify_about_high_priority_issues]=value; end

#order_blocks(group, blocks) ⇒ Object

Sets the block order for the given group. Example:

preferences.order_blocks('left', ['issueswatched', 'news'])

176
177
178
179
180
181
182
183
# File 'app/models/user_preference.rb', line 176

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
end

#recently_used_projectsObject


102
# File 'app/models/user_preference.rb', line 102

def recently_used_projects; (self[:recently_used_projects] || 3).to_i; end

#recently_used_projects=(value) ⇒ Object


103
# File 'app/models/user_preference.rb', line 103

def recently_used_projects=(value); self[:recently_used_projects] = value.to_i; end

#remove_block(block) ⇒ Object

Removes block from the user page layout Example:

preferences.remove_block('news')

151
152
153
154
155
156
157
# File 'app/models/user_preference.rb', line 151

def remove_block(block)
  block = block.to_s.underscore
  my_page_layout.each_key do |group|
    my_page_layout[group].delete(block)
  end
  my_page_layout
end

#set_others_hashObject


61
62
63
# File 'app/models/user_preference.rb', line 61

def set_others_hash
  self.others ||= {}
end

#textarea_fontObject


99
# File 'app/models/user_preference.rb', line 99

def textarea_font; self[:textarea_font]; end

#textarea_font=(value) ⇒ Object


100
# File 'app/models/user_preference.rb', line 100

def textarea_font=(value); self[:textarea_font]=value; end

#toolbar_language_optionsObject


107
108
109
# File 'app/models/user_preference.rb', line 107

def toolbar_language_options
  self[:toolbar_language_options].presence || DEFAULT_TOOLBAR_LANGUAGE_OPTIONS.join(',')
end

#toolbar_language_options=(value) ⇒ Object


111
112
113
114
115
116
117
# File 'app/models/user_preference.rb', line 111

def toolbar_language_options=(value)
  languages =
    value.to_s.delete(' ').split(',').select do |lang|
      Redmine::SyntaxHighlighting.language_supported?(lang)
    end.compact
  self[:toolbar_language_options] = languages.join(',')
end

#update_block_settings(block, settings) ⇒ Object


185
186
187
188
189
# File 'app/models/user_preference.rb', line 185

def update_block_settings(block, settings)
  block = block.to_s
  block_settings = my_page_settings(block).merge(settings.symbolize_keys)
  my_page_settings[block] = block_settings
end

#warn_on_leaving_unsavedObject


87
# File 'app/models/user_preference.rb', line 87

def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end

#warn_on_leaving_unsaved=(value) ⇒ Object


88
# File 'app/models/user_preference.rb', line 88

def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end