Class: Campo::Select
Overview
For building ‘select’ tags.
Constant Summary
Constants inherited from Base
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
Instance Method Summary collapse
-
#initialize(name, params = {}) ⇒ Select
constructor
option params [Hash] :attributes option params [#to_s] :haml_insert.
- #option(*args) ⇒ Object
-
#with_default(inner = "Choose one:", attributes = {disabled: "disabled"}) ⇒ Object
Adds a default selection to a select list.
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
Methods included from Childish
Constructor Details
#initialize(name, params = {}) ⇒ Select
option params [Hash] :attributes option params [#to_s] :haml_insert
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.( 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
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.
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 |