Class: FinePrint::Contract

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/fine_print/contract.rb

Instance Method Summary collapse

Instance Method Details

#can_be_published?Boolean



70
71
72
# File 'app/models/fine_print/contract.rb', line 70

def can_be_published?
  signatures.empty? && !is_published?
end

#can_be_unpublished?Boolean



74
75
76
# File 'app/models/fine_print/contract.rb', line 74

def can_be_unpublished?
  signatures.empty? && is_latest?
end

#can_be_updated?Boolean Also known as: can_be_destroyed?

Access Control #



66
67
68
# File 'app/models/fine_print/contract.rb', line 66

def can_be_updated?
  signatures.empty?
end

#draft_copyObject



58
59
60
# File 'app/models/fine_print/contract.rb', line 58

def draft_copy
  Contract.where(:name => name, :version => nil).first || dup.tap{|contract| contract.version = nil}
end

#is_latest?Boolean



28
29
30
# File 'app/models/fine_print/contract.rb', line 28

def is_latest?
  is_published? && version == same_name.maximum(:version)
end

#is_published?Boolean



24
25
26
# File 'app/models/fine_print/contract.rb', line 24

def is_published?
  !version.nil?
end

#no_signaturesObject

Validation #



84
85
86
87
88
# File 'app/models/fine_print/contract.rb', line 84

def no_signatures
  return if signatures.empty?
  errors.add(:base, 'Contract cannot be modified because users have signed it')
  false
end

#publishObject



40
41
42
43
44
45
46
47
# File 'app/models/fine_print/contract.rb', line 40

def publish
  no_signatures
  errors.add(:base, 'Contract is already published') if is_published?
  return false unless errors.empty?

  self.version = (same_name.published.first.try(:version) || 0) + 1
  save
end

#sign(user) ⇒ Object



32
33
34
# File 'app/models/fine_print/contract.rb', line 32

def sign(user)
  FinePrint.sign_contract(self, user)
end

#signed_by?(user) ⇒ Boolean



36
37
38
# File 'app/models/fine_print/contract.rb', line 36

def signed_by?(user)
  FinePrint.signed_contract?(self, user)
end

#unpublishObject



49
50
51
52
53
54
55
56
# File 'app/models/fine_print/contract.rb', line 49

def unpublish
  no_signatures
  errors.add(:base, 'Contract is not the latest published version') unless is_latest?
  return false unless errors.empty?

  self.version = nil
  save
end