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.



32
33
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
# 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 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).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

Instance Attribute Details

#default_fieldsObject (readonly)

Returns the value of attribute default_fields.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def default_fields
  @default_fields
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def dimensions
  @dimensions
end

#display_typeObject (readonly)

Returns the value of attribute display_type.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def display_type
  @display_type
end

#fieldsObject (readonly)

Returns the value of attribute fields.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def fields
  @fields
end

#filterObject (readonly)

Returns the value of attribute filter.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def filter
  @filter
end

#listObject

Returns the value of attribute list.



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

def list
  @list
end

#list_change_timesObject (readonly)

Returns the value of attribute list_change_times.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def list_change_times
  @list_change_times
end

#order_by_valueObject (readonly)

Returns the value of attribute order_by_value.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def order_by_value
  @order_by_value
end

#percentObject

Returns the value of attribute percent.



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

def percent
  @percent
end

#select_valueObject (readonly)

Returns the value of attribute select_value.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def select_value
  @select_value
end

#titleObject (readonly)

Returns the value of attribute title.



20
21
22
# File 'lib/simple_drilldown/search.rb', line 20

def title
  @title
end

Class Method Details

.human_attribute_name(attribute) ⇒ Object



28
29
30
# File 'lib/simple_drilldown/search.rb', line 28

def self.human_attribute_name(attribute)
  attribute
end

.validators_on(_attribute) ⇒ Object



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

def self.validators_on(_attribute)
  []
end

Instance Method Details

#drill_down(dimensions, *values) ⇒ Object



105
106
107
108
109
110
111
112
# 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

#idObject

Used for DOM id



97
98
99
# File 'lib/simple_drilldown/search.rb', line 97

def id
  'SEARCH'
end

#list?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/simple_drilldown/search.rb', line 101

def list?
  list
end

#to_keyObject



114
115
116
# File 'lib/simple_drilldown/search.rb', line 114

def to_key
  url_options.to_a
end

#url_optionsObject



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

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