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

#default_order_field, #filters, #orders

Attributes inherited from Action

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

Instance Method Summary collapse

Methods included from ActionListSupport

#default_order, #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

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

Methods included from ActionAPISupport

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

Methods included from ActionTabSupport

#field_tabs, #select_tabs, #tab

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 ActionDraggableSupport

#draggable

Methods included from ActionQuerySupport

#find_record, #get_query, #query, #select

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/engine2/action/list.rb', line 96

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)
    elsif @filters || static.filters
        static.filters.to_h.merge(@filters.to_h).each do |name, filter|
            query = filter.(handler, query, {})
            handler.permit query
        end
    end

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

    if order = params[:order]
        order = order.to_sym
        handler.permit lookup(:fields, order, :sort)
        query = list_order(query, handler, order, params[:asc] == "true")
    elsif order = @default_order_field || static.default_order_field
        query = list_order(query, handler, order, true)
    end

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

    query = query.limit(per_page, page)

    res = {entries: page_frame(handler, query.load_all)}
    res[:count] = count if count
    res
end

#list_context(query, handler) ⇒ Object



190
191
192
# File 'lib/engine2/action/list.rb', line 190

def list_context query, handler
    query
end

#list_order(query, handler, order, asc) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/engine2/action/list.rb', line 178

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

    asc ? query : query.reverse
end

#list_search(query, handler, search) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/engine2/action/list.rb', line 131

def list_search query, handler, search
    hash = JSON.parse(search, symbolize_names: true) rescue handler.halt_forbidden
    model = assets[:model]
    sfields = lookup(:search_field_list)
    handler.permit sfields

    filters = @filters || static.filters ? static.filters.to_h.merge(@filters.to_h) : {}

    sfields.each do |name|
        type_info = model.find_type_info(name)
        name = name.to_sym
        filter = filters[name]
        if !(value = hash[name]).nil? # use key?
            query = if filter
                filter.(handler, query, hash)
            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
        else
            if filter
                query = filter.(handler, query, hash)
                handler.permit query
            end
        end
    end

    # hash.each_pair do |name, value|
    #     handler.permit name = sfields.find{|sf|sf.to_sym == name}
    #     type_info = model.find_type_info(name)
    #     query = if filter = (@filters && @filters[name]) || (dynamic? && (static.filters && static.filters[name]))
    #         filter.(handler, query, hash)
    #     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

#page_frame(handler, entries) ⇒ Object



92
93
94
# File 'lib/engine2/action/list.rb', line 92

def page_frame handler, entries
    entries
end

#post_runObject



87
88
89
90
# File 'lib/engine2/action/list.rb', line 87

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