Module: Elastics::Result::RailsHelper::Highlighter

Defined in:
lib/elastics/result/rails_helper.rb

Constant Summary collapse

RE =
/highlighted_(\w+)/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elastics/result/rails_helper.rb', line 14

def method_missing(meth, *args, &block)
  meth.to_s =~ RE
  attribute = $1
  if attribute
    opts = {:fragment_separator => ' ... '}.merge(args.first||{})
    if self['highlight']
      key, high = self['highlight'].find { |k,v| k.gsub('.','_') == attribute }
      high = Array.wrap(high) if high
    end
    if high.blank?
      respond_to?(attribute.to_sym) ? send(attribute.to_sym) : ''
    else
      high.join(opts[:fragment_separator]).html_safe
    end
  else
    super
  end
end

Instance Method Details

#respond_to?(meth, private = false) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/elastics/result/rails_helper.rb', line 9

def respond_to?(meth, private=false)
  meth.to_s =~ RE
  !!$1 || super
end