lib/table_helper/cell.rb in table_helper-0.1.0 vs lib/table_helper/cell.rb in table_helper-0.2.0

- old
+ new

@@ -1,34 +1,49 @@ module TableHelper # Represents a single cell within a table. This can either be a regular # data cell (td) or a header cell (th). By default, all cells will have - # their column name appended to the cell's class attribute. + # their name appended to the cell's class attribute. # - # == Creating data cells + # == Examples # - # Cell.new(:author, 'John Doe').build - # - # ...would generate the following tag: - # - # <td class="author">John Doe</td> - # - # == Creating header cells - # + # # Data cell + # c = Cell.new(:author, 'John Doe') + # c.html # => <td class="author">John Doe</td> + # + # # Header cell # c = Cell.new(:author, 'Author Name') # c.content_type = :header - # c.build - # - # ...would generate the following tag: - # - # <th class="author">Author Name</th> + # c.html + # + # # With namespace + # c = Cell.new(:author, :namespace => 'post') + # c.content_type = :header + # c.html # => <td class="post-author">Author</td> class Cell < HtmlElement - def initialize(class_name, content = class_name.to_s.titleize, html_options = {}) #:nodoc + # The css class to apply to empty cells + cattr_accessor :empty_class + @@empty_class = 'ui-state-empty' + + # The content to display within the cell + attr_reader :content + + # The type of content this cell represents (:data or :header) + attr_reader :content_type + + def initialize(name, content = name.to_s.titleize, html_options = {}) #:nodoc + html_options, content = content, name.to_s.titleize if content.is_a?(Hash) + namespace = html_options.delete(:namespace) super(html_options) - @content = content - @html_options[:class] = ("#{class_name} " + @html_options[:class].to_s).strip if class_name + @content = content.to_s + if name + name = "#{namespace}-#{name}" unless namespace.blank? + self[:class] = "#{self[:class]} #{name}".strip + end + self[:class] = "#{self[:class]} #{empty_class}".strip if content.blank? + self.content_type = :data end # Indicates what type of content will be stored in this cell. This can # be set to either :data or :header. @@ -38,12 +53,8 @@ end private def tag_name @content_type == :data ? 'td' : 'th' - end - - def content - @content end end end