Module: Campo::Convenience

Included in:
Base
Defined in:
lib/campo/campo.rb

Overview

Here to make life a bit easier and cut down on RSI.

Instance Method Summary collapse

Instance Method Details

#bit_of_ruby(*args, &block) ⇒ Object Also known as: haml_ruby_insert

Examples:

Add a bit of code to the markup

form.bit_of_ruby( "= 5 + 1" ) }

76
77
78
79
80
# File 'lib/campo/campo.rb', line 76

def bit_of_ruby( *args, &block  )
  tag = Campo::Haml_Ruby_Insert.new( *args, &block  )
  self << tag
  tag
end

#checkbox(name, label = nil, attributes = {}) ⇒ Object

Parameters:

  • name (String)

    The name html attribute.

  • label (optional, String, nil) (defaults to: nil)

    Give the label a name. Defaults to a capitalised name with _ replaced by spaces.

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


151
152
153
# File 'lib/campo/campo.rb', line 151

def checkbox( name, label=nil, attributes={} )
  input( name, :checkbox, label, attributes )
end

#fieldset(text, attributes = {}, &block) ⇒ Object

Examples:

Fieldset as a block is easiest to read

form.fieldset("Your details") do |f|
  f.text( "full_name",  size: 60 )
  f.text( "dob", "Date of birth: ", size: 8 )
end

Parameters:

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


69
70
71
# File 'lib/campo/campo.rb', line 69

def fieldset( text, attributes={}, &block )
  self << Fieldset.new( text, attributes, &block )
end

#hidden(name, attributes = {}) ⇒ Object


133
134
135
# File 'lib/campo/campo.rb', line 133

def hidden( name, attributes={}  )
  self << Campo::Input.new( name, :hidden, attributes )
end

#input(name, type, label = nil, attributes = {}) ⇒ Object

Parameters:

  • type (:symbol)

    The type html attribute.

  • name (String)

    The name html attribute.

  • label (optional, String, nil) (defaults to: nil)

    Give the label a name. Defaults to a capitalised name with _ replaced by spaces.

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


158
159
160
161
162
163
164
165
166
167
# File 'lib/campo/campo.rb', line 158

def input( name, type, label=nil, attributes={} )
  if label.kind_of? Hash
    attributes = label
    label = nil
  end

  field = Campo::Input.new( name, type, attributes ).labelled( label )
  self << field
  field
end

#literal(*args, &block) ⇒ Object

Examples:

Output a literal string

form.literal %Q!%p= "This is a paragraph "!

See Also:


88
89
90
91
92
# File 'lib/campo/campo.rb', line 88

def literal( *args, &block  )
  tag = Campo::Literal.new( *args, &block  )
  self << tag
  tag
end

#password(name, label = nil, attributes = {}) ⇒ Object

Parameters:

  • name (String)

    The name html attribute.

  • label (optional, String, nil) (defaults to: nil)

    Give the label a name. Defaults to a capitalised name with _ replaced by spaces.

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


139
140
141
# File 'lib/campo/campo.rb', line 139

def password( name, label=nil, attributes={}  )
  input( name, :password, label, attributes  )
end

#radio(name, label = nil, attributes = {}) ⇒ Object

Parameters:

  • name (String)

    The name html attribute.

  • label (optional, String, nil) (defaults to: nil)

    Give the label a name. Defaults to a capitalised name with _ replaced by spaces.

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


145
146
147
# File 'lib/campo/campo.rb', line 145

def radio( name, label=nil, attributes={} )
  input( name, :radio, label, attributes )
end

#select(*args, &block) ⇒ 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"] ] })

See Also:


112
113
114
115
116
# File 'lib/campo/campo.rb', line 112

def select( *args, &block )
  select = Campo::Select.new( *args, &block )
  self << select
  select
end

#submit(name = "Submit", attributes = {}) ⇒ Object

Parameters:

  • name (optional, String) (defaults to: "Submit")
  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.


172
173
174
175
176
# File 'lib/campo/campo.rb', line 172

def submit( name="Submit", attributes={} )
  submit = Campo::Input.new( name, :submit, {value: name}.merge(attributes) )
  self << submit
  submit
end

#text(name, label = nil, attributes = {}) ⇒ Input

Add an input with type of text

Examples:

f.text "full_name",  size: 60 
f.text "dob", "Date of birth: ", size: 8

Parameters:

  • name (String)

    The name html attribute.

  • label (optional, String, nil) (defaults to: nil)

    Give the label a name. Defaults to a capitalised name with _ replaced by spaces.

  • attributes (optional, Hash) (defaults to: {})

    Any attributes you wish to add to the haml element.

Returns:

  • (Input)

    With the attribute `type=text`


128
129
130
# File 'lib/campo/campo.rb', line 128

def text( name, label=nil, attributes={}  )
  input( name, :text, label, attributes  )
end

#textarea(name, inner = nil, attributes = {}, &block) ⇒ Object

There is no easy way to give this convenience method in the same convention as the other methods, so this uses a hash argument for the label

Examples:

textarea "name", "The text wrapped inside", label: "Example textarea"

182
183
184
185
186
187
188
189
190
191
# File 'lib/campo/campo.rb', line 182

def textarea( name,  inner=nil, attributes={}, &block  ) 
  if inner.kind_of? Hash
    attributes = inner
    inner = nil
  end
  label = attributes.delete(:label) || attributes.delete(:labelled)
  textarea = Campo::Textarea.new( name, inner, attributes ).labelled( label )
  self << textarea
  textarea
end