module ActsAsPreferenced class Preference attr_reader :name, :default, :section def initialize( store, name, options = {} ) options.assert_valid_keys( :section, :label, :choices, :default ) @store, @name = store, name @section = options[:section] @label = options[:label] @choices = options[:choices] @default = options[:default] store.sections[@section] << self store.all_preferences << self end def label if @store.translation_scope && @label.nil? I18n.t( @name, :scope => [@store.translation_scope, @section] ) else @label end end # Options translations are stored under hardcoded :choices key in Preferences # translation_scope. TODO: make it not hardcoded and make it work for other # options than Array (eg. Range?) # TODO: it should be cached def choices if @choices.is_a?( Array ) # TODO: I don't quite understand checking for an array here @choices.collect { |choice| choice.is_a?( Array ) ? choice : [I18n.t( choice, :scope => [@store.translation_scope, :choices] ), opt] } else @choices end end def value_valid?( value ) value = value.to_sym if value.is_a?( String ) if @choices return @choices.include?( value ) else return [true, false].include?( value ) end end end end