Class: Engine2::StarToManyLinkAction

Inherits:
Action show all
Includes:
ActionModelSupport
Defined in:
lib/engine2/action/link.rb

Instance Attribute Summary

Attributes inherited from Action

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

Instance Method Summary collapse

Methods included from ActionModelSupport

#hide_pk, #node_defined, #show_pk, #unsupported_association

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, #repeat, #request, #request_action_proc_params, #split_keys

Constructor Details

This class inherits a constructor from Engine2::Action

Instance Method Details

#invoke(handler) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/engine2/action/link.rb', line 15

def invoke handler
    json = handler.post_to_json
    parent = json[:parent_id]
    ids = json[:ids]
    handler.permit parent && ids
    invoke_link_db handler, split_keys(parent), ids
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/engine2/action/link.rb', line 23

def invoke_link_db handler, parent, ids
    model = assets[:model]
    assoc = assets[:assoc]

    case assoc[:type]
    when :one_to_many
        model.db.transaction do
            pk = Hash[assoc[:keys].zip(parent)]
            ids.each do |id|
                model.where(model.primary_keys_hash(split_keys(id))).update(pk)
            end
        end
    when :many_to_many
        p_pk = Hash[assoc[:left_keys].zip(parent)]
        values = ids.map do |id|
            p_pk.merge Hash[assoc[:right_keys].zip(split_keys(id))]
        end
        model.db[assoc[:join_table]].multi_insert values
    else unsupported_association
    end
    {}
end

#pre_runObject



10
11
12
13
# File 'lib/engine2/action/link.rb', line 10

def pre_run
    super
    execute "action.errors || [action.parent().invoke(), action.panel_close()]"
end