Class: Campo::Select

Inherits:
Base
  • Object
show all
Defined in:
lib/campo/campo.rb

Overview

For building ‘select’ tags.

Constant Summary

Constants inherited from Base

Base::DEFAULT

Instance Attribute Summary

Attributes inherited from Base

#attributes, #attributes The element's html attributes., #fields, #fields The element's child elements.

Attributes included from Childish

#parent

Instance Method Summary collapse

Methods inherited from Base

#each, #labelled, #on_output, #output, output, quotable, unhash

Methods included from Convenience

#bit_of_ruby, #checkbox, #fieldset, #hidden, #input, #literal, #password, #radio, #select, #submit, #text, #textarea

Methods included from Iding

#id_tag

Methods included from Childish

#push=

Constructor Details

#initialize(name, params = {}) ⇒ Select

option params [Hash] :attributes option params [#to_s] :haml_insert

Parameters:

  • :name (String)
  • :params (Hash)


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/campo/campo.rb', line 513

def initialize( name, params={} )
  opts = params.fetch :opts, []
  attributes = params.fetch :attributes, {}
  haml_insert = params.delete :haml_insert
  
  super( name, { tabindex: %q!#{@campo_tabindex += 1}! }.merge(attributes) )
  
  self.on_output do |n=0, tab=2|
    %Q!#{" " * n * tab}%select{ atts[:#{name.gsub(/\W/, "_").downcase}], #{Base.unhash( @attributes )} }! 
  end
  
  self.fields += Helpers.options_builder( name, opts ) unless opts.nil? || opts.empty?
  
  self.fields << Haml_Ruby_Insert.new( haml_insert ) unless haml_insert.nil?
    
  self
end

Instance Method Details

#option(*args) ⇒ Object

Examples:

# Select with a block of options
f.select("teas") do |s|
  s.with_default
  s.option("ceylon")
  s.option("breakfast")
  s.option("earl grey")
  s.option("oolong")
  s.option("sencha")
end.labelled("Favourite tea:")

# Select using chain of options
form.select("bands").option("Suede").option("Blur").option("Oasis").option("Echobelly").option("Pulp").option("Supergrass").with_default.labelled("Favourite band:")

select( "breads", {opts: [[1, "White"],[2,"Malted"],[3,"Black"],[4,"Wholemeal"], [5,"Rye"] ] })

Parameters:

  • value (#to_s)

    The value for attribute ‘value’.

  • inner (#to_s)

    The display text.

  • selected (true, false, nil)

    Whether the field is selected. Defaults to false.

  • attributes (Hash)

    Hash of attributes. They’ll get added to the element.



537
538
539
540
541
542
543
544
# File 'lib/campo/campo.rb', line 537

def option( *args )
  value = args.shift
  inner = args.shift 
  selected, attributes = *args
  inner = value.capitalize if inner.nil?
  self << Campo::Option.new( @attributes[:name], value, inner, selected, attributes )
  self
end

#with_default(inner = "Choose one:", attributes = {disabled: "disabled"}) ⇒ Object

Adds a default selection to a select list. By default it is disabled.

Examples:

As a default:
form.select("teas").with_default.option("ceylon")
# output:
  %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", name: "teas",  }
     %option{  value: "", disabled: "disabled", name: "teas",  }Choose one:
     %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas",  }Ceylon

form.select("teas").with_default("My fave tea is:").option("ceylon")
# output:
  %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", name: "teas",  }
  %option{  value: "", disabled: "disabled", name: "teas",  }My fave tea is:
  %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas",  }Ceylon

Parameters:

  • The (String, nil)

    display string for the option. Default is “Choose one:”.

  • attributes (Hash, nil) (defaults to: {disabled: "disabled"})

    Attributes for the option. Defaults to Hash[ :disabled => “disabled” ], pass in an empty hash to override (or a filled one), or nil for the default.



563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/campo/campo.rb', line 563

def with_default( inner="Choose one:", attributes={disabled: "disabled"} )
  unless inner.nil? || inner.kind_of?( String )
    attributes = inner
    inner = nil
  end
  
  inner ||="Choose one:"
  attributes ||= {disabled: "disabled"}
  attributes = {id: "#{@attributes[:name]}_default" }.merge! attributes
  self.fields.unshift Campo::Option.new( @attributes[:name], "", inner , nil, attributes )
  self
end