Class: Campo::Select

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

Overview

Literal

Constant Summary

Constants inherited from Base

Base::DEFAULT

Instance Attribute Summary

Attributes inherited from Base

#attributes, #fields

Attributes included from Childish

#parent

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Convenience

#bit_of_ruby, #checkbox, #fieldset, #input, #literal, #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.



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/campo.rb', line 337

def initialize( name, params={} )
  opts = params[:opts] || []
  attributes = params[:attributes] || {}
  haml_insert = params[:haml_insert] || nil
  
  super( name, { tabindex: %q!#{i += 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:")


357
358
359
360
361
362
363
364
# File 'lib/campo.rb', line 357

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:") ⇒ Object

Examples:

As a default:
form.select("teas").with_default.option("ceylon")
# output:
  %select{ atts[:teas], tabindex: "#{i += 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: "#{i += 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


380
381
382
383
# File 'lib/campo.rb', line 380

def with_default( inner="Choose one:" )
  self.fields.unshift Campo::Option.new( @attributes[:name], "", inner , nil, {disabled: "disabled" } )
  self
end