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 4.7KB

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