class ParamsReady::Ordering::OrderingParameter
Public Instance Methods
by_columns()
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 29 def by_columns bare_value.each_with_index.each_with_object(Hash.new([:none, nil])) do |(tuple, index), hash| hash[tuple[0].unwrap] = [tuple[1].unwrap, index] end end
filtered(name)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 46 def filtered(name) bare_value.map do |tuple| tuple.format(Intent.instance(:backend)) end.partition do |item| next item[0] == name end end
inverted_order()
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 73 def inverted_order update_in(inverted_order_value, []) end
inverted_order_value()
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 59 def inverted_order_value bare_value.map do |tuple| name = tuple.first.unwrap case tuple.second.unwrap when :asc [name, :desc] when :desc [name, :asc] else raise ParamsReadyError, "Unexpected ordering: '#{tuple.second.unwrap}'" end end end
marshal(intent)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 12 def marshal(intent) arr = to_array(intent) return arr unless intent.marshal?(name_for_formatter) arr.join(definition.class::COLUMN_DELIMITER) end
order_for(name)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 121 def order_for(name) order = bare_value.find do |tuple| tuple[0].unwrap == name end return :none if order.nil? order[1].unwrap end
prepend_item(name, direction, array)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 54 def prepend_item(name, direction, array) return array if direction == :none [[name, direction], *array] end
reordered(name, new_dir)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 96 def reordered(name, new_dir) update_in(reordered_value(name, new_dir), []) end
reordered_value(name, new_dir)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 91 def reordered_value(name, new_dir) _, save = filtered(name) prepend_item(name, new_dir, save) end
restriction_from_context(context)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 100 def restriction_from_context(context) restriction = context.to_restriction return restriction if restriction.name_permitted? :ordering || !required? Restriction.permit({ ordering: [] }) end
to_arel(default_table, context: Restriction.blanket_permission, inverted: false)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 107 def to_arel(default_table, context: Restriction.blanket_permission, inverted: false) ordering = inverted ? inverted_order : self ordering.to_array_with_context(context).flat_map do |(column_name, direction)| column = definition.columns[column_name] attribute = column.attribute(column_name, default_table, context) column.clauses(attribute, direction, inverted: inverted) end end
to_array(intent = Intent.instance(:backend))
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 19 def to_array(intent = Intent.instance(:backend)) arr = bare_value arr.map do |tuple| name = tuple.first.unwrap next unless intent.name_permitted?(name) || definition.required?(name) tuple.format(intent) end.compact end
to_array_with_context(context)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 116 def to_array_with_context(context) intent = Intent.instance(:backend).clone(restriction: restriction_from_context(context)) to_array(intent.for_children(self)) end
toggle_schema(schema)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 35 def toggle_schema(schema) case schema when :desc [:desc, :asc] when :asc [:asc, :desc] else [:none, :none] end end
toggled_order(name)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 87 def toggled_order(name) update_in(toggled_order_value(name), []) end
toggled_order_value(name)
click to toggle source
# File lib/params_ready/ordering/ordering.rb, line 77 def toggled_order_value(name) drop, save = filtered(name) old_dir = drop.count > 0 ? drop.first[1] : :none primary, secondary = toggle_schema(definition.columns[name.to_sym].ordering) new_dir = old_dir == primary ? secondary : primary prepend_item(name, new_dir, save) end