Module: Prefered::ClassMethods

Defined in:
lib/prefered.rb

Instance Method Summary collapse

Instance Method Details

#has_preferences(&block) ⇒ Object Also known as: has_settings



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/prefered.rb', line 11

def has_preferences(&block)
  has_one :preferences, :class_name => "Prefered::Preference", :as => :owner, :autosave => true, :dependent => :destroy
  alias_method :settings, :preferences
  after_initialize do
    build_preferences(:owner => self) unless preferences
    # would prefer to pass block straight to preference model through delegation, however rails assoication_proxy later
    # breaks instance_eval for the block, and the block binding becomes broken, so we have to 'pass' the block through
    # this instance variable
    preferences.instance_variable_set(:@preference_settings_block, block)
    preferences.init_preferences
  end
end