Skip to content

Commit

Permalink
Labelled can now take an attribute hash; improved the docs slightly a…
Browse files Browse the repository at this point in the history
…nd got rid of a warning;
  • Loading branch information
Iain Barnett committed Dec 16, 2012
1 parent 44cb02d commit 72661d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/campo/campo.rb
Expand Up @@ -281,12 +281,14 @@ def output( n=0, tab=2 )


# Bit of a convenience method for adding a label around any element.
# @param [String] inner The text for the label.
# @param [String] :inner The text for the label.
# @param [Hash] :attributes Attributes for the label.
# @return [Base]
def labelled( inner=nil )
# @see Label#initialize
def labelled( inner=nil, attributes={} )
inner ||= self.attributes[:name].gsub(/\[\]/, "").gsub("_"," ").capitalize
parent = self.parent
label = Label.new( %Q!#{@attributes[:id]}!, inner ) << self
label = Label.new( %Q!#{@attributes[:id]}!, inner, attributes ) << self
retval = if parent.nil?
label
else
Expand Down Expand Up @@ -540,7 +542,7 @@ def option( *args )

# Adds a default selection to a select list. By default it is disabled.
# @param [String,nil] The display string for the option. Default is "Choose one:".
# @param [Hash,nil] attributes Attributes for the option. Defaults to {disabled: "disabled"}, pass in an empty hash to override (or a filled one), or nil for the default.
# @param [Hash,nil] attributes 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.
# @example
# As a default:
# form.select("teas").with_default.option("ceylon")
Expand Down
4 changes: 2 additions & 2 deletions spec/campo_spec.rb
Expand Up @@ -40,7 +40,7 @@ module Campo
context "Given no form" do
context "When given a select field with options" do
let(:expected) { %q!
%label{ for: "teas", }
%label{ class: "list", for: "teas", }
Favourite tea:
%select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", id: "teas", name: "teas", }
%option{ atts[:teas_default], id: "teas_default", value: "", disabled: "disabled", name: "teas", }Choose one:
Expand All @@ -51,7 +51,7 @@ module Campo
}
let(:tag) {
select = Campo::Select.new( "teas" )
tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:", class: "list")
tag
}
subject { Campo.output tag, :partial=>true }
Expand Down

0 comments on commit 72661d6

Please sign in to comment.