Class: Campo::Select

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

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

Returns a new instance of Select.



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/campo/campo.rb', line 499

def initialize( name, params={} )
  opts = params[:opts] || []
  attributes = params[:attributes] || {}
  haml_insert = params[:haml_insert] || nil
  
  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:")


519
520
521
522
523
524
525
526
# File 'lib/campo/campo.rb', line 519

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 “disabled”, pass in an empty hash to override (or a filled one), or nil for the default.



545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/campo/campo.rb', line 545

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