Class: Locomotive::SearchExt::CustomType

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/locomotive/search_ext/plugin/custom_type.rb

Constant Summary collapse

Syntax =
/(#{::Liquid::QuotedFragment})\s*on fields\s*(#{::Liquid::QuotedFragment})\s*to\s*(#{::Liquid::QuotedFragment})/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ CustomType

Returns a new instance of CustomType.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/locomotive/search_ext/plugin/custom_type.rb', line 9

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @content_type_slug = $1
    @fields = $2.split(',')
    @target = $3
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type - Valid syntax: search_custom_type 'content_type_slug' on fields 'field_a, field_b' to results")
  end

  #@site = context.registers[:site]
  #content_type = @site.content_type.where(slug: @content_type_slug).first
  #raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type': #{@content_type_slug} is not a content type ") unless content_type
  #real_fields = content_type.entries_custom_fields.collect(&:label)
  #@fields.each do  |field|
  #  unless real_fields.include?(field)
  #    raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type':#{field} is not a field on #{@content_type_slug}")
  #  end
  #end
  super
end

Instance Method Details

#render(context) ⇒ Object

Raises:

  • (::Liquid::Error)


30
31
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
79
80
81
82
83
84
85
86
87
# File 'lib/locomotive/search_ext/plugin/custom_type.rb', line 30

def render(context)
  @site = context.registers[:site]
  content_type = @site.content_types.where(slug: @content_type_slug).first
  raise ::Liquid::Error.new("Error in 'search_custom_type': #{@content_type_slug} is not a content type ") unless content_type
  @fields.each do |field|
    unless content_type.entries.first.respond_to?(field)
      raise ::Liquid::Error.new("Error in 'search_custom_type':#{field} is not a field on #{@content_type_slug}")
    end
  end

  #profile = RubyProf.profile do
  search = nil
  search_terms = context.registers[:controller].params[:search]
  if search_terms and !search_terms.empty?
    Rails.logger.warn "Params: #{search_terms}, { \"site_id\" => #{@site.id}, \"content_type_slug\" => #{@content_type_slug} }"
    search = ::ActiveSearch.search(
      search_terms,
      {
      "site_id" => @site.id,
      "content_type_slug" => @content_type_slug,
    }
    )
  end

  @fields.select! do |field|
    param = context.registers[:controller].params[field]
    param && !param.empty?
  end

  if search
    slugs = {}
    search.each do |result,score|
      Rails.logger.warn "search: #{result}"
      slugs[result['_slug']] = score
    end
  end

  Rails.logger.warn "Seach Slugs: #{slugs}"
  final_results = []
  content_type.ordered_entries(context["with_scope"]).each do |entry|
    if !search or slugs.has_key?(entry._slug)
      if match_fields(context, entry)
        idx = 0
        idx = slugs[entry._slug] if search
        final_results[idx] ||= []
        final_results[idx] << entry.to_liquid
      end
    end
  end

  context[@target.to_s] = final_results.reverse.flatten.compact
  #end
  # Print a graph profile to text
  #printer = RubyProf::MultiPrinter.new(profile)
  #printer.print(:path => '/home/greeneca/colibri/tmp/profile')

  ""
end