Class: SimpleDrilldown::Search

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/simple_drilldown/search.rb

Defined Under Namespace

Modules: DisplayType, SelectValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes_or_search, default_fields = nil, default_select_value = SelectValue::COUNT) ⇒ Search

Returns a new instance of Search.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simple_drilldown/search.rb', line 34

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 do |_k, 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).present? ? attributes[:select_value] : @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

Instance Attribute Details

#default_fieldsObject (readonly)

Returns the value of attribute default_fields.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def default_fields
  @default_fields
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def dimensions
  @dimensions
end

#display_typeObject (readonly)

Returns the value of attribute display_type.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def display_type
  @display_type
end

#fieldsObject (readonly)

Returns the value of attribute fields.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def fields
  @fields
end

#filterObject (readonly)

Returns the value of attribute filter.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def filter
  @filter
end

#listObject

Returns the value of attribute list.



24
25
26
# File 'lib/simple_drilldown/search.rb', line 24

def list
  @list
end

#list_change_timesObject (readonly)

Returns the value of attribute list_change_times.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def list_change_times
  @list_change_times
end

#order_by_valueObject (readonly)

Returns the value of attribute order_by_value.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def order_by_value
  @order_by_value
end

#percentObject

Returns the value of attribute percent.



24
25
26
# File 'lib/simple_drilldown/search.rb', line 24

def percent
  @percent
end

#select_valueObject (readonly)

Returns the value of attribute select_value.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def select_value
  @select_value
end

#titleObject (readonly)

Returns the value of attribute title.



22
23
24
# File 'lib/simple_drilldown/search.rb', line 22

def title
  @title
end

Class Method Details

.human_attribute_name(attribute) ⇒ Object



30
31
32
# File 'lib/simple_drilldown/search.rb', line 30

def self.human_attribute_name(attribute)
  attribute
end

.validators_on(_attribute) ⇒ Object



26
27
28
# File 'lib/simple_drilldown/search.rb', line 26

def self.validators_on(_attribute)
  []
end

Instance Method Details

#drill_down(dimensions, *values) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/simple_drilldown/search.rb', line 107

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

#idObject

Used for DOM id



99
100
101
# File 'lib/simple_drilldown/search.rb', line 99

def id
  'SEARCH'
end

#list?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/simple_drilldown/search.rb', line 103

def list?
  list
end

#to_keyObject



116
117
118
# File 'lib/simple_drilldown/search.rb', line 116

def to_key
  url_options.to_a
end

#url_optionsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/simple_drilldown/search.rb', line 82

def url_options
  o = {
    search: {
      title: title,
      list: list ? '1' : '0',
      percent: percent ? '1' : '0',
      list_change_times: list_change_times ? '1' : '0',
      filter: filter,
      dimensions: dimensions,
      display_type: display_type,
    },
  }
  o[:search][:fields] = fields unless fields == @default_fields
  o
end