Sha256: ce4b3449c36e14c852fdbef440fba0396be5ce45c069fb7564818f39cfb9362e

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

module Facades
  module Builders
    class TableBuilder
      attr_reader :template, :options, :resources, :buffer
      
      def initialize(resources, tpl, options)
        @buffer = ""
        @template, @options, @resources = tpl, options, resources
      end
      
      delegate :content_tag, :to => :template
      
      ##
      # Get a list of all headings for the table
      # Use the resource class's table_headings method first, options hash next
      # 
      def headings
        return resource_class.table_attributes if resource_class.respond_to?(:table_headings)
        [options[:headings]].flatten.compact
      end
      
      ##
      # The class for this resource
      def resource_class
        resources.first.class
      end
      
      ##
      # Renders the table content immediately using the provided options
      def inline!
        [head, body].map(&:to_s).join("\n")
      end
      
      def head
        return content_tag(:thead, render_heading) unless block_given?        
        content_tag(:thead) do
          yield self
        end
      end
      
      def body
        return content_tag(:tbody, render_body) unless block_given?        
        content_tag(:tbody) do
          yield self
        end
      end
      
      private
      
      # Render an entire body inline
      def render_body
        rows = []
        resources.each_with_index do |res, index|
          attrs = { :class => (ind.even? ? 'even' : 'odd') }
          buff = content_tag(:tr, attrs) do
            headings.collect do |hd|
              content_tag(:td, res.send(:"#{hd}"))
            end.to_s.join("\n")
          end
          rows << buff
        end
        rows.map(&:to_s).join("\n")
      end
      
      # Render entire body inline
      def render_heading
        buffer << template.content_tag(:thead) do
          headings.collect do |hd|
            content_tag(:th, hd)
          end.to_s.join("\n")
        end
      end
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facades-0.0.6 lib/facades/builders/table.rb
facades-0.0.5 lib/facades/builders/table.rb
facades-0.0.4 lib/facades/builders/table.rb
facades-0.0.3 lib/facades/builders/table.rb