Class: Engine2::DeleteMetaBase

Inherits:
Meta show all
Defined in:
lib/engine2/meta/delete_meta.rb

Direct Known Subclasses

BulkDeleteMeta, DeleteMeta

Instance Attribute Summary

Attributes inherited from Meta

#action, #assets, #invokable, #static

Instance Method Summary collapse

Methods inherited from Meta

#action_defined, #check_static_meta, #dynamic?, #freeze_meta, #get, http_method, #http_method, inherited, #initialize, #invoke!, #lookup, #merge, #meta_type, meta_type, #post_process, #post_run, #pre_run, #request, #request_meta_proc_params, #split_keys

Constructor Details

This class inherits a constructor from Engine2::Meta

Instance Method Details

#invoke_delete_db(handler, ids) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/engine2/meta/delete_meta.rb', line 6

def invoke_delete_db handler, ids
    begin
        model = assets[:model]
        model.db.transaction do
            ids.each do |id|
                keys = split_keys(id)
                restrict = model.association_reflections.select do |name, rel|
                    case rel[:type]
                    when :one_to_many
                        !model.db[name].where(Hash[rel[:keys].zip(keys)]).empty?
                    when :many_to_many
                        !model.db[rel[:join_table]].where(Hash[rel[:left_keys].zip(keys)]).empty?
                    when :many_to_one
                    when :one_to_one
                    else
                        unsupported_association rel[:type]
                    end
                end
                raise Sequel::DestroyFailed.new("Blokujące relacje: #{restrict.map{|name, rel| name}.join(', ')}" ) unless restrict.empty?

                rec = model.call(Hash[model.primary_keys.zip(keys)])
                rec.destroy(transaction: false)
                # model.where(model.primary_keys_hash(keys)).delete # model.dataset[model.primary_key => id].delete
            end
        end
        {}
    rescue Sequel::NoExistingObject
        handler.halt_not_found LOCS[:no_entry]
    rescue Sequel::DestroyFailed => failure
        handler.halt_forbidden failure.error.to_s
    end
end