class Basepack::Forms::Query

Attributes

auth_object[R]
date_format[RW]
edit_ql[R]
filterql_options[R]
params[R]
ql[RW]
query[R]

Public Class Methods

localize_value(field, predicate_name, value) click to toggle source
# File lib/basepack/forms/query.rb, line 191
def self.localize_value(field, predicate_name, value)
  return value if value.is_a? Array
  return "" if FilterQL.predicates[predicate_name][:type] == :boolean

  case field.type
  when :date, :datetime
    I18n.l(value.to_date)
  when :time
    I18n.l(value.to_time, :format => "%H:%M")
  else
    value
  end
end
new(factory, chain, options = {}) click to toggle source
Calls superclass method Basepack::Forms::Base::new
# File lib/basepack/forms/query.rb, line 15
def initialize(factory, chain, options = {})
  super(factory, chain, options)

  @scope = options[:scope]
  @auth_object = options[:auth_object]
  @filterql_options = options[:filterql_options]
  @collection_includes = options[:collection_includes]

  if params = options[:params]
    @params = params.slice(:query, :ql, :f, :page, :per).reject {|k, v| v.empty?}
    @query = params[:query]

    if has_errors?
      @ql = params[:ql]
    elsif params[:edit_ql]
      @edit_ql = params[:edit_ql]
    end
  end
end

Public Instance Methods

collection() click to toggle source
# File lib/basepack/forms/query.rb, line 35
def collection
  @collection || begin
    query_from_params
    @collection
  end
end
collection_without_pagination() click to toggle source
# File lib/basepack/forms/query.rb, line 42
def collection_without_pagination
  collection.offset(nil).limit(nil)
end
conditions_to_ql() click to toggle source
# File lib/basepack/forms/query.rb, line 154
def conditions_to_ql
  FilterQL.conditions_to_ql(filtered_fields.map do |field, predicate_name, value|
    [field_nested_name(field), predicate_name, value]
  end)
end
default_partial() click to toggle source
# File lib/basepack/forms/query.rb, line 179
def default_partial
  'forms/query'
end
enum_options(fields = fields) click to toggle source
# File lib/basepack/forms/query.rb, line 160
def enum_options(fields = fields)
  res = {}
  fields.each do |f|
    options = f.enum_options
    if options and f.filterable?
      #key f.name is not enought because of enum_options is called recursielly on assiciations
      res[field_nested_name(f)] = options
    elsif (f.association? and !nested_in and !f.polymorphic?)
      res.update(f.nform.enum_options)
    end
  end
  res
end
field_nested_name(field) click to toggle source
# File lib/basepack/forms/query.rb, line 69
def field_nested_name(field)
  field.form.nested_in ? "#{field_nested_name(field.form.nested_in)}_#{field.name}" : field.name
end
filterable_field(name) click to toggle source
# File lib/basepack/forms/query.rb, line 73
def filterable_field(name)
  f = field(name)
  f.try(:filterable?) ? f : nil
end
filterable_fields() click to toggle source
# File lib/basepack/forms/query.rb, line 78
def filterable_fields
  fields.select {|f| f.filterable? }
end
filtered_fields() click to toggle source
# File lib/basepack/forms/query.rb, line 97
def filtered_fields
  @filtered_fields || begin
    if nested_in
      @filtered_fields = []
    else
      @filtered_fields = resource_filter.c.map do |condition|
        if condition.valid? and condition.attributes.size == 1
          if field = nested_filterable_field(condition.attributes.first.name.to_sym)
            [field, condition.predicate_name, Query.localize_value(field, condition.predicate_name, condition.value)]
          end
        end
      end.concat(Array.wrap(resource_filter.custom_filters).map do |name, predicate_name, values|
        if field = nested_filterable_field(name.to_sym)
          [field, predicate_name, values]
        end
      end).compact
    end
  end
end
filtered_fields_find(nested_field_name, condition = nil) click to toggle source
# File lib/basepack/forms/query.rb, line 117
def filtered_fields_find(nested_field_name, condition = nil)
  filtered_fields.find do |f|
    (field_nested_name(f[0]) == nested_field_name) and 
    (condition ? (f[1] == condition) : true)
  end
end
has_errors?() click to toggle source
# File lib/basepack/forms/query.rb, line 53
def has_errors?
  resource_filter.errors[:base].present?
end
initial_data() click to toggle source
# File lib/basepack/forms/query.rb, line 124
def initial_data
  if @edit_ql
    init = []
  else
    init = filtered_fields.map do |field, predicate_name, values|
      {
        label:     field.nested_label,
        name:      field_nested_name(field),
        type:      field.type,
        value:     values,
        predicate: predicate_name,
        template:  field.render.to_s,
      }
    end
  end

  if @ql or @edit_ql
    init << {
      label:     I18n.t('basepack.query.query'),
      name:      'ql',
      type:      'ql',
      value:     @ql || conditions_to_ql,
      predicate: 'eq',
      template:  '',
    }
  end

  init
end
nested_filterable_field(name) click to toggle source
# File lib/basepack/forms/query.rb, line 57
def nested_filterable_field(name)
  filterable_field(name) || begin
    n = name.to_s.split('_')
    (n.size - 1).downto(1) do |count|
      if f = filterable_field(n[0, count].join('_').to_sym)
        return f.nform.nested_filterable_field(n[count..-1].join('_').to_sym)
      end
    end
    nil
  end
end
path() click to toggle source
# File lib/basepack/forms/query.rb, line 187
def path
  view.polymorphic_path([:query, association_chain, resource_class].flatten)
end
present?() click to toggle source
# File lib/basepack/forms/query.rb, line 82
def present?
  @query or @ql or filtered_fields.present?
end
render_field!(field) click to toggle source
# File lib/basepack/forms/query.rb, line 183
def render_field!(field)
  # TODO view.render field.partial
end
resource_filter() click to toggle source
# File lib/basepack/forms/query.rb, line 46
def resource_filter
  @resource_filter || begin
    query_from_params
    @resource_filter
  end
end
setup() click to toggle source
# File lib/basepack/forms/query.rb, line 86
def setup
  {
    options: {
      regional: { datePicker: { dateFormat: date_format }.reverse_merge!(Basepack::Forms::Edit.data_picker_options) },
      predicates: FilterQL.predicates,
      enum_options: enum_options,
    },
    initial: initial_data,
  }
end

Private Instance Methods

query_from_params() click to toggle source
# File lib/basepack/forms/query.rb, line 207
def query_from_params
  @resource_filter, @collection = Basepack::Utils.query_from_params(
    @scope,
    @params,
    {
      auth_object: @auth_object,
      filterql_options: @filterql_options,
    }
  )
  @collection = @collection.includes(@collection_includes) if @collection_includes
end