Class: GridFu::Element

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/grid_fu/element.rb,
lib/grid_fu/element/nesting.rb,
lib/grid_fu/element/rendering.rb,
lib/grid_fu/element/configuration.rb

Direct Known Subclasses

Cell, Row, Section, Table

Instance Method Summary collapse

Constructor Details

#initialize(*args, &definition) ⇒ Element

Returns a new instance of Element.



5
6
7
8
9
# File 'lib/grid_fu/element.rb', line 5

def initialize(*args, &definition)
  instance_exec(&definition) if block_given?
  options = args.extract_options!
  config.merge!(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)

Catches a call to configuration option setter.

Example: body do

html_options { class: 'test' } # Holds such calls

end



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/grid_fu/element/configuration.rb', line 10

def method_missing(method_name, *args, &block)
  return super unless method_name.to_s.in?(config.allowed_configuration_options)
  config[method_name] = args.first || block

  class_eval do
    define_method method_name do |value|
      config[method_name] = value
    end
    protected method_name
  end

  config[method_name]
end

Instance Method Details

#element_to_html(element, *args) ⇒ Object



25
26
27
# File 'lib/grid_fu/element/rendering.rb', line 25

def element_to_html(element, *args)
  send(element).map { |item| item.to_html(*args) }.join
end

#to_html(*args) ⇒ Object

Translates element to html tag.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/grid_fu/element/rendering.rb', line 4

def to_html(*args)
  tag, override_html_options, html_options =
    get_options([:tag, :override_html_options, :html_options], *args)

  raise "Set tag option for #{self.class.name}" if tag.blank?

  html_options = override_html_options.merge(html_options)
  html_options = _to_html_args(html_options)

  html = []

  html << "<#{tag}"
  html << " #{html_options}" if html_options.present?
  html << '>'

  html << html_content(*args).to_s

  html << "</#{tag}>"
  html.join
end