lib/engine2/action/decode.rb in engine2-1.0.8 vs lib/engine2/action/decode.rb in engine2-1.0.9

- old
+ new

@@ -55,10 +55,14 @@ action_type :decode_list def invoke handler {entries: get_query.limit(200).load_all} end + + def order *fields + @query = get_query.order *fields + end end class TypeAheadAction < DecodeAction action_type :typeahead @@ -69,20 +73,42 @@ def limit lmt @limit = lmt end + def get_limit + @limit + end + + def get_case_insensitive + @case_insensitive + end + def case_insensitive @case_insensitive = true end + def order *fields + @query = get_query.order *fields + end + def invoke handler + model = assets[:model] if query = handler.params[:query] - condition = @meta[:decode_fields].map{|f|f.like("%#{query}%", case_insensitive: @case_insensitive)}.reduce{|q, f| q | f} - {entries: get_query.where(condition).limit(@limit).load_all} + fields = @meta[:decode_fields] || static.meta[:decode_fields] + + entries = if query.to_s.empty? + get_query + else + table_name = model.table_name + condition = fields.map{|f|table_name.q(f).like("%#{query}%", case_insensitive: @case_insensitive || static.get_case_insensitive)}.reduce{|q, f| q | f} + get_query.where(condition) + end.limit(@limit || static.get_limit).load_all + + {entries: entries} else handler.permit id = handler.params[:id] - record = get_query.load Hash[assets[:model].primary_keys.zip(split_keys(id))] + record = get_query.unordered.load Hash[model.primary_keys_qualified.zip(split_keys(id))] # handler.halt_not_found(LOCS[:no_entry]) unless record {entry: record} end end end \ No newline at end of file