Class: Setting::Tab

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

Constant Summary collapse

@@all =
[]
@@by_label =
{}
@@default =
:general

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, options = {}) ⇒ Tab

Returns a new instance of Tab.



194
195
196
197
198
199
# File 'app/models/setting.rb', line 194

def initialize(label, options = {})
  @label = label.to_sym
  @label_override = options[:label]
  @order = options[:order] || 1
  Setting::Section.by_tab_and_label[@label] ||= {}
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



189
190
191
# File 'app/models/setting.rb', line 189

def label
  @label
end

#orderObject (readonly)

Returns the value of attribute order.



189
190
191
# File 'app/models/setting.rb', line 189

def order
  @order
end

Class Method Details

.[](label) ⇒ Object



224
225
226
# File 'app/models/setting.rb', line 224

def [](label)
  @@by_label[label.to_sym]
end

.allObject



228
229
230
# File 'app/models/setting.rb', line 228

def all
  @@all
end

.defaultObject



232
233
234
# File 'app/models/setting.rb', line 232

def default
  @@default
end

.define(label, options = {}) ⇒ Object



247
248
249
250
251
252
253
254
255
256
# File 'app/models/setting.rb', line 247

def define(label, options = {})
  raise "Duplicate tab '#{label}' being defined" if @@by_label.include? label.to_sym
  tab = Setting::Tab.new label, options
  @@all << tab
  @@by_label[label.to_sym] = tab

  with_default_tab label do
    yield if block_given?
  end
end

.with_default_tab(label) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'app/models/setting.rb', line 236

def with_default_tab(label)
  original = @@default

  begin
    @@default = label.to_sym
    yield
  ensure
    @@default = original
  end
end

Instance Method Details

#<=>(other) ⇒ Object



217
218
219
220
221
# File 'app/models/setting.rb', line 217

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

#[](section_label) ⇒ Object



212
213
214
215
# File 'app/models/setting.rb', line 212

def []()
  Setting::Section.by_tab_and_label[label] ||= {}
  Setting::Section.by_tab_and_label[label][.to_sym]
end

#localizedObject



201
202
203
204
205
# File 'app/models/setting.rb', line 201

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

#sectionsObject



207
208
209
210
# File 'app/models/setting.rb', line 207

def sections
  Setting::Section.by_tab_and_label[label] ||= {}
  Setting::Section.by_tab_and_label[label].values.sort.map &:label
end