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



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

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

.[]=(label, definition) ⇒ Object



162
163
164
165
166
167
168
# File 'app/models/setting.rb', line 162

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



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

def by_tab_section_and_label
  @@by_tab_section_and_label
end

Instance Method Details

#<=>(other) ⇒ Object



150
151
152
153
154
# File 'app/models/setting.rb', line 150

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
# 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 || 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