Class: Setting::Meta

Inherits:
Object
  • Object
show all
Defined in:
app/models/setting.rb

Constant Summary collapse

@@settings =
{}
@@by_tab_section_and_label =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, options) ⇒ Meta

Returns a new instance of Meta.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/setting.rb', line 55

def initialize(label, options)
  @label = label.to_sym
  @label_override = options[:label]
  @info_label = options[:info_label]
  @tab = (options[:tab] || Setting::Tab.default)
  @section = (options[:section] || Setting::Section.default)
  @order = (options[:order] || 0)
  @type = (options[:type] || :string).to_sym
  @onchange = options[:onchange]
  @validation = options[:validation]
  @select_from = options[:select_from]
  raise "Invalid setting type #{@type}" unless [:text, :string, :symbol, :boolean, :int, :float, :array, :hash].include? @type

  # Auto create general tab and section if it isn't created
  if @tab == :general && !Setting::Tab[@tab]
    Setting::Tab.define :general, :order => 0
  end

  if @section == :general && !Setting::Section.by_tab_and_label[@tab][@section]
    Setting::Section.define :general, :order => 0, :tab => @tab
  end

  if options.include? :default
    @default = options[:default]
  elsif @type == :string
    @default = ""
  elsif @type == :text
    @default = ""
  elsif @type == :symbol
    @default = :""
  elsif @type == :boolean
    @default = false
  elsif @type == :int
    @default = 0
  elsif @type == :float
    @default = 0.0
  elsif @type == :array
    @default = []
  elsif @type == :hash
    @default = {}
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



51
52
53
# File 'app/models/setting.rb', line 51

def default
  @default
end

#info_labelObject (readonly)

Returns the value of attribute info_label.



51
52
53
# File 'app/models/setting.rb', line 51

def info_label
  @info_label
end

#labelObject (readonly)

Returns the value of attribute label.



51
52
53
# File 'app/models/setting.rb', line 51

def label
  @label
end

#onchangeObject (readonly)

Returns the value of attribute onchange.



51
52
53
# File 'app/models/setting.rb', line 51

def onchange
  @onchange
end

#orderObject (readonly)

Returns the value of attribute order.



51
52
53
# File 'app/models/setting.rb', line 51

def order
  @order
end

#sectionObject (readonly)

Returns the value of attribute section.



51
52
53
# File 'app/models/setting.rb', line 51

def section
  @section
end

#select_fromObject (readonly)

Returns the value of attribute select_from.



51
52
53
# File 'app/models/setting.rb', line 51

def select_from
  @select_from
end

#tabObject (readonly)

Returns the value of attribute tab.



51
52
53
# File 'app/models/setting.rb', line 51

def tab
  @tab
end

#typeObject (readonly)

Returns the value of attribute type.



51
52
53
# File 'app/models/setting.rb', line 51

def type
  @type
end

#validationObject (readonly)

Returns the value of attribute validation.



51
52
53
# File 'app/models/setting.rb', line 51

def validation
  @validation
end

Class Method Details

.[](label) ⇒ Object



169
170
171
172
# File 'app/models/setting.rb', line 169

def [](label)
  raise "Missing setting '#{label}'" unless include?(label)
  @@settings[label.to_sym]
end

.[]=(label, definition) ⇒ Object



174
175
176
177
178
179
180
# File 'app/models/setting.rb', line 174

def []=(label, definition)
  raise "Duplicate setting '#{label}' given!" if @@settings[label.to_sym]
  @@settings[label.to_sym] = definition
  @@by_tab_section_and_label[definition.tab] ||= {}
  @@by_tab_section_and_label[definition.tab][definition.section] ||= {}
  @@by_tab_section_and_label[definition.tab][definition.section][label.to_sym] = definition
end

.by_tab_section_and_labelObject



182
183
184
# File 'app/models/setting.rb', line 182

def by_tab_section_and_label
  @@by_tab_section_and_label
end

.include?(label) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/setting.rb', line 165

def include?(label)
  @@settings[label.to_sym].present?
end

Instance Method Details

#<=>(other) ⇒ Object



158
159
160
161
162
# File 'app/models/setting.rb', line 158

def <=>(other)
  result = order <=> other.order
  result = label <=> other.label if result == 0
  result
end

#convert(value) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/setting.rb', line 112

def convert(value)
  if value.nil?
    return default.dup if type == :array || type == :hash
    return default
  end

  case type
  when :string
    value
  when :text
    value
  when :symbol
    value.to_sym
  when :boolean
    value == "true"
  when :int
    value.to_i
  when :float
    value.to_f
  when :array
    YAML.load value
  when :hash
    YAML.load value
  end
end

#convert_to_save(value) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/setting.rb', line 138

def convert_to_save(value)
  return value if value.nil?

  if type == :boolean
    (!!value).to_s
  elsif type == :array && select_from && select_from_options.all? { |x| x.kind_of? Symbol }
    value.each do |x|
      unless select_from_options.map(&:to_s).include? x.to_s
        raise "Invalid value: #{x}"
      end
    end

    value.map(&:to_sym).to_yaml
  elsif type == :array || type == :hash
    value.to_yaml
  else
    value.to_s
  end
end

#localizedObject



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

def localized
  return @label_override.call if @label_override.respond_to? :call
  return @label_override if @label_override
  I18n.t @label, :scope => "settings.show.settings"
end

#select_from_optionsObject



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

def select_from_options
  if select_from.respond_to? :call
    select_from.call
  else
    select_from
  end
end