# coding: utf-8 # frozen_string_literal: true module Engine2 class ArrayListAction < Action action_type :list include ActionListSupport (DefaultFilters ||= {}).merge!( exact: lambda{|entries, name, value, type_info, hash| entries.select{|e|e[name] == value} }, string: lambda{|entries, name, value, type_info, hash| entries.select{|e|e[name].to_s[value]} }, boolean: lambda{|*args| DefaultFilters[:exact].(*args)}, integer: lambda{|entries, name, value, type_info, hash| if value.is_a? Hash from, to = value[:from], value[:to] if from && to entries.select{|e|e[name] >= from.to_i && e[name] <= to.to_i} else entries.select{|e| from ? e[name] >= from.to_i : e[name] <= to.to_i} end elsif value.is_a? Integer entries.select{|e|e[name] == value.to_i} else nil end } ) def data_source handler [] end def invoke handler params = handler.params # if params[:initial] || params[:refresh] entries = data_source(handler) per_page = lookup(:config, :per_page) page = params[:page].to_i handler.permit page >= 0 && page < 1000 if order_str = params[:order] order = order_str.to_sym handler.permit lookup(:fields, order, :sort) entries = entries.sort_by{|e|e[order].to_s} entries = entries.reverse if params[:asc] == "true" end if search = params[:search] entries = list_search(entries, handler, search) end {entries: entries.drop(page).take(per_page), count: entries.size} end def list_search entries, handler, search hash = JSON.parse(search, symbolize_names: true) rescue handler.halt_forbidden model = assets[:model] sfields = lookup(:search_field_list) handler.permit sfields hash.each_pair do |name, value| handler.permit sfields.include?(name) type_info = model.find_type_info(name) entries = if filter = (@filters && @filters[name]) || (dynamic? && (static.filters && static.filters[name])) filter.(handler, entries, hash) elsif filter = DefaultFilters[type_info[:otype]] filter.(entries, name, value, type_info, hash) else raise E2Error.new("Filter not found for field '#{name}' in model '#{model}'") unless filter end handler.permit entries end entries end end end