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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/spiderfw/model/mixins/versioned.rb', line 45
def versioning(branch=nil)
@version_models ||= {}
@version_elements ||= {}
par = self.containing_module
local_name = self.name.sub(par.name, '')
versioned_module = par.const_get(:Versioned)
unless @version_models[branch]
vmod = Versioned.create_version_model(self)
vmod_name = local_name.to_s
vmod_name += Spider::Inflector.camelize(branch) if branch
versioned_module.const_set_full(vmod_name, vmod)
@version_models[branch] = vmod
end
vmod = @version_models[branch]
if self.attributes[:sub_model] && self.attributes[:sub_model].respond_to?(:version_model)
vmod.attributes[:sub_model] = self.attributes[:sub_model].version_model
end
if @polymorphic_models
@polymorphic_models.each do |pmod, options|
pmod = pmod.version_model if pmod.respond_to?(:version_model)
vmod.polymorphic(pmod, options)
end
end
vmod.attributes[:version_model] = true
vmod.primary_keys.each do |pk|
vmod.remove_element(pk.name) unless pk.attributes[:version_pk] end
vmod.elements_array.each do |el|
el.attributes[:autoincrement] = false
end
self.remove_element(:history) vmod.element(:v_original, self, :add_multiple_reverse => {:name => :history, :version_history => true, :hidden => true}, :hidden => true)
vmod.element(:version_date, DateTime)
vmod.element(:version_comment, String)
vmod.remove_element(:history)
self.elements_array.each do |el|
next if el.name == :v_sha1
elh = dump_element(el)
if el.integrated?
if el.integrated_from.type < Versioned && vmod.elements[el.name] && vmod.elements[el.name].model? && \
!vmod.elements[el.name].integrated_from.type.attributes[:version_model]
vmod.remove_element(el)
end
next
end
next unless elh
if (el.model?)
is_version_content = el.type < Spider::Model::Versioned && el.attributes[:version_content] != false
if elh[:attributes][:integrated_model]
elh[:type].elements.each do |ielname, iel|
i = vmod.elements[ielname]
vmod.remove_element(ielname) if i && i.integrated? && i.integrated_from.name == el.name
end
elh[:attributes][:integrate] = true
elh[:attributes].delete(:extended_model)
elh[:attributes].delete(:embedded)
end
if el.attributes[:add_reverse] || el.attributes[:add_multiple_reverse]
rev = el.attributes[:add_reverse] || el.attributes[:add_multiple_reverse]
el.type.version_model.remove_element(rev[:name]) if el.type.respond_to?(:version_model)
end
if vmod.elements[el.name] && elh[:attributes][:reverse] && !is_version_content
vmod.elements[el.name].attributes.delete(:reverse)
vmod.elements[el.name].attributes.delete(:condition)
end
if el.multiple? && !el.attributes[:junction] && !is_version_content
elh[:attributes].delete(:reverse)
elh[:attributes].delete(:add_reverse)
elh[:attributes].delete(:add_multiple_reverse)
vmod.remove_element(el.name)
vmod.send(elh[:method], el.name, el.type, elh[:attributes])
elsif !el.attributes[:added_reverse] && el.model < Spider::Model::Versioned
vmod.remove_element(el.name)
if elh[:method] == :tree
vmod.remove_element(elh[:attributes][:reverse])
vmod.remove_element(elh[:attributes][:tree_left])
vmod.remove_element(elh[:attributes][:tree_right])
vmod.remove_element(elh[:attributes][:tree_depth])
vmod.remove_element(elh[:attributes][:tree_position])
vmod.tree(el.name, elh[:attributes])
else
vmod.send(elh[:method], el.name, el.type.version_model, elh[:attributes])
end
@version_elements[el.name] = el.name
elsif el.attributes[:junction] && el.attributes[:owned]
junction = vmod.elements[el.name].attributes[:association_type]
unless junction < Spider::Model::Versioned || (el.attributes[:embedded] && self.storage.supports?(:embedding))
junction.module_eval{ include Spider::Model::Versioned }
junction.versioning(branch)
end
unless vmod.elements[el.name].model.attributes[:version_model]
elh[:attributes][:through] = junction.version_model
elh[:attributes][:junction_our_element] = "#{elh[:attributes][:reverse]}".to_sym
elh[:attributes][:junction_their_element] = "#{elh[:attributes][:junction_their_element]}".to_sym
vmod.remove_element(el.name)
junction_type = nil
if is_version_content
junction_type = el.type.version_model
else
junction_type = el.type
elh[:attributes].delete(:reverse)
elh[:attributes].delete(:add_reverse)
elh[:attributes].delete(:add_multiple_reverse)
v_junction = junction.version_model
v_junction.remove_element(elh[:attributes][:junction_their_element])
v_junction.element(elh[:attributes][:junction_their_element], el.type, :association => :choice, :junction_reference => true)
v_junction.integrate(elh[:attributes][:junction_their_element], :hidden => true, :no_pks => true)
end
vmod.send(elh[:method], el.name, junction_type, elh[:attributes])
junction.version_model.integrate(elh[:attributes][:junction_their_element]) if junction.attributes[:sub_model] == self
end
@version_elements[el.name] = el.name
end
end
end
vmod.elements_array.each{ |el| el.attributes[:unique] = false if el.attributes[:unique] }
doc_storage = Spider.conf.get('storage.versioning.use_document')
if doc_storage && Spider.conf.get("storages.#{doc_storage}")
vmod.use_storage(doc_storage)
vmod.elements_array.select{ |el| el.junction? }.each do |el|
el.model.use_storage(doc_storage)
end
end
end
|