Module: Cheveret::Table::Sizing

Included in:
Base
Defined in:
lib/cheveret/table/sizing.rb

Defined Under Namespace

Modules: ClassMethods, Sizable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#widthObject



74
75
76
# File 'lib/cheveret/table/sizing.rb', line 74

def width
  @width
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/cheveret/table/sizing.rb', line 28

def self.included(base)
  base.module_eval do
    extend ClassMethods
  end

  # allow columns to be flexible and/or have their width set
  ::Cheveret::Column.send :include, Sizable
end

Instance Method Details

#needs_resize?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/cheveret/table/sizing.rb', line 129

def needs_resize?
  columns.map(&:size).sum != width
end

#render_td(column, options = {}) ⇒ Object



100
101
102
103
# File 'lib/cheveret/table/sizing.rb', line 100

def render_td(column, options={})
  options[:style] = "width:#{column.size}px;" if column.size > 0
  super
end

#render_th(column, options = {}) ⇒ Object



92
93
94
95
# File 'lib/cheveret/table/sizing.rb', line 92

def render_th(column, options={})
  options[:style] = "width:#{column.size}px;" if column.size > 0
  super
end

#resize!Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cheveret/table/sizing.rb', line 108

def resize!
  columns_width, flexibles = 0, []

  columns.values.each do |column|
    columns_width += column.width
    flexibles << column if column.flexible?
  end

  # todo: handle too-many/too-wide columns
  raise "uh-oh spaghettio-s" if columns_width > width

  # todo: fix rounding in with calculation
  if columns_width < width && !flexibles.empty?
    padding = (width - columns_width) / flexibles.length
    flexibles.each { |column| column.size = column.width + padding }
  end
end