Sha256: 16e7609a16c97d937470ef104cd3df3c10e664adbc61a6bec7624926e6d9a3f2

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

require 'acts_as_preferenced/version'
require 'acts_as_preferenced/store/base'
require 'acts_as_preferenced/store/association'
require 'acts_as_preferenced/store/field'
require 'acts_as_preferenced/preference'
require 'acts_as_preferenced/section'

module ActsAsPreferenced

  def self.included( klass )
    klass.extend( ActMethods )
  end

  module ActMethods

    def acts_as_preferenced( options = {} )
      options.assert_valid_keys( :field, :association, :translation_scope )
      class_attribute :preference_config
      include InstanceMethods
      extend ClassMethods
      if options[:field]
        field = options[:field]
        self.module_eval { serialize field, Hash } # TODO: move this line into config initialization
        config = ActsAsPreferenced::Store::Field.new( self, field, options[:translation_scope] )
        self.preference_config = config
        yield config
      elsif options[:association]
        config = ActsAsPreferenced::Store::Association.new( self, options[:association], options[:translation_scope] )
        self.preference_config = config
        yield config
      else
        raise ArgumentError, "Invalid options for acts_as_preferenced: #{options.inspect}"
      end
    end

  end

  module ClassMethods

    # TODO: Those methods should only be available when preferences are actually defined
    def preference_sections
      preference_config.sections.keys
    end

    def preferences_for_section( section )
      preference_config.sections[section]
    end

    def all_preferences
      preference_config.all_preferences
    end

  end

  module InstanceMethods

    def preference_value( name )
      # TODO: implement
    end

    #def preferences
    #  Preferences.configs[self.class].all
    #end

    #def preferences= (prefs)
    #  Preferences.configs[self.class].all = prefs
    #end
  end

end

ActiveRecord::Base.send( :include, ActsAsPreferenced )

Version data entries

2 entries across 2 versions & 1 rubygems

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