Class: Engine2::ListAction

Inherits:
Action show all
Includes:
ActionListSupport, ActionQuerySupport
Defined in:
lib/engine2/action/list.rb

Direct Known Subclasses

ManyToOneListAction, StarToManyListAction

Instance Attribute Summary

Attributes included from ActionListSupport

#filters, #orders

Attributes inherited from Action

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

Instance Method Summary collapse

Methods included from ActionListSupport

#field_tabs, #filter, #filter_case_insensitive, #order, #post_process, #pre_run, #search_live, #search_template, #searchable, #searchable_tabs, #select_toggle_menu, #sortable, #template

Methods included from ActionModelSupport

#get_type_info, #hide_pk, #node_defined, #pre_run, #show_pk, #unsupported_association

Methods included from ActionAPISupport

#config, #decorate, #field_filter, #hide_fields, #info, #info!, #loc!, #render, #show_fields

Methods included from ActionTabSupport

#field_tabs, #lazy_tab, #select_tabs

Methods included from ActionPanelSupport

#modal_action, #panel, #panel_class, #panel_footer, #panel_header, #panel_panel_template, #panel_template, #panel_title, #pre_run

Methods included from ActionMenuSupport

#menu, #menu?, #post_process

Methods included from ActionOnChangeSupport

#on_change

Methods included from ActionQuerySupport

#find_record, #get_query, #query, #select

Methods inherited from Action

action_type, #action_type, #arguments, #check_static_action, #define_invoke, #dynamic?, #execute, #freeze_action, #http_method, http_method, inherit, inherited, #initialize, #invoke!, #lookup, #merge, #node_defined, #post_process, #pre_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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/engine2/action/list.rb', line 78

def invoke handler
    params = handler.params
    model = assets[:model]
    query = list_context(get_query, handler)

    if search = params[:search]
        query = list_search(query, handler, search)
    end

    count = query.count if lookup(:config, :use_count)

    if order_str = params[:order]
        order = order_str.to_sym
        handler.permit lookup(:info, order, :sort)

        if order_blk = (@orders && @orders[order]) || (dynamic? && (static.orders && static.orders[order]))
            query = order_blk.(query, handler)
        else
            order = model.table_name.q(order) if model.type_info[order]
            query = query.order(order)
        end

        query = query.reverse if params[:asc] == "true"
    end

    per_page = lookup(:config, :per_page)
    page = params[:page].to_i
    handler.permit page >= 0 && page < 1000

    query = query.limit(per_page, page)

    res = {entries: query.all}
    res[:count] = count if count
    res
end

#list_context(query, handler) ⇒ Object



137
138
139
# File 'lib/engine2/action/list.rb', line 137

def list_context query, handler
    query
end

#list_search(query, handler, search) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/engine2/action/list.rb', line 114

def list_search query, handler, search
    hash = JSON.parse(search, symbolize_names: true) rescue handler.halt_forbidden
    model = assets[:model]
    sfields = lookup(:search_fields)
    handler.permit sfields
    hash.each_pair do |name, value|
        handler.permit name = sfields.find{|sf|sf.to_sym == name}

        type_info = get_type_info(name)
        query = if filter = (@filters && @filters[name]) || (dynamic? && (static.filters && static.filters[name]))
            filter.(query, hash, handler)
        elsif filter = DefaultFilters[type_info[:otype]]
            name = model.type_info[name] ? model.table_name.q(name) : Sequel.expr(name)
            filter.(query, name, value, type_info, hash)
        else
            raise E2Error.new("Filter not found for field '#{name}' in model '#{model}'") unless filter
        end

        handler.permit query
    end
    query
end

#post_runObject



73
74
75
76
# File 'lib/engine2/action/list.rb', line 73

def post_run
    query select(*assets[:model].columns.reject{|col| assets[:model].type_info[col][:length].to_i > 20}.take(10)) unless @query
    super
end