class ReportsKits::FormBuilder

Constants

DEFAULT_DATE_RANGE_VALUE

Attributes

additional_params[RW]
context_record[RW]
properties[RW]
properties_to_filter[RW]

Public Class Methods

new(properties, additional_params: nil, context_record: nil) click to toggle source
# File lib/reports_kits/form_builder.rb, line 9
def initialize(properties, additional_params: nil, context_record: nil)
  self.properties = properties.deep_symbolize_keys
  self.additional_params = additional_params
  self.context_record = context_record
  self.properties_to_filter = Reports::PropertiesToFilter.new(properties, context_record: context_record)
end

Public Instance Methods

check_box(filter_key, options = {}) click to toggle source
# File lib/reports_kits/form_builder.rb, line 16
def check_box(filter_key, options = {})
  filter = properties_to_filter.perform(filter_key)
  checked = options.key?(:value) ? options[:value] : filter.normalized_properties[:criteria].try(:[], :value) == 'true'
  check_box_tag(filter_key, '1', checked, options)
end
date_range(filter_key, options = {}) click to toggle source
# File lib/reports_kits/form_builder.rb, line 22
def date_range(filter_key, options = {})
  filter = properties_to_filter.perform(filter_key)
  defaults = { class: 'form-control input-sm date_range_picker' }
  options = defaults.deep_merge(options)
  value = options[:value].presence || filter.normalized_properties[:criteria].try(:[], :value).presence
  value ||= default_date_range_value
  text_field_tag(filter_key, value, options)
end
multi_autocomplete(filter_key, options = {}) click to toggle source
# File lib/reports_kits/form_builder.rb, line 31
def multi_autocomplete(filter_key, options = {})
  filter = properties_to_filter.perform(filter_key)
  reports_kit_path = Rails.application.routes.url_helpers.reports_kit_path
  path = "#{reports_kit_path}reports_kit/filters/#{filter_key}/autocomplete?"
  path += additional_params.to_query if additional_params.present?

  defaults = {
    class: 'form-control input-sm select2',
    multiple: 'multiple',
    data: {
      placeholder: options[:placeholder],
      path: path
    }
  }
  options = defaults.deep_merge(options)
  select_tag(filter_key, nil, options)
end
string_filter(filter_key, options = {}) click to toggle source
# File lib/reports_kits/form_builder.rb, line 49
def string_filter(filter_key, options = {})
  filter = properties_to_filter.perform(filter_key)
  defaults = { class: 'form-control input-sm' }
  options = defaults.deep_merge(options)
  text_field_tag(filter_key, filter.normalized_properties[:criteria].try(:[], :value), options)
end

Private Instance Methods

default_date_range_value() click to toggle source
# File lib/reports_kits/form_builder.rb, line 58
def default_date_range_value
  @default_date_range_value ||= begin
    start_date = Reports::Data::Utils.format_time_value(DEFAULT_DATE_RANGE_VALUE[0])
    end_date = Reports::Data::Utils.format_time_value(DEFAULT_DATE_RANGE_VALUE[1])
    [start_date, Reports::FilterTypes::Datetime::SEPARATOR, end_date].join(' ')
  end
end