File: lib/plugins/active_record_extensions/lib/active_record_extensions.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveRecord
  class: Base#3
inherits from
  Object ( Builtin-Module )
has properties
class method: validates_path / 1 #5
class method: object_id_attr / 2 #13

Class Hierarchy

Object ( Builtin-Module )
  Base ( ActiveRecord ) #3

Code

   1  require 'active_record'
   2 
   3  class ActiveRecord::Base
   4    
   5    def self.validates_path(*args)
   6      configuration = args.extract_options!
   7      validates_each(args, configuration) do |record, attr_name, value|
   8        page = Page.find_by_path(value)
   9        record.errors.add(attr_name, :page_not_found, :default => configuration[:message]) if page.nil? || page.is_a?(FileNotFoundPage)
  10      end
  11    end
  12    
  13    def self.object_id_attr(symbol, klass)
  14      module_eval %{
  15        def #{symbol}
  16          if @#{symbol}.nil? or (@old_#{symbol}_id != #{symbol}_id)
  17            @old_#{symbol}_id = #{symbol}_id
  18            klass = #{klass}.descendants.find { |d| d.#{symbol}_name == #{symbol}_id }
  19            klass ||= #{klass}
  20            @#{symbol} = klass.new
  21          else
  22            @#{symbol}
  23          end
  24        end
  25      }
  26    end
  27    
  28  end