Class: CML::Tags::Search

Inherits:
CML::Tag show all
Defined in:
lib/cml/tags/search.rb

Constant Summary collapse

DEFAULT_CSE =
"002077591359011991755:yygrm1glnxq"
BareTemplate =
<<-HTML.freeze
  <div class="cml_row search" data-engine="{{engine}}" data-cse="{{custom_search_engine}}" >
    <input name="{{prefixed_search_terms_name}}" type="text" value="{{search_terms_value}}" class="{{search_terms_classes}}"/> <input type="button" value="Search" class="no_validate" />
  </div>
  <div class="cml_row results"></div>
  {{inputs}}
HTML
Template =
<<-HTML.freeze
  {{label}}
  #{BareTemplate}
HTML
InputTemplate =
<<-HTML.freeze
  <div class="cml_row selected_result">
    <input name="{{name}}" type="text" class="{{classes}}" value="{{search_result_value}}" />
  </div>
HTML

Constants included from CML::TagLogic

CML::TagLogic::And, CML::TagLogic::AndPhraseExp, CML::TagLogic::CombinatorDefault, CML::TagLogic::CombinatorDict, CML::TagLogic::CombinatorExp, CML::TagLogic::GroupExp, CML::TagLogic::Or, CML::TagLogic::OrCombinatorExp, CML::TagLogic::PrecedenceRegexp, CML::TagLogic::TokenExp, CML::TagLogic::TokenRegexp

Instance Attribute Summary

Attributes inherited from CML::Tag

#attrs, #cml, #opts, #tag

Attributes included from CML::TagLogic

#errors, #has_grouped_logic, #has_liquid_logic, #logic_tree

Instance Method Summary collapse

Methods inherited from CML::Tag

#cacheable?, #children, #classes, #convert, #ensure_resolved, #finite_value?, #gold=, #gold?, #gold_class, #gold_reason, #initialize, #instructions, #iterating?, #label, #legend, memoize, #multi_type, #name, #parent_multiples, #prefix, #preloaded_data, #raw_label, #to_liquid, #to_s, #validate?, #validations, #validators, #value, #wrapper

Methods included from CML::TagLogic

#dependencies_on_fields, #dependencies_through_cml_group, #depends_on_fields, #describe_logic_token, #detect_grouped_logic, #detect_liquid_logic, #each_logic_token_in, #expand_logic, #expand_parsed_expression, #keep_merge!, #only_if, parse_expression, resolve_combinator

Constructor Details

This class inherits a constructor from CML::Tag

Instance Method Details

#custom_search_engineObject



40
41
42
# File 'lib/cml/tags/search.rb', line 40

def custom_search_engine
  engine.to_s == "googlecse" ? (@attrs["cse"] || DEFAULT_CSE).to_s : "" 
end

#dataObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cml/tags/search.rb', line 62

def data
  super.merge({
    "search_terms_classes" => search_terms_classes,
    "engine" => engine,
    "search_terms_name" => search_terms_name,
    "prefixed_search_terms_name" => prefixed_search_terms_name,
    "search_terms_value" => search_terms_value,
    "search_result_value" => search_result_value,
    "custom_search_engine" => custom_search_engine
  })
end

#engineObject



36
37
38
# File 'lib/cml/tags/search.rb', line 36

def engine
  (@attrs["engine"] ? @attrs["engine"].to_s : nil) || "googlecse"
end

#in_logic_graph?Boolean

Returns:

  • (Boolean)


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

def in_logic_graph?
  is_field?
end

#is_field?Boolean

Returns:

  • (Boolean)


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

def is_field?
  !(@attrs["field"] && @attrs["field"].to_s == "false")
end

#prefixed_search_terms_nameObject



48
49
50
# File 'lib/cml/tags/search.rb', line 48

def prefixed_search_terms_name
  prefix(search_terms_name)
end

#search_result_valueObject



57
58
59
60
# File 'lib/cml/tags/search.rb', line 57

def search_result_value
  val = (@attrs["value"] || (@opts[:data] || {})[name] || "")
  val.is_a?(Array) ? val[0].to_s : val.to_s
end

#search_terms_classesObject



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

def search_terms_classes
  (@attrs["default"] ? "default " : "") + engine + " no_validate"
end

#search_terms_nameObject



44
45
46
# File 'lib/cml/tags/search.rb', line 44

def search_terms_name
  name(true, "search_terms")
end

#search_terms_valueObject



52
53
54
55
# File 'lib/cml/tags/search.rb', line 52

def search_terms_value
  val = ((@opts[:data] || {})[search_terms_name] || @attrs["default"])
  val.is_a?(Array) ? val[0].to_s : val.to_s
end

#to_htmlObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cml/tags/search.rb', line 74

def to_html
  if is_field?
    if @opts[:normalize]
      seeds = Array((@opts[:data] || {})[name])
      seeds << nil if seeds.length == 0
    
      inputs = seeds.map do |input|
        Liquid::Template.parse(InputTemplate).render(data.merge({
          "search_result_value" => input.to_s, 
          "classes" => classes+(input ? " cml_gold_loaded" : "")
        }))
      end.join("\n")
    else
      inputs = Liquid::Template.parse(InputTemplate).render(data)
    end
  else
    inputs = ""
  end
  
  wrapper(Liquid::Template.parse(Template).render(data.merge({
    "inputs" => inputs
  }), [LiquidFilters]))
end

#wrapper_classesObject



98
99
100
# File 'lib/cml/tags/search.rb', line 98

def wrapper_classes
  super + " text"
end