Class: ContentTagHelper::Tag

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::CaptureHelper
Defined in:
lib/ecrire/app/helpers/content_tag_helper.rb

Defined Under Namespace

Classes: CSS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Tag

Returns a new instance of Tag.



23
24
25
26
27
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 23

def initialize(name, *args)
  @name = name
  @attributes = tag_options(*args)
  @attributes[:class] = Tag::CSS.new(@attributes.fetch(:class, {}))
end

Instance Attribute Details

#cssObject (readonly)

Returns the value of attribute css.



21
22
23
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 21

def css
  @css
end

#idObject

Returns the value of attribute id.



20
21
22
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 20

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 21

def name
  @name
end

Instance Method Details

#[](k) ⇒ Object



44
45
46
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 44

def [](k)
  @attributes[k]
end

#[]=(k, v) ⇒ Object



48
49
50
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 48

def []=(k,v)
  @attributes[k] = v
end

#render(content) ⇒ Object



52
53
54
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 52

def render(content)
  "<#{name}#{render_attributes}>#{content.to_s.strip}</#{name}>".html_safe
end

#render_attributesObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 56

def render_attributes
  attrs = @attributes.dup
  if css.empty?
    attrs.delete :class
  else
    attrs[:class] = css.to_s
  end

  attrs.keys.map do |k|
    "#{k}='#{attrs[k]}'"
  end.join(' ').prepend(' ').html_safe
end

#tag_options(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/ecrire/app/helpers/content_tag_helper.rb', line 29

def tag_options(*args)
  options = nil
  args.each do |a|
    if a.is_a?(Hash)
      options = a
      break
    end
  end
  (options || {}).with_indifferent_access
end