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.



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
81
82
83
84
85
86
87
88
# File 'lib/simple_drilldown/search.rb', line 42

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] = [*@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.



32
33
34
# File 'lib/simple_drilldown/search.rb', line 32

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.



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

def display_type
  @display_type
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#listObject

Returns the value of attribute list.



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

def list
  @list
end

#list_change_timesObject (readonly)

Returns the value of attribute list_change_times.



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

def list_change_times
  @list_change_times
end

#order_by_valueObject (readonly)

Returns the value of attribute order_by_value.



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

def order_by_value
  @order_by_value
end

#percentObject

Returns the value of attribute percent.



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

def percent
  @percent
end

#select_valueObject (readonly)

Returns the value of attribute select_value.



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

def select_value
  @select_value
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.human_attribute_name(attribute) ⇒ Object



38
39
40
# File 'lib/simple_drilldown/search.rb', line 38

def self.human_attribute_name(attribute)
  attribute
end

.validators_on(_attribute) ⇒ Object



34
35
36
# File 'lib/simple_drilldown/search.rb', line 34

def self.validators_on(_attribute)
  []
end

Instance Method Details

#drill_down(dimensions, *values) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/simple_drilldown/search.rb', line 111

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



107
108
109
# File 'lib/simple_drilldown/search.rb', line 107

def id
  'SEARCH'
end

#to_keyObject



120
121
122
# File 'lib/simple_drilldown/search.rb', line 120

def to_key
  url_options.to_a
end

#url_optionsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_drilldown/search.rb', line 90

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