Module: AnnotationsVersionFu::ClassMethods

Defined in:
lib/annotations_version_fu.rb

Instance Method Summary collapse

Instance Method Details

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



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
# File 'lib/annotations_version_fu.rb', line 12

def annotations_version_fu(options={}, &block)
  return if self.included_modules.include? AnnotationsVersionFu::InstanceMethods
  __send__ :include, AnnotationsVersionFu::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 if Annotations::Config.versioning_enabled
  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.set_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?
  
  reload_versioned_columns_info
end

#reload_versioned_columns_infoObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/annotations_version_fu.rb', line 76

def reload_versioned_columns_info
  self.reset_column_information
  self.versioned_class.reset_column_information
  if self.versioned_class.table_exists?
    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



87
88
89
# File 'lib/annotations_version_fu.rb', line 87

def versioned_class
  const_get versioned_class_name
end