Class: Engine2::DeleteActionBase

Inherits:
Action show all
Defined in:
lib/engine2/action/delete.rb

Direct Known Subclasses

BulkDeleteAction, DeleteAction

Instance Attribute Summary

Attributes inherited from Action

#assets, #invokable, #meta, #node, #static

Instance Method Summary collapse

Methods inherited from Action

action_type, #action_type, #arguments, #check_anonymous_action_class, #check_static_action, #define_invoke, #define_method, #dynamic?, #execute, #freeze_action, #http_method, http_method, inherit, inherited, #initialize, #invoke!, #join_keys, #lookup, #merge, #node_defined, #post_process, #post_run, #pre_run, #repeat, #request, #request_action_proc_params, #split_keys

Constructor Details

This class inherits a constructor from Engine2::Action

Instance Method Details

#invoke_delete_db(handler, ids, from_assoc = nil) ⇒ 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
38
39
40
41
42
43
# File 'lib/engine2/action/delete.rb', line 6

def invoke_delete_db handler, ids, from_assoc = nil
    model = assets[:model]
    model.db.transaction do
        ids.each do |id|
            keys = split_keys(id)

            model.association_reflections.each do |name, assoc|
                ds = case assoc[:type]
                when :one_to_one
                when :one_to_many
                    model.db[assoc.associated_class.table_name].where(Hash[assoc[:keys].zip(keys)])
                when :many_to_many
                    model.db[assoc[:join_table]].where(Hash[assoc[:left_keys].zip(keys)])
                when :many_to_one
                    nil
                else
                    unsupported_association assoc[:type]
                end

                if assoc[:delete] == :cascade || from_assoc == assoc.associated_class.table_name
                    ds.delete
                else
                    raise Sequel::DestroyFailed.new("#{LOCS[:delete_restricted]}: #{name}") unless ds.empty?
                end if ds
            end

            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