Module: VersionIncludes

Defined in:
lib/has-versions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/has-versions.rb', line 24

def self.included(base)
  base.class_eval do
    
    # has_many :histories, :class_name => self.name, :primary_key => :uid, :foreign_key => :uid, :conditions => {:state => 'history' }
  
    # has_one :draft, :class_name => self.name, :primary_key => :uid, :foreign_key => :uid, :conditions => {:state => 'draft' }
  
    # has_one :publication, :class_name => self.name, :primary_key => :uid, :foreign_key => :uid, :conditions => {:state => 'publication' }
  
    scope :uid, lambda {|uid| where(:uid =>uid) }
    scope :publication, where(:state=>'publication')
    scope :draft, where(:state=>'draft')
    scope :with_version, lambda {|version| self.where("version <= '#{version}'") }
    
    before_create 'before_create_initialization'
    after_create 'after_create_initialization'

  end
end

Instance Method Details

#after_create_initializationObject



61
62
63
64
65
66
# File 'lib/has-versions.rb', line 61

def after_create_initialization
  if self.uid.nil?
    self.uid = id
    self.save
  end
end

#before_create_initializationObject



56
57
58
59
# File 'lib/has-versions.rb', line 56

def before_create_initialization
  self.state = 'draft'
  self.version = nil
end

#commitable?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/has-versions.rb', line 128

def commitable?
  draft? && (self.created_at.to_i!=self.updated_at.to_i)
end

#create_draft!Object



108
109
110
111
112
113
114
# File 'lib/has-versions.rb', line 108

def create_draft!
  draft = self.clone
  draft.state = 'draft'
  draft.created_at = Time.now
  draft.updated_at = Time.now
  draft.save
end

#draftObject



47
48
49
# File 'lib/has-versions.rb', line 47

def draft
  self.class.where(:uid=>uid).where(:state=>'draft').all.first
end

#draft!Object



68
69
70
71
72
# File 'lib/has-versions.rb', line 68

def draft!
  self.state = 'draft'
  self.version = nil
  self.save
end

#draft?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/has-versions.rb', line 116

def draft?
  state.eql?('draft')
end

#historiesObject



44
45
46
# File 'lib/has-versions.rb', line 44

def histories
  self.class.where(:uid=>uid).where(:state=>'history').order('version DESC').all
end

#historize!Object



101
102
103
104
105
106
# File 'lib/has-versions.rb', line 101

def historize!
  if publication?
    self.state = 'history'
    self.save
  end
end

#history?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/has-versions.rb', line 124

def history?
  state.eql?('history')
end

#publicationObject



50
51
52
# File 'lib/has-versions.rb', line 50

def publication
  self.class.where(:uid=>uid).where(:state=>'publication').all.first
end

#publication?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/has-versions.rb', line 120

def publication?
  state.eql?('publication')
end

#publish!(version) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/has-versions.rb', line 74

def publish!(version)
  if draft?
    return nil if publication && publication.version >= version
    publication.historize! if publication
    self.state = 'publication'
    self.version = version
    self.save
    create_draft!
  end
end

#restore!Object



92
93
94
95
96
97
# File 'lib/has-versions.rb', line 92

def restore!
  if history? || publication?
    draft.destroy if draft
    create_draft!
  end
end

#revert!Object



85
86
87
88
89
90
# File 'lib/has-versions.rb', line 85

def revert!
  if draft? && publication
    draft = publication.create_draft!
    self.destroy
  end
end