Class: DataMapper::Form::Tag

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {}, &block
  @name, @options, @attributes = name, options, (options[: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

#afterObject

Markup placed after the element.



39
40
41
# File 'lib/dm-forms/tag.rb', line 39

def after
  @after
end

#attributesObject

Attributes pulled from #options.



29
30
31
# File 'lib/dm-forms/tag.rb', line 29

def attributes
  @attributes
end

#beforeObject

Markup placed before the element.



34
35
36
# File 'lib/dm-forms/tag.rb', line 34

def before
  @before
end

#descriptionObject

Tag’s description.



44
45
46
# File 'lib/dm-forms/tag.rb', line 44

def description
  @description
end

#nameObject

Name of element (input, fieldset, etc).



19
20
21
# File 'lib/dm-forms/tag.rb', line 19

def name
  @name
end

#optionsObject

Options passed to the constructor.



24
25
26
# File 'lib/dm-forms/tag.rb', line 24

def options
  @options
end

Instance Method Details

#classesObject

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.

Returns:

  • (Boolean)


115
116
117
# File 'lib/dm-forms/tag.rb', line 115

def has_label?
  !@attributes[:label].blank?
end

#inner_htmlObject

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

#labelObject

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_attributesObject

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

#renderObject 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.

Returns:

  • (Boolean)


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 />).

Returns:

  • (Boolean)


86
87
88
# File 'lib/dm-forms/tag.rb', line 86

def self_closing?
  @options[:self_closing]
end