You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_preference.rb 5.1KB

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