Class: Ramaze::Helper::BlueForm::Form
- Inherits:
-
Object
- Object
- Ramaze::Helper::BlueForm::Form
- Defined in:
- lib/ramaze/helper/blue_form.rb
Overview
Main form class that contains all the required methods to generate form specific tags, such as textareas and select boxes. Do note that this class is not thread-safe so you should modify it only within one thread of execution.
Instance Attribute Summary collapse
-
#form_values ⇒ Object
readonly
Returns the value of attribute form_values.
-
#g ⇒ Object
readonly
Returns the value of attribute g.
Instance Method Summary collapse
-
#build(form_errors = {}) ⇒ Object
Builds the form by generating the opening/closing tags and executing the methods in the block.
-
#fieldset(&block) ⇒ Object
Generate a fieldset tag.
-
#initialize(form_values, options) ⇒ Object
constructor
Constructor method that generates an instance of the Form class.
-
#input_checkbox(label, name, checked = nil, args = {}) ⇒ Object
(also: #checkbox)
Generate an input tag with a type of “checkbox”.
-
#input_file(label, name, args = {}) ⇒ Object
(also: #file)
Generate a field for uploading files.
-
#input_hidden(name, value = nil, args = {}) ⇒ Object
(also: #hidden)
Generate a hidden field.
-
#input_password(label, name, args = {}) ⇒ Object
(also: #password)
Generate an input tag with a type of “password” along with a label.
-
#input_radio(label, name, checked = nil, args = {}) ⇒ Object
(also: #radio)
Generate an input tag with a type of “radio”.
-
#input_submit(value = nil, args = {}) ⇒ Object
(also: #submit)
Generate a submit tag (without a label).
-
#input_text(label, name, args = {}) ⇒ Object
(also: #text)
Generate an input tag with a type of “text” along with a label tag.
-
#legend(text) ⇒ Object
Generate a ‘<legend>` tag.
-
#select(label, name, args = {}) ⇒ Object
Generate a select tag along with the option tags and a label.
-
#textarea(label, name, args = {}) ⇒ Object
Generate a text area.
-
#to_s ⇒ String
Method used for converting the results of the BlueForm helper to a string.
Constructor Details
#initialize(form_values, options) ⇒ Object
Constructor method that generates an instance of the Form class.
166 167 168 169 170 |
# File 'lib/ramaze/helper/blue_form.rb', line 166 def initialize(form_values, ) @form_values = form_values @form_args = .dup @g = Gestalt.new end |
Instance Attribute Details
#form_values ⇒ Object (readonly)
Returns the value of attribute form_values.
156 157 158 |
# File 'lib/ramaze/helper/blue_form.rb', line 156 def form_values @form_values end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
155 156 157 |
# File 'lib/ramaze/helper/blue_form.rb', line 155 def g @g end |
Instance Method Details
#build(form_errors = {}) ⇒ Object
Builds the form by generating the opening/closing tags and executing the methods in the block.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ramaze/helper/blue_form.rb', line 178 def build(form_errors = {}) # Convert all the keys in form_errors to strings and # retrieve the correct values in case @form_errors = {} form_errors.each do |key, value| if value.respond_to?(:first) value = value.first end @form_errors[key.to_s] = value end @g.form(@form_args) do if block_given? yield self end end end |
#fieldset(&block) ⇒ Object
Generate a fieldset tag.
222 223 224 |
# File 'lib/ramaze/helper/blue_form.rb', line 222 def fieldset(&block) @g.fieldset(&block) end |
#input_checkbox(label, name, checked = nil, args = {}) ⇒ Object Also known as: checkbox
Generate an input tag with a type of “checkbox”.
If you want to have multiple checkboxes you can either use an array or a hash. In the case of an array the values will also be used as text for each checkbox. When using a hash the key will be displayed and the value will be the value of the checkbox. Example:
@data = Class.new
attr_reader :gender_arr
attr_reader :gender_hash
def initialize
@gender_arr = ['male', 'female']
@gender_hash = {"Male" => "male", "Female" => "female"}
end
end.new
form_for(@data, :method => :post) do |f|
f.input_checkbox "Gender", :gender_arr
f.input_checkbox "Gender", :gender_hash
end
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/ramaze/helper/blue_form.rb', line 355 def input_checkbox(label, name, checked = nil, args = {}) id = args[:id] ? args[:id] : "#{id_for(name)}_0" # Determine whether or not to show the value of the checkbox if args.key?(:show_value) show_value = args.delete(:show_value) else show_value = true end # Determine whether or not to show the label if args.key?(:show_label) show_label = args.delete(:show_label) else show_label = true end # Get the checkbox value from either the args hash or from # the form object (as specified in the form_for() method). if !args[:values] and @form_values.respond_to?(name) args[:values] = @form_values.send(name) end # That class for each element wrapper (a span tag) can be customized # using :span_class => "a_class". if args[:span_class] span_class = args[:span_class] args.delete(:span_class) else span_class = "checkbox_wrap" end # Get the type from the args hash instead of pre-defining it. Doing so # means we can use this method for the input_radio method. args[:type] = :checkbox if !args[:type] # Convert the values to an array if it's something we can't use in a loop # (e.g. a string). if args[:values].class != Hash and args[:values].class != Array args[:values] = [args[:values]] end # Create a checkbox for each value if !args[:values].empty? @g.p do # Let's create the label and the hidden field if show_label === true label_for(id, label, name) end # Loop through all the values. Each checkbox will have an ID of # "form-NAME-INDEX". Each name will be NAME followed by [] to # indicate it's an array (since multiple values are possible). args[:values].each_with_index do |value, index| id = args[:id] ? args[:id] : "#{id_for(name)}_#{index}" if args[:type] == :checkbox checkbox_name = "#{name}[]" else checkbox_name = name end # Copy all additional attributes and their values except the # values array. opts = args.clone opts.delete(:values) # Get the value and text to display for each checkbox if value.class == Array checkbox_text = value[0] checkbox_value = value[1] else checkbox_text = checkbox_value = value end # Let's see if the current item is checked if checked.class == Array if checked.include?(checkbox_value) opts[:checked] = 'checked' end else if checkbox_value == checked opts[:checked] = 'checked' end end # And we're done, easy wasn't it? opts = opts.merge( :name => checkbox_name, :id => id, :value => checkbox_value ) # Generate the following HTML: # # <span class="#{span_class}"> # <input type="checkbox" name="#{checkbox_name}" id="#{id}" # value="#{value}" /> #{value} # </span> # @g.span(:class => span_class) do @g.input(opts) " #{checkbox_text}" if show_value === true end end end end end |
#input_file(label, name, args = {}) ⇒ Object Also known as: file
Generate a field for uploading files.
522 523 524 525 526 527 528 529 530 |
# File 'lib/ramaze/helper/blue_form.rb', line 522 def input_file(label, name, args = {}) id = args[:id] ? args[:id] : id_for(name) args = args.merge(:type => :file, :name => name, :id => id) @g.p do label_for(id, label, name) @g.input(args) end end |
#input_hidden(name, value = nil, args = {}) ⇒ Object Also known as:
Generate a hidden field. Hidden fields are essentially the same as text fields except that they aren’t displayed in the browser.
546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/ramaze/helper/blue_form.rb', line 546 def input_hidden(name, value = nil, args = {}) args = args.merge(:type => :hidden, :name => name) if !value and @form_values.respond_to?(name) args[:value] = @form_values.send(name) else args[:value] = value end @g.input(args) end |
#input_password(label, name, args = {}) ⇒ Object Also known as: password
Generate an input tag with a type of “password” along with a label. Password fields are pretty much the same as text fields except that the content of these fields is replaced with dots. This method has the following alias: “password”.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/ramaze/helper/blue_form.rb', line 271 def input_password(label, name, args = {}) # The ID can come from 2 places, id_for and the args hash id = args[:id] ? args[:id] : id_for(name) args = args.merge(:type => :password, :name => name, :id => id) if !args[:value] and @form_values.respond_to?(name) args[:value] = @form_values.send(name) end @g.p do label_for(id, label, name) @g.input(args) end end |
#input_radio(label, name, checked = nil, args = {}) ⇒ Object Also known as: radio
Generate an input tag with a type of “radio”.
If you want to generate multiple radio buttons you can use an array just like you can with checkboxes. Example:
@data = Class.new
attr_reader :gender_arr
attr_reader :gender_hash
def initialize
@gender_arr = ['male', 'female']
@gender_hash = {"Male" => "male", "Female" => "female"}
end
end.new
form_for(@data, :method => :post) do |f|
f.input_radio "Gender", :gender_arr
f.input_radio "Gender", :gender_hash
end
For more information see the input_checkbox() method.
498 499 500 501 502 503 504 505 506 507 |
# File 'lib/ramaze/helper/blue_form.rb', line 498 def input_radio(label, name, checked = nil, args = {}) # Force a type of "radio" args[:type] = :radio if !args[:span_class] args[:span_class] = "radio_wrap" end self.input_checkbox(label, name, checked, args) end |
#input_submit(value = nil, args = {}) ⇒ Object Also known as: submit
Generate a submit tag (without a label). A submit tag is a button that once it’s clicked will send the form data to the server.
299 300 301 302 303 304 305 306 |
# File 'lib/ramaze/helper/blue_form.rb', line 299 def input_submit(value = nil, args = {}) args = args.merge(:type => :submit) args[:value] = value unless value.nil? @g.p do @g.input(args) end end |
#input_text(label, name, args = {}) ⇒ Object Also known as: text
Generate an input tag with a type of “text” along with a label tag. This method also has the alias “text” so feel free to use that one instead of input_text.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/ramaze/helper/blue_form.rb', line 240 def input_text(label, name, args = {}) # The ID can come from 2 places, id_for and the args hash id = args[:id] ? args[:id] : id_for(name) args = args.merge(:type => :text, :name => name, :id => id) if !args[:value] and @form_values.respond_to?(name) args[:value] = @form_values.send(name) end @g.p do label_for(id, label, name) @g.input(args) end end |
#legend(text) ⇒ Object
Generate a ‘<legend>` tag.
207 208 209 |
# File 'lib/ramaze/helper/blue_form.rb', line 207 def legend(text) @g.legend(text) end |
#select(label, name, args = {}) ⇒ Object
Generate a select tag along with the option tags and a label.
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/ramaze/helper/blue_form.rb', line 601 def select(label, name, args = {}) id = args[:id] ? args[:id] : id_for(name) multiple, size = args.values_at(:multiple, :size) # Get all the values if !args[:values] and @form_values.respond_to?(name) values = @form_values.send(name) else values = args[:values] args.delete(:values) end args[:multiple] = 'multiple' if multiple args[:size] = (size || values.count || 1).to_i args[:name] = multiple ? "#{name}[]" : name args = args.merge(:id => id) # Retrieve the selected value has_selected, selected = args.key?(:selected), args[:selected] selected = [selected] if !selected.is_a?(Array) args.delete(:selected) @g.p do label_for(id, label, name) @g.select args do values.each do |value, o_name| o_name ||= value o_args = {:value => value} if has_selected and selected.include?(value) o_args[:selected] = 'selected' end @g.option(o_args){ o_name } end end end end |
#textarea(label, name, args = {}) ⇒ Object
Generate a text area.
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/ramaze/helper/blue_form.rb', line 571 def textarea(label, name, args = {}) id = args[:id] ? args[:id] : id_for(name) # Get the value of the textarea if !args[:value] and @form_values.respond_to?(name) value = @form_values.send(name) else value = args[:value] args.delete(:value) end args = args.merge(:name => name, :id => id) @g.p do label_for(id, label, name) @g.textarea(args){ value } end end |
#to_s ⇒ String
Method used for converting the results of the BlueForm helper to a string
646 647 648 |
# File 'lib/ramaze/helper/blue_form.rb', line 646 def to_s @g.to_s end |