class Engine2::StarToManyListAction

Public Instance Methods

list_context(query, handler) click to toggle source
# File lib/engine2/action/list.rb, line 217
def list_context query, handler
    handler.permit parent = handler.params[:parent_id]
    model = assets[:model]
    assoc = assets[:assoc]
    parent_keys = split_keys(parent)
    case assoc[:type]
    when :one_to_many
        keys = assoc[:keys]
        condition = parent_keys.all?(&:empty?) ? false : Hash[keys.map{|k| model.table_name.q(k)}.zip(parent_keys)]
        if handler.params[:negate]
            query = query.exclude(condition)
            query = query.or(Hash[keys.zip([nil])]) if keys.all?{|k|model.db_schema[k][:allow_null] == true} # type_info[:required] ?
            query
        else
            query.where(condition)
        end
    when :many_to_many
        q_pk = model.primary_keys_qualified
        j_table = assoc[:join_table]
        l_keys = assoc[:left_keys].map{|k| j_table.q(k)}
        r_keys = assoc[:right_keys].map{|k| j_table.q(k)}
        r_keys_vals = Hash[r_keys.zip(q_pk)]
        l_keys_vals = parent_keys.all?(&:empty?) ? false : Hash[l_keys.zip(parent_keys)]

        if handler.params[:negate]
            query.exclude(model.db[j_table].select(nil).where(r_keys_vals & l_keys_vals).exists)
        else
            # query.qualify.join(j_table, [r_keys_vals, l_keys_vals])
            if joins = query.opts[:join] and joins.find{|j|j.table == j_table}
                query
            else
                query.qualify.left_join(j_table, r_keys_vals)
            end.filter(l_keys_vals)
        end
    else unsupported_association
    end
end
post_run() click to toggle source
Calls superclass method Engine2::ListAction#post_run
# File lib/engine2/action/list.rb, line 255
def post_run
    super
    nd = node.parent.nodes[:decode_entry]
    request do |h|
        if h.initial?
            action = nd.*
            rec = action.invoke_decode(h, [split_keys(h.params[:parent_id])]).first
            panel_title "#{static.panel[:title]} - #{action.meta[:decode_fields].map{|f|rec[f]}.join(action.meta[:separator])}"
        end
    end if nd
end
pre_run() click to toggle source
Calls superclass method Engine2::Action#pre_run
# File lib/engine2/action/list.rb, line 211
def pre_run
    super
    menu(:panel_menu).option_at 0, :close, icon: "remove"
    panel_title "#{assets[:model].model_icon.icon} #{LOCS[assets[:assoc][:name]]}"
end