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.



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

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



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

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

#[]=(attr_name, value) ⇒ Object



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

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



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

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

#activity_scope=(value) ⇒ Object



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

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



170
171
172
173
174
175
176
177
178
179
# File 'app/models/user_preference.rb', line 170

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



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

def comments_sorting; self[:comments_sorting]; end

#comments_sorting=(order) ⇒ Object



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

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

#default_issue_queryObject



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

def default_issue_query; self[:default_issue_query] end

#default_issue_query=(value) ⇒ Object



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

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

#default_project_queryObject



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

def default_project_query; self[:default_project_query] end

#default_project_query=(value) ⇒ Object



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

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

#history_default_tabObject



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

def history_default_tab; self[:history_default_tab]; end

#history_default_tab=(value) ⇒ Object



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

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']


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

def my_page_groups
  Redmine::MyPage.groups
end

#my_page_layoutObject



135
136
137
# File 'app/models/user_preference.rb', line 135

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

#my_page_layout=(arg) ⇒ Object



139
140
141
# File 'app/models/user_preference.rb', line 139

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

#my_page_settings(block = nil) ⇒ Object



143
144
145
146
147
148
149
150
# File 'app/models/user_preference.rb', line 143

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

#my_page_settings=(arg) ⇒ Object



152
153
154
# File 'app/models/user_preference.rb', line 152

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

#no_self_notifiedObject



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

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

#no_self_notified=(value) ⇒ Object



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

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

#notify_about_high_priority_issuesObject



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

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



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

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'])


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

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



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

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

#recently_used_projects=(value) ⇒ Object



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

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')


159
160
161
162
163
164
165
# File 'app/models/user_preference.rb', line 159

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



63
64
65
# File 'app/models/user_preference.rb', line 63

def set_others_hash
  self.others ||= {}
end

#textarea_fontObject



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

def textarea_font; self[:textarea_font]; end

#textarea_font=(value) ⇒ Object



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

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

#toolbar_language_optionsObject



109
110
111
# File 'app/models/user_preference.rb', line 109

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

#toolbar_language_options=(value) ⇒ Object



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

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



193
194
195
196
197
# File 'app/models/user_preference.rb', line 193

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



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

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

#warn_on_leaving_unsaved=(value) ⇒ Object



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

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