Class: ActiveVersioning::Model::VersionProxy

Inherits:
Delegator
  • Object
show all
Defined in:
lib/active_versioning/model/version_proxy.rb

Overview

‘class` should delegate to versioned resource, not return ActiveVersioning::Model::VersionProxy

Constant Summary collapse

VersionInvalid =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ VersionProxy

Returns a new instance of VersionProxy.



18
19
20
21
# File 'lib/active_versioning/model/version_proxy.rb', line 18

def initialize(version)
  @version = version
  __setobj__(version.reify)
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/active_versioning/model/version_proxy.rb', line 16

def version
  @version
end

Instance Method Details

#__getobj__Object



23
24
25
# File 'lib/active_versioning/model/version_proxy.rb', line 23

def __getobj__
  @record
end

#__setobj__(record) ⇒ Object



27
28
29
# File 'lib/active_versioning/model/version_proxy.rb', line 27

def __setobj__(record)
  @record = record
end

#commit(params = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_versioning/model/version_proxy.rb', line 87

def commit(params = {})
  raise draft_exception unless version.draft?

  attrs = version_attributes.tap do |attrs|
    attrs.merge!(
      draft:        false,
      event:        ActiveVersioning::Events::COMMIT,
      committed_at: Time.current
    )

    attrs.merge!(params)
  end

  version.update(attrs)

  __getobj__.update(versioned_attributes)
end

#live?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/active_versioning/model/version_proxy.rb', line 79

def live?
  false
end

#reloadObject



35
36
37
# File 'lib/active_versioning/model/version_proxy.rb', line 35

def reload
  __setobj__(version.reify)
end

#saveObject



43
44
45
46
47
48
49
# File 'lib/active_versioning/model/version_proxy.rb', line 43

def save(*)
  if valid?
    version.update(version_attributes)
  else
    false
  end
end

#save!Object



51
52
53
54
55
56
57
# File 'lib/active_versioning/model/version_proxy.rb', line 51

def save!(*)
  if valid?
    version.update!(version_attributes)
  else
    raise ::ActiveRecord::RecordInvalid.new(__getobj__)
  end
end

#to_paramObject



39
40
41
# File 'lib/active_versioning/model/version_proxy.rb', line 39

def to_param
  version.versionable.to_param
end

#update(attributes) ⇒ Object Also known as: update_attributes



59
60
61
62
63
64
65
66
# File 'lib/active_versioning/model/version_proxy.rb', line 59

def update(attributes)
  raise draft_exception unless version.draft?

  with_transaction_returning_status do
    assign_attributes(attributes)
    save
  end
end

#update!(attributes) ⇒ Object Also known as: update_attributes!



69
70
71
72
73
74
75
76
# File 'lib/active_versioning/model/version_proxy.rb', line 69

def update!(attributes)
  raise draft_exception unless version.draft?

  with_transaction_returning_status do
    assign_attributes(attributes)
    save!
  end
end

#version?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/active_versioning/model/version_proxy.rb', line 83

def version?
  true
end