Class: ThemeSettingsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_settings_manager.rb

Direct Known Subclasses

Bool, Enum, Float, Integer, List, String, Upload

Defined Under Namespace

Classes: Bool, Enum, Float, Integer, List, String, Upload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, theme, opts = {}) ⇒ ThemeSettingsManager

Returns a new instance of ThemeSettingsManager.



16
17
18
19
20
21
22
# File 'lib/theme_settings_manager.rb', line 16

def initialize(name, default, theme, opts = {})
  @name = name.to_sym
  @default = default
  @theme = theme
  @opts = opts
  @types = self.class.types
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/theme_settings_manager.rb', line 4

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/theme_settings_manager.rb', line 4

def name
  @name
end

#themeObject (readonly)

Returns the value of attribute theme.



4
5
6
# File 'lib/theme_settings_manager.rb', line 4

def theme
  @theme
end

Class Method Details

.create(name, default, type, theme, opts = {}) ⇒ Object



10
11
12
13
14
# File 'lib/theme_settings_manager.rb', line 10

def self.create(name, default, type, theme, opts = {})
  type_name = self.types.invert[type].downcase.capitalize
  klass = "ThemeSettingsManager::#{type_name}".constantize
  klass.new(name, default, theme, opts)
end

.typesObject



6
7
8
# File 'lib/theme_settings_manager.rb', line 6

def self.types
  ThemeSetting.types
end

Instance Method Details

#create_record!Object



65
66
67
68
69
# File 'lib/theme_settings_manager.rb', line 65

def create_record!
  record = ThemeSetting.new(name: @name, data_type: type, theme: @theme)
  record.save!
  record
end

#db_recordObject



53
54
55
56
57
58
59
# File 'lib/theme_settings_manager.rb', line 53

def db_record
  # theme.theme_settings will already be preloaded, so it is better to use
  # `find` on an array, rather than make a round trip to the database
  theme.theme_settings.to_a.find do |i|
    i.name.to_s == @name.to_s && i.data_type.to_s == type.to_s
  end
end

#descriptionObject



36
37
38
# File 'lib/theme_settings_manager.rb', line 36

def description
  @opts[:description] # Old method of specifying description. Is now overridden by locale file
end

#ensure_is_valid_value!(new_value) ⇒ Object



90
91
92
93
94
# File 'lib/theme_settings_manager.rb', line 90

def ensure_is_valid_value!(new_value)
  unless is_valid_value?(new_value)
    raise Discourse::InvalidParameters.new invalid_value_error_message
  end
end

#has_max?Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/theme_settings_manager.rb', line 101

def has_max?
  max = @opts[:max]
  (max.is_a?(::Integer) || max.is_a?(::Float)) && max != ::Float::INFINITY
end

#has_min?Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/theme_settings_manager.rb', line 96

def has_min?
  min = @opts[:min]
  (min.is_a?(::Integer) || min.is_a?(::Float)) && min != -::Float::INFINITY
end

#has_record?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/theme_settings_manager.rb', line 61

def has_record?
  db_record.present?
end

#invalid_value_error_messageObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/theme_settings_manager.rb', line 75

def invalid_value_error_message
  name = type == @types[:integer] || type == @types[:float] ? "number" : type_name
  primary_key = "themes.settings_errors.#{name}_value_not_valid"

  secondary_key = primary_key
  secondary_key += "_min" if has_min?
  secondary_key += "_max" if has_max?

  translation = I18n.t(primary_key)
  return translation if secondary_key == primary_key

  translation += " #{I18n.t(secondary_key, min: @opts[:min], max: @opts[:max])}"
  translation
end

#is_valid_value?(new_value) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/theme_settings_manager.rb', line 71

def is_valid_value?(new_value)
  true
end

#requests_refresh?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/theme_settings_manager.rb', line 40

def requests_refresh?
  @opts[:refresh]
end

#typeObject



32
33
34
# File 'lib/theme_settings_manager.rb', line 32

def type
  @types[type_name]
end

#type_nameObject



28
29
30
# File 'lib/theme_settings_manager.rb', line 28

def type_name
  self.class.name.demodulize.downcase.to_sym
end

#valueObject



24
25
26
# File 'lib/theme_settings_manager.rb', line 24

def value
  has_record? ? db_record.value : default
end

#value=(new_value) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/theme_settings_manager.rb', line 44

def value=(new_value)
  ensure_is_valid_value!(new_value)

  record = has_record? ? db_record : create_record!
  record.value = new_value.to_s
  record.save!
  record.value
end