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
|
# File 'lib/cms/behaviors/versioning.rb', line 11
def is_versioned(options={})
@is_versioned = true
@version_foreign_key = (options[:version_foreign_key] || "#{name.underscore}_id").to_s
@version_table_name = (options[:version_table_name] || "#{table_name.singularize}_versions").to_s
extend ClassMethods
include InstanceMethods
has_many :versions, :class_name => version_class_name, :foreign_key => version_foreign_key
before_validation_on_create :initialize_version
attr_accessor :revert_to_version
versioned_class = self
const_set("Version", Class.new(ActiveRecord::Base)).class_eval do
class << self; attr_accessor :versioned_class end
def versioned_class
self.class.versioned_class
end
def versioned_object_id
send("#{versioned_class.name.underscore}_id")
end
def versioned_object
send(versioned_class.name.underscore.to_sym)
end
named_scope :recent_updates, :order => "updated_at desc", :limit => 10,
:joins => versioned_class.table_name.singularize.to_sym,
:conditions => { "#{versioned_class.table_name}.deleted" => false }
end
version_class.versioned_class = self
version_class.belongs_to(name.underscore.to_sym, :foreign_key => version_foreign_key)
version_class.is_userstamped if userstamped?
end
|