class SimpleDrilldown::Search

Attributes

default_fields[R]
dimensions[R]
display_type[R]
fields[R]
filter[R]
list[RW]
list_change_times[R]
order_by_value[R]
percent[RW]
select_value[R]
title[R]

Public Class Methods

human_attribute_name(attribute) click to toggle source
# File lib/simple_drilldown/search.rb, line 28
def self.human_attribute_name(attribute)
  attribute
end
new(attributes_or_search, default_fields = nil, default_select_value = SelectValue::COUNT) click to toggle source
# File lib/simple_drilldown/search.rb, line 32
def initialize(attributes_or_search, default_fields = nil, default_select_value = SelectValue::COUNT)
  if attributes_or_search.is_a? self.class
    s = attributes_or_search
    @dimensions = s.dimensions.dup
    @display_type = s.display_type.dup
    @fields = s.fields.dup
    @filter = s.filter.dup
    @list = s.list
    @percent = s.percent
    @list_change_times = s.list_change_times
    @order_by_value = s.order_by_value
    @select_value = s.select_value.dup
    @title = s.title
    @default_fields = s.default_fields
  else
    attributes = attributes_or_search
    @default_fields = default_fields
    @default_select_value = default_select_value
    @dimensions = (attributes && attributes[:dimensions]) || []
    @dimensions.delete_if(&:empty?)
    @filter = attributes && attributes[:filter] ? attributes[:filter] : {}
    @filter.keys.dup.each { |k| @filter[k] = Array(@filter[k]) }
    @filter.each_value do |v|
      v.delete('')
      v.delete('Select Some Options')
    end
    @filter.delete_if { |_k, v| v.empty? }
    @display_type = attributes && attributes[:display_type] ? attributes[:display_type] : DisplayType::NONE
    @display_type = DisplayType::BAR if @dimensions.size >= 2 && @display_type == DisplayType::PIE

    @order_by_value = attributes && (attributes[:order_by_value] == '1')
    @select_value = attributes&.dig(:select_value).presence&.to_sym || @default_select_value
    @list = attributes&.[](:list) == '1'
    @percent = attributes&.[](:percent) == '1'
    @list_change_times = attributes&.[](:list_change_times) == '1'
    @fields = if attributes && attributes[:fields]
                if attributes[:fields].is_a?(Array)
                  attributes[:fields]
                else
                  attributes[:fields].to_h.select { |_k, v| v == '1' }.map { |k, _v| k }
                end
              else
                @default_fields
              end
    @title = attributes[:title] if attributes&.dig(:title).present?
  end
end
validators_on(_attribute) click to toggle source
# File lib/simple_drilldown/search.rb, line 24
def self.validators_on(_attribute)
  []
end

Public Instance Methods

drill_down(dimensions, *values) click to toggle source
# File lib/simple_drilldown/search.rb, line 105
def drill_down(dimensions, *values)
  raise 'Too many values' if values.size > self.dimensions.size

  s = self.class.new(self)
  values.each_with_index { |v, i| s.filter[dimensions[i][:url_param_name]] = [v] }
  values.size.times { s.dimensions.shift }
  s
end
id() click to toggle source

Used for DOM id

# File lib/simple_drilldown/search.rb, line 97
def id
  'SEARCH'
end
list?() click to toggle source
# File lib/simple_drilldown/search.rb, line 101
def list?
  list
end
to_key() click to toggle source
# File lib/simple_drilldown/search.rb, line 114
def to_key
  url_options.to_a
end
url_options() click to toggle source
# File lib/simple_drilldown/search.rb, line 80
def url_options
  o = {
    search: {
      title:,
      list: list ? '1' : '0',
      percent: percent ? '1' : '0',
      list_change_times: list_change_times ? '1' : '0',
      filter:,
      dimensions:,
      display_type:
    }
  }
  o[:search][:fields] = fields unless fields == @default_fields
  o
end