Class: Engine2::StarToManyLinkMeta

Inherits:
Meta show all
Includes:
MetaModelSupport
Defined in:
lib/engine2/meta/link_meta.rb

Instance Attribute Summary

Attributes inherited from Meta

#action, #assets, #invokable, #static

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaModelSupport

#action_defined, #get_type_info, #hide_pk, #pre_run, #show_pk, #unsupported_association

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, #response, #split_keys

Constructor Details

This class inherits a constructor from Engine2::Meta

Class Method Details



36
37
38
39
40
41
42
# File 'lib/engine2/meta/link_meta.rb', line 36

def self.many_to_many_link_db model, assoc, parent, ids
    p_pk = Hash[assoc[:left_keys].zip(parent)]
    values = ids.map do |id|
        p_pk.merge Hash[assoc[:right_keys].zip(Sequel::split_keys(id))]
    end
    model.db[assoc[:join_table]].multi_insert values
end


29
30
31
32
33
34
# File 'lib/engine2/meta/link_meta.rb', line 29

def self.one_to_many_link_db model, assoc, parent, ids
    pk = Hash[assoc[:keys].zip(parent)]
    ids.each do |id|
        model.where(model.primary_keys_hash(Sequel::split_keys(id))).update(pk)
    end
end

Instance Method Details

#invoke(handler) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/engine2/meta/link_meta.rb', line 9

def invoke handler
    json = handler.post_to_json
    parent = json[:parent_id]
    ids = json[:ids]
    handler.permit parent && ids
    model = assets[:model]
    assoc = assets[:assoc]
    case assoc[:type]
    when :one_to_many
        model.db.transaction do
            self.class.one_to_many_link_db model, assoc, split_keys(parent), ids
        end
        {}
    when :many_to_many
        self.class.many_to_many_link_db model, assoc, split_keys(parent), ids
        {}
    else unsupported_association
    end
end