Class: UserPreference

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

Overview

Redmine - project management software Copyright (C) 2006-2017 Jean-Philippe Lang

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.

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.

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.

Since:

  • 0.4.0

Constant Summary collapse

TEXTAREA_FONT_OPTIONS =
['monospace', 'proportional']

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

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

Constructor Details

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

Returns a new instance of UserPreference



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/user_preference.rb', line 37

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 = true
    end
  end
  self.others ||= {}
end

Instance Method Details

#[](attr_name) ⇒ Object



57
58
59
60
61
62
63
# File 'app/models/user_preference.rb', line 57

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

#[]=(attr_name, value) ⇒ Object



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

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

Since:

  • 3.2.0



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

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

#activity_scope=(value) ⇒ Object

Since:

  • 3.2.0



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

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

Since:

  • 3.4.0



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

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

Since:

  • 0.7.0



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

def comments_sorting; self[:comments_sorting] end

#comments_sorting=(order) ⇒ Object

Since:

  • 0.7.0



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

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

#my_page_groupsObject

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

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

Since:

  • 3.4.0



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

def my_page_groups
  Redmine::MyPage.groups
end

#my_page_layoutObject

Since:

  • 3.4.0



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

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

#my_page_layout=(arg) ⇒ Object

Since:

  • 3.4.0



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

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

#my_page_settings(block = nil) ⇒ Object

Since:

  • 3.4.0



107
108
109
110
111
112
113
114
# File 'app/models/user_preference.rb', line 107

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

#my_page_settings=(arg) ⇒ Object

Since:

  • 3.4.0



116
117
118
# File 'app/models/user_preference.rb', line 116

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

#no_self_notifiedObject

Since:

  • 2.3.2



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

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

#no_self_notified=(value) ⇒ Object

Since:

  • 2.3.2



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

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

#order_blocks(group, blocks) ⇒ Object

Sets the block order for the given group. Example:

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

Since:

  • 3.4.0



148
149
150
151
152
153
154
155
# File 'app/models/user_preference.rb', line 148

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

#remove_block(block) ⇒ Object

Removes block from the user page layout Example:

preferences.remove_block('news')

Since:

  • 3.4.0



123
124
125
126
127
128
129
# File 'app/models/user_preference.rb', line 123

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

#set_others_hashObject

Since:

  • 1.4.0



53
54
55
# File 'app/models/user_preference.rb', line 53

def set_others_hash
  self.others ||= {}
end

#textarea_fontObject

Since:

  • 3.4.0



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

def textarea_font; self[:textarea_font] end

#textarea_font=(value) ⇒ Object

Since:

  • 3.4.0



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

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

#update_block_settings(block, settings) ⇒ Object

Since:

  • 3.4.0



157
158
159
160
161
# File 'app/models/user_preference.rb', line 157

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

Since:

  • 1.2.0



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

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

#warn_on_leaving_unsaved=(value) ⇒ Object

Since:

  • 1.2.0



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

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