Class: Vario::Setting

Inherits:
ApplicationRecord show all
Defined in:
app/models/vario/setting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'app/models/vario/setting.rb', line 5

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'app/models/vario/setting.rb', line 5

def description
  @description
end

#settable_settingObject (readonly)

Returns the value of attribute settable_setting.



5
6
7
# File 'app/models/vario/setting.rb', line 5

def settable_setting
  @settable_setting
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'app/models/vario/setting.rb', line 5

def type
  @type
end

Instance Method Details

#collectionObject



152
153
154
155
156
157
158
159
160
# File 'app/models/vario/setting.rb', line 152

def collection
  return @collection if @collection
  return unless settable_setting

  @collection = settable_setting[:collection]
  @collection ||= instance_exec(&settable_setting[:collection_proc]) if settable_setting[:collection_proc]
  @collection ||= []
  @collection
end

#configureObject



66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/vario/setting.rb', line 66

def configure
  self.keys ||= []

  @settable_setting = Vario.config.settable_setting(settable_type, name)
  return unless settable_setting

  @type = settable_setting[:type]
  self.keys = settable_setting[:keys]
  @default = settable_setting[:default]
  @description = settable_setting[:description]
end

#group_nameObject



19
20
21
22
23
24
25
26
27
# File 'app/models/vario/setting.rb', line 19

def group_name
  return nil if name.blank?

  parts = name.split('.', 2)

  return nil if parts.size == 1

  parts.first
end

#human_value(value) ⇒ Object



133
134
135
136
137
138
139
# File 'app/models/vario/setting.rb', line 133

def human_value(value)
  parsed_value = parse_value(value)
  return parsed_value if collection.blank?
  return parsed_value.map { |pv| collection.find { |i| i.last == pv }.first } if type == :array

  collection.find { |i| i.last == parsed_value }.first
end

#initialized?Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'app/models/vario/setting.rb', line 116

def initialized?
  return false if Vario.config.raise_on_undefined?(settable) && !persisted?
  settable_setting.present? || persisted?
end

#levelsObject



78
79
80
# File 'app/models/vario/setting.rb', line 78

def levels
  @levels ||= (attributes['levels'] || []).map { |level| Level.new(self, level) }
end

#move_level_down(level) ⇒ Object



110
111
112
113
114
# File 'app/models/vario/setting.rb', line 110

def move_level_down(level)
  index = levels.index(level)
  level = levels.delete(level)
  levels.insert(index + 1, level)
end

#move_level_up(level) ⇒ Object



104
105
106
107
108
# File 'app/models/vario/setting.rb', line 104

def move_level_up(level)
  index = levels.index(level)
  level = levels.delete(level)
  levels.insert(index - 1, level)
end

#new_levelObject



162
163
164
# File 'app/models/vario/setting.rb', line 162

def new_level
  Level.new(self, {}, true)
end

#parse_value(value) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/vario/setting.rb', line 121

def parse_value(value)
  return false if [0, '0', 'false', '', false].include?(value) && type == :boolean
  return true if [1, '1', 'true', true].include?(value) && type == :boolean
  return unless value.present?
  return parse_value_array(value) if type == :array
  return value unless value.is_a?(String)
  return YAML.load(value) if type == :hash
  return value.to_i if type == :integer

  value
end

#parse_value_array(value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'app/models/vario/setting.rb', line 141

def parse_value_array(value)
  values = value
  values = values.split(/,|\n/).map(&:strip) if values.is_a?(String)
  values = values.reject(&:blank?)

  # If a collection has been defined, and 'all' is in the selected values, return all id's from the colletion
  return collection.map(&:last) if (values.include?('all') || values.include?(:all)) && collection

  values
end

#setting_nameObject



29
30
31
32
33
# File 'app/models/vario/setting.rb', line 29

def setting_name
  return name if name.blank?

  name.split('.', 2).last || name
end

#titleObject



35
36
37
38
39
# File 'app/models/vario/setting.rb', line 35

def title
  return "#{group_name.humanize}: #{setting_name.humanize}" if group_name.present?

  setting_name.humanize if setting_name.present?
end

#uniq_levelsObject



82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/vario/setting.rb', line 82

def uniq_levels
  selected = []

  levels.each do |level|
    next if selected.select { |uniq_level| uniq_level.conditions_hash == level.conditions_hash}.present?

    selected << level
  end

  selected
end

#uniq_levels!Object



94
95
96
97
98
# File 'app/models/vario/setting.rb', line 94

def uniq_levels!
  self.levels = uniq_levels.select { |l| l.value_present? }.map(&:to_h)
  @levels = nil
  save!
end

#update_level_dataObject



100
101
102
# File 'app/models/vario/setting.rb', line 100

def update_level_data
  self.levels = levels.select { |l| l.value_present? }.map(&:to_h)
end

#value_for(context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/vario/setting.rb', line 41

def value_for(context)
  current_default = context.delete(:default)

  unless initialized?
    raise UnknownSetting, "Configuration key '#{name}' not initialized for #{settable_type}" if Vario.config.raise_on_undefined?(settable_type)
    return current_default unless Vario.config.create_on_request?(settable_type)

    # This is a new record, save it to the table since create_on_request? is true
    self.keys = Vario.config.default_keys?(settable_type)
    self.levels << Vario::Level.new(self, conditions: {}, value: current_default)
  end

  context = context.symbolize_keys
  update_context(context)
  validate_context(context)

  result = levels.find do |level|
    context >= level.conditions_hash
  end

  return parse_value(result.value) if result.present? && result.value_present?

  parse_value(current_default || default)
end