Class: CML::Tags::Select

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

Constant Summary collapse

Template =
<<-HTML.freeze
{{label}}
<div class="cml_row"><select name="{{name}}" class="{{classes}}">
  {{content}}
</select></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?, #classes, #convert, #ensure_resolved, #gold=, #gold?, #gold_class, #gold_reason, #initialize, #instructions, #iterating?, #label, #legend, memoize, #multi_type, #name, #parent_multiples, #prefix, #preloaded_data, #raw_label, #to_html, #to_liquid, #to_s, #validate?, #validations, #validators, #value, #wrapper, #wrapper_classes

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, #in_logic_graph?, #keep_merge!, #only_if, parse_expression, resolve_combinator

Constructor Details

This class inherits a constructor from CML::Tag

Instance Method Details

#childrenObject



18
19
20
21
22
# File 'lib/cml/tags/select.rb', line 18

def children
  default + @cml.xpath(".//cml:option").map do |c|
    Option.new(c, @opts)
  end
end

#contentObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cml/tags/select.rb', line 29

def content
  cloned= @cml.dup
  c = children
  # Add the default...
  d = default
  # Preloaded data
  preload = preloaded_data
  
  cloned.children.first.add_previous_sibling(d[0].cml) if d.length > 0
  cloned.xpath(".//cml:*[not(self::cml:option)]").remove
  cloned.xpath(".//cml:option").each_with_index do |r,i|
    r.namespace = nil
    c[i].selected = true if preload && preload == c[i].value
    r.replace(c[i].convert)
  end
  #Remove the surrounding tag
  cloned.to_xhtml.gsub(/<\/?cml:select[^>]*?>|<\/?>/,'')
end

#dataObject



25
26
27
# File 'lib/cml/tags/select.rb', line 25

def data
  super.merge({"content" => content})
end

#defaultObject

Auto magically prepend a default option if required



12
13
14
15
16
# File 'lib/cml/tags/select.rb', line 12

def default
  default = [Option.new("<cml:option value=\"\">#{@cml["default"]}</cml:option>", @opts)] if @cml["default"]
  default ||= [Option.new("<cml:option value=\"\">Select one</cml:option>", @opts)] if validations.include?('validates-required')
  default || [] 
end

#finite_value?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/cml/tags/select.rb', line 49

def finite_value?
  true
end