Class: DataMapper::Form::Tag
- Inherits:
-
Object
- Object
- DataMapper::Form::Tag
- Defined in:
- lib/dm-forms/tag.rb
Constant Summary collapse
- BOOLEAN_ATTRIBUTES =
Boolean attributes.
:disabled, :readonly, :multiple, :checked, :selected
- IGNORE_CLASSES_ON_ELEMENTS =
Elements which should not include auto-generated classes.
:form, :label, :fieldset, :hidden
Instance Attribute Summary collapse
-
#after ⇒ Object
Markup placed after the element.
-
#attributes ⇒ Object
Attributes pulled from #options.
-
#before ⇒ Object
Markup placed before the element.
-
#description ⇒ Object
Tag’s description.
-
#name ⇒ Object
Name of element (input, fieldset, etc).
-
#options ⇒ Object
Options passed to the constructor.
Instance Method Summary collapse
-
#classes ⇒ Object
Returns user generated classes as well as those generated by dm-forms for styling consistancy.
-
#has_label? ⇒ Boolean
Wither or not this tag has a label.
-
#initialize(name, options = {}, &block) ⇒ Tag
constructor
A new instance of Tag.
-
#inner_html ⇒ Object
The inner HTML of this tag, only available for elements which are not self-closing.
-
#label ⇒ Object
Generates a label when needed.
-
#prepare_boolean_attributes ⇒ Object
Prepare boolean attribute values, so that the user may utilize :multiple => true, instead of :multiple => :multiple.
-
#render ⇒ Object
(also: #to_s)
Render final markup of this element or ‘tag’.
-
#required? ⇒ Boolean
Is the element required.
-
#self_closing? ⇒ Boolean
Wither or not a tag is self-closing (<br />).
Constructor Details
#initialize(name, options = {}, &block) ⇒ Tag
Returns a new instance of Tag.
46 47 48 49 50 51 52 |
# File 'lib/dm-forms/tag.rb', line 46 def initialize name, = {}, &block @name, @options, @attributes = name, , ([:attributes] ||= {}) @before, @after = attribute(:before, ''), attribute(:after, '') @description = Elements.desc(attribute(:description)) || '' @model = attribute :model (@attributes[:value] ||= '') << Elements.capture_elements(@model, &block) if block_given? end |
Instance Attribute Details
#after ⇒ Object
Markup placed after the element.
39 40 41 |
# File 'lib/dm-forms/tag.rb', line 39 def after @after end |
#attributes ⇒ Object
Attributes pulled from #options.
29 30 31 |
# File 'lib/dm-forms/tag.rb', line 29 def attributes @attributes end |
#before ⇒ Object
Markup placed before the element.
34 35 36 |
# File 'lib/dm-forms/tag.rb', line 34 def before @before end |
#description ⇒ Object
Tag’s description.
44 45 46 |
# File 'lib/dm-forms/tag.rb', line 44 def description @description end |
#name ⇒ Object
Name of element (input, fieldset, etc).
19 20 21 |
# File 'lib/dm-forms/tag.rb', line 19 def name @name end |
#options ⇒ Object
Options passed to the constructor.
24 25 26 |
# File 'lib/dm-forms/tag.rb', line 24 def @options end |
Instance Method Details
#classes ⇒ Object
Returns user generated classes as well as those generated by dm-forms for styling consistancy.
123 124 125 |
# File 'lib/dm-forms/tag.rb', line 123 def classes @classes ||= [@attributes[:class], generate_classes].join(' ').strip if should_add_classes? end |
#has_label? ⇒ Boolean
Wither or not this tag has a label.
115 116 117 |
# File 'lib/dm-forms/tag.rb', line 115 def has_label? !@attributes[:label].blank? end |
#inner_html ⇒ Object
The inner HTML of this tag, only available for elements which are not self-closing.
94 95 96 |
# File 'lib/dm-forms/tag.rb', line 94 def inner_html attribute :value, '' unless self_closing? end |
#label ⇒ Object
Generates a label when needed.
108 109 110 |
# File 'lib/dm-forms/tag.rb', line 108 def label @label ||= has_label? ? Elements.label(@attributes.delete(:label), :for => @attributes[:name], :required => required?) : '' end |
#prepare_boolean_attributes ⇒ Object
Prepare boolean attribute values, so that the user may utilize :multiple => true, instead of :multiple => :multiple.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dm-forms/tag.rb', line 71 def prepare_boolean_attributes @attributes.each_pair do |key, value| if BOOLEAN_ATTRIBUTES.include? key if value @attributes[key] = key else @attributes.delete key end end end end |
#render ⇒ Object Also known as: to_s
Render final markup of this element or ‘tag’.
57 58 59 60 61 62 63 64 |
# File 'lib/dm-forms/tag.rb', line 57 def render @attributes[:class] = classes unless classes.blank? prepare_boolean_attributes before << label close = self_closing? ? " />" : ">#{inner_html}</#{@name}>" open = "<#{@name} #{@attributes.to_html_attributes}" tag = before << open << close << description << after << "\n" end |
#required? ⇒ Boolean
Is the element required.
101 102 103 |
# File 'lib/dm-forms/tag.rb', line 101 def required? attribute :required, false end |
#self_closing? ⇒ Boolean
Wither or not a tag is self-closing (<br />).
86 87 88 |
# File 'lib/dm-forms/tag.rb', line 86 def self_closing? @options[:self_closing] end |