Class: Setting::Meta
- Inherits:
-
Object
- Object
- Setting::Meta
- Defined in:
- app/models/setting.rb
Constant Summary collapse
- @@settings =
{}
- @@by_tab_section_and_label =
{}
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#info_label ⇒ Object
readonly
Returns the value of attribute info_label.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#onchange ⇒ Object
readonly
Returns the value of attribute onchange.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#section ⇒ Object
readonly
Returns the value of attribute section.
-
#select_from ⇒ Object
readonly
Returns the value of attribute select_from.
-
#tab ⇒ Object
readonly
Returns the value of attribute tab.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
Class Method Summary collapse
- .[](label) ⇒ Object
- .[]=(label, definition) ⇒ Object
- .by_tab_section_and_label ⇒ Object
- .include?(label) ⇒ Boolean
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #convert(value) ⇒ Object
- #convert_to_save(value) ⇒ Object
-
#initialize(label, options) ⇒ Meta
constructor
A new instance of Meta.
- #localized ⇒ Object
- #select_from_options ⇒ Object
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, ) @label = label.to_sym @label_override = [:label] @info_label = [:info_label] @tab = ([:tab] || Setting::Tab.default) @section = ([:section] || Setting::Section.default) @order = ([:order] || 0) @type = ([:type] || :string).to_sym @onchange = [:onchange] @validation = [:validation] @select_from = [: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 .include? :default @default = [: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
#default ⇒ Object (readonly)
Returns the value of attribute default.
51 52 53 |
# File 'app/models/setting.rb', line 51 def default @default end |
#info_label ⇒ Object (readonly)
Returns the value of attribute info_label.
51 52 53 |
# File 'app/models/setting.rb', line 51 def info_label @info_label end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
51 52 53 |
# File 'app/models/setting.rb', line 51 def label @label end |
#onchange ⇒ Object (readonly)
Returns the value of attribute onchange.
51 52 53 |
# File 'app/models/setting.rb', line 51 def onchange @onchange end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
51 52 53 |
# File 'app/models/setting.rb', line 51 def order @order end |
#section ⇒ Object (readonly)
Returns the value of attribute section.
51 52 53 |
# File 'app/models/setting.rb', line 51 def section @section end |
#select_from ⇒ Object (readonly)
Returns the value of attribute select_from.
51 52 53 |
# File 'app/models/setting.rb', line 51 def select_from @select_from end |
#tab ⇒ Object (readonly)
Returns the value of attribute tab.
51 52 53 |
# File 'app/models/setting.rb', line 51 def tab @tab end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
51 52 53 |
# File 'app/models/setting.rb', line 51 def type @type end |
#validation ⇒ Object (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_label ⇒ Object
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
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 && .all? { |x| x.kind_of? Symbol } value.each do |x| unless .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 |
#localized ⇒ Object
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_options ⇒ Object
104 105 106 107 108 109 110 |
# File 'app/models/setting.rb', line 104 def if select_from.respond_to? :call select_from.call else select_from end end |