Sha256: 38d60eed2579ed2b031a8c43d5830b330b6ad163d8ebfe3f92236babec8aa498

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

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] ), choice] }
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_preferenced-0.9.1 lib/acts_as_preferenced/preference.rb