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.



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
97
98
# File 'app/models/setting.rb', line 57

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.



53
54
55
# File 'app/models/setting.rb', line 53

def default
  @default
end

#info_labelObject (readonly)

Returns the value of attribute info_label.



53
54
55
# File 'app/models/setting.rb', line 53

def info_label
  @info_label
end

#labelObject (readonly)

Returns the value of attribute label.



53
54
55
# File 'app/models/setting.rb', line 53

def label
  @label
end

#onchangeObject (readonly)

Returns the value of attribute onchange.



53
54
55
# File 'app/models/setting.rb', line 53

def onchange
  @onchange
end

#orderObject (readonly)

Returns the value of attribute order.



53
54
55
# File 'app/models/setting.rb', line 53

def order
  @order
end

#sectionObject (readonly)

Returns the value of attribute section.



53
54
55
# File 'app/models/setting.rb', line 53

def section
  @section
end

#select_fromObject (readonly)

Returns the value of attribute select_from.



53
54
55
# File 'app/models/setting.rb', line 53

def select_from
  @select_from
end

#tabObject (readonly)

Returns the value of attribute tab.



53
54
55
# File 'app/models/setting.rb', line 53

def tab
  @tab
end

#typeObject (readonly)

Returns the value of attribute type.



53
54
55
# File 'app/models/setting.rb', line 53

def type
  @type
end

#validationObject (readonly)

Returns the value of attribute validation.



53
54
55
# File 'app/models/setting.rb', line 53

def validation
  @validation
end

Class Method Details

.[](label) ⇒ Object



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

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

.[]=(label, definition) ⇒ Object



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

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



172
173
174
# File 'app/models/setting.rb', line 172

def by_tab_section_and_label
  @@by_tab_section_and_label
end

Instance Method Details

#<=>(other) ⇒ Object



152
153
154
155
156
# File 'app/models/setting.rb', line 152

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

#convert(value) ⇒ Object



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

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



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

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



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

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



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

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