Class: ActiveRecord::Base

Inherits:
Object show all
Defined in:
lib/plugins/active_record_extensions/lib/active_record_extensions.rb

Direct Known Subclasses

Layout, Page, PagePart, Radiant::Config, Snippet, User

Class Method Summary collapse

Class Method Details

.object_id_attr(symbol, klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/plugins/active_record_extensions/lib/active_record_extensions.rb', line 4

def self.object_id_attr(symbol, klass)
  module_eval %{
    def #{symbol}
      if @#{symbol}.nil? or (@old_#{symbol}_id != #{symbol}_id)
        @old_#{symbol}_id = #{symbol}_id
        klass = #{klass}.descendants.find { |d| d.#{symbol}_name == #{symbol}_id }
        klass ||= #{klass}
        @#{symbol} = klass.new
      else
        @#{symbol}
      end
    end
  }
end