Module: VersionFu::ClassMethods

Defined in:
lib/version_fu/version_fu.rb

Instance Method Summary collapse

Instance Method Details

#version_fu(options = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/version_fu/version_fu.rb', line 7

def version_fu(options={}, &block)
  return if self.included_modules.include? VersionFu::InstanceMethods
  __send__ :include, VersionFu::InstanceMethods

  cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, 
                 :version_column, :versioned_columns

  self.versioned_class_name         = options[:class_name]  || 'Version'
  self.versioned_foreign_key        = options[:foreign_key] || self.to_s.foreign_key
  self.versioned_table_name         = options[:table_name]  || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
  self.version_column               = options[:version_column]    || 'version'

  # Setup versions association
  class_eval do
    has_many :versions, :class_name  => "#{self.to_s}::#{versioned_class_name}",
                        :foreign_key => versioned_foreign_key,
                        :dependent   => :destroy do
      def latest
        find :first, :order=>'version desc'
      end                    
    end

    before_save :check_for_new_version
  end
  
  # Versioned Model
  const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
    # find first version before the given version
    def self.before(version)
      find :first, :order => 'version desc',
        :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
    end

    # find first version after the given version.
    def self.after(version)
      find :first, :order => 'version',
        :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
    end

    def previous
      self.class.before(self)
    end

    def next
      self.class.after(self)
    end
  end

  # Housekeeping on versioned class
  versioned_class.cattr_accessor :original_class
  versioned_class.original_class = self
  versioned_class.table_name = versioned_table_name
  
  # Version parent association
  versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym, 
    :class_name  => "::#{self.to_s}", 
    :foreign_key => versioned_foreign_key
  
  # Block extension
  versioned_class.class_eval &block if block_given?
  
  if self.versioned_class.table_exists?
    # Finally setup which columns to version
    self.versioned_columns =  versioned_class.new.attributes.keys - 
      [versioned_class.primary_key, versioned_foreign_key, version_column, 'created_at', 'updated_at']
  else
    ActiveRecord::Base.logger.warn "Version Table not found"
  end
end

#versioned_classObject



77
78
79
# File 'lib/version_fu/version_fu.rb', line 77

def versioned_class
  const_get versioned_class_name
end