Module: Spider::Model::Versioned::Mapper

Defined in:
lib/spiderfw/model/mixins/versioned.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) execute_action(action, object, params)

:nodoc:



271
272
273
274
275
276
277
278
279
# File 'lib/spiderfw/model/mixins/versioned.rb', line 271

def execute_action(action, object, params) # :nodoc:
    return super unless [:save_version, :save_junction_version].include?(action)
    case action
    when :save_version
        save_version(object)
    when :save_junction_version
        save_junction_version(object)
    end
end

- (Object) get_dependencies(task)



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/spiderfw/model/mixins/versioned.rb', line 219

def get_dependencies(task)
    return super unless @model.respond_to?(:version_model)
    deps = []
    obj = task.object
    action = task.action
    version_contents = obj.class.version_contents
    vmod = @model.version_model
    case action
    when :save_version
        version_contents.each do |vc|
            next unless vc.model?
            next if vc.integrated?
            v_el = vmod.elements[vc.name]
            next unless v_el
            is_embedded = v_el.attributes[:embedded] && vmod.storage.supports?(:embedding)
            next if is_embedded && !vc.junction?
            Spider.logger.debug("VC #{vc.name}")
            set = obj.send(vc.name)
            next unless set
            set = obj.prepare_version_object(vc.name, set) if obj.respond_to?(:prepare_version_object)
            next if set.eql?(obj)
            set = obj.instance_variable_get("@#{vc.name}_junction") if vc.junction? && !vc.attributes[:keep_junction]
            next unless set
            set = [set] unless vc.multiple?
            set.each do |set_obj|
                s_obj = set_obj
                if have_references?(vc)
                    #Spider.logger.debug("Version on #{obj} (#{obj.class}) depends on #{set_obj} (#{set_obj.class})")
                    deps << [task, MapperTask.new(set_obj, :save_version)]
                elsif vc.junction?
                    dejunct = set_obj.get(vc.attributes[:junction_their_element])
                    dejunct = obj.class.prepare_junction_version_object(vc.name, dejunct) if obj.class.respond_to?(:prepare_junction_version_object)
                    dejunct_task = MapperTask.new(dejunct, :save_version)
                    junction_task = MapperTask.new(set_obj, :save_version)
                    if is_embedded
                        deps << [task, dejunct_task] if dejunct.class < Spider::Model::Versioned
                    else
                        deps << [junction_task, dejunct_task] if dejunct.class < Spider::Model::Versioned
                        deps << [junction_task, task]
                    end
                else
                    #Spider.logger.debug("Version on #{set_obj} (#{set_obj.class}) depends on #{obj} (#{obj.class})")
                    deps << [MapperTask.new(set_obj, :save_version), task]
                end
            end
        end
    else
        return super
    end
    deps
end

- (Object) save_junction_version(object)



314
315
316
317
318
319
320
321
322
# File 'lib/spiderfw/model/mixins/versioned.rb', line 314

def save_junction_version(object)
    vmod = @model.version_model
    vobj = vmod.static
    object.class.elements_array.select{ |el| el.attributes[:junction_reference] }.each do |el|
        val = object.get(el)
        vobj.set(el, val.get(:v_sha1))
    end
    vobj.mapper.do_insert(vobj)
end

- (Object) save_version(object)



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/spiderfw/model/mixins/versioned.rb', line 281

def save_version(object)
    mod = @model
    vmod = mod.version_model
    vobj = vmod.static()
    ve = mod.version_element
    current_sha1 = object.get(ve)
    new_sha1 = object.version_sha1
    # debugger
    # debugger
    return if current_sha1 == new_sha1
    # debugger
    object.v_sha1 = new_sha1
    vobj = Spider::Model.get(vmod, :v_sha1 => new_sha1)
    vobj.autoload(false)
    object.populate_version_object(vobj)
    vobj.autoload(false)
    
    vobj.set(mod.elements[:history].reverse, object)
    vobj.set(:version_date, DateTime.now)
    # vobj.set(:version_comment, comment)
    object.mapper.do_update(object)
    dup = false
    begin
        vobj.mapper.insert(vobj)
        #vobj.insert
    rescue Spider::Model::Storage::DuplicateKey
        dup = true
        Spider.logger.error("Duplicate version for #{self}")
    end
    object.autoload(true)
    object.trigger(:version_saved) unless dup
end