Class: Toolsmith::Views::DefinitionListFor

Inherits:
Base
  • Object
show all
Defined in:
lib/toolsmith/views/definition_list_for.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ horizontal: true }

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#content_tag

Constructor Details

#initialize(context, &block) ⇒ DefinitionListFor

Returns a new instance of DefinitionListFor.



9
10
11
12
13
# File 'lib/toolsmith/views/definition_list_for.rb', line 9

def initialize(context, &block)
  @content_block = block if block_given?
  @options = DEFAULT_OPTIONS.dup
  super(context, &nil)
end

Instance Attribute Details

#content_blockObject (readonly)

Returns the value of attribute content_block.



5
6
7
# File 'lib/toolsmith/views/definition_list_for.rb', line 5

def content_block
  @content_block
end

#objectObject

Returns the value of attribute object.



4
5
6
# File 'lib/toolsmith/views/definition_list_for.rb', line 4

def object
  @object
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/toolsmith/views/definition_list_for.rb', line 4

def options
  @options
end

Class Method Details

.new_with_object(context, object, &block) ⇒ Object



15
16
17
18
19
# File 'lib/toolsmith/views/definition_list_for.rb', line 15

def self.new_with_object(context, object, &block)
  definition_list = new(context, &block)
  definition_list.object = object
  definition_list
end

Instance Method Details

#define(attribute) ⇒ Object



21
22
23
24
25
# File 'lib/toolsmith/views/definition_list_for.rb', line 21

def define(attribute)
  human_attribute = attribute.to_s.humanize

  (:dt, human_attribute) + (:dd, object.send(attribute))
end

#dl_optionsObject



32
33
34
35
36
# File 'lib/toolsmith/views/definition_list_for.rb', line 32

def dl_options
  {
    class: options[:horizontal] ? "dl-horizontal" : nil
  }
end

#to_sObject



27
28
29
30
# File 'lib/toolsmith/views/definition_list_for.rb', line 27

def to_s
  content = context.capture(self, &content_block)
   :dl, content, dl_options
end