Class: MmEsSearch::Api::Facet::RangeFacetRow

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, MongoMapper::EmbeddedDocument
Defined in:
lib/mm_es_search/api/facet/range_facet_row.rb

Instance Method Summary collapse

Instance Method Details

#attributes(*args) ⇒ Object Also known as: to_mongo



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 28

def attributes(*args)
  attr = super
  attr.each_with_object({}) do |(key, value), hsh|
    hsh[key] = case value
    when ActiveSupport::TimeWithZone
      value.utc.to_time
    else
      value
    end
  end
end

#from=(val) ⇒ Object



22
23
24
25
26
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 22

def from=(val)
  super.tap do
    binding.pry if from.is_a?(DateTime)
  end
end

#parentObject



89
90
91
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 89

def parent
  _parent_document
end

#render_numeric(pretty_print) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 60

def render_numeric(pretty_print)
  
  if pretty_print
    #TODO handle units (see http://bit.ly/rhx05t)
    from_formatted = number_to_human(from) if from?
    to_formatted = number_to_human(to) if to?
  else
    from_formatted = from if from?
    to_formatted = to if to?
  end
  
  if from? and to?
    "from #{from_formatted} to #{to_formatted}"
  elsif from?
    "#{from_formatted} or greater"
  else
    "upto #{to_formatted}"
  end
  
end

#to_english(pretty_print = true) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 52

def to_english(pretty_print = true)
  case from || to
  when Numeric
    render_numeric(pretty_print)
  when Time, DateTime
  end
end

#to_form_name(data_type) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 81

def to_form_name(data_type)
  params = []
  params << "data_type:#{data_type}"
  params << "from:#{from}" if from?
  params << "to:#{to}" if to?
  return params.join('&')
end

#to_range_itemObject



45
46
47
48
49
50
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 45

def to_range_item
  RangeItem.new(
    :from => from,
    :to   => to
  )
end

#zero_countObject



41
42
43
# File 'lib/mm_es_search/api/facet/range_facet_row.rb', line 41

def zero_count
  self.count = 0
end