Sha256: 0b9cc2b8530ab0774176594a32340258a1b93d0891d242c4a264d07f42620d9b

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

module TableGo
  class Table

    attr_accessor :collection, :model_klass
    attr_accessor :columns

    def initialize(collection, model_klass, options, &block)
      @collection  = collection
      @model_klass = model_klass
      @columns     = []
      apply_options!(options)
      evaluate_dsl!(block)
    end

    def evaluate_dsl!(block)
      if block
        # instance_eval(&block)
        block.call(self)
      else
        attribute_names_from_model_klass.each do |column_name|
          column(column_name)
        end
      end
    end

    # def model_klass_reflection_keys
    #   @model_klass_reflection_keys ||= model_klass.reflections.keys
    # end


    def attribute_names_from_model_klass
      model_klass.respond_to?(:column_names) ? model_klass.column_names : []
    end

    def apply_options!(options)
      options.each { |k, v| send(k, v) }
    end



    def column(name, options = {}, &block)
      @columns << Column.new(self, name, options, &block)
    end

    def title(title = nil)
      @title = title if title
      @title
    end

    def table_html(table_html = nil)
      @table_html = table_html if table_html
      @table_html
    end

    def row_html(row_html = nil)
      @row_html = row_html if row_html
      @row_html
    end

    def render_rows_only(render_rows_only = nil)
      @render_rows_only = render_rows_only if render_rows_only
      @render_rows_only
    end

    def without_header(without_header = nil)
      @without_header = without_header if without_header
      @without_header
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_go-0.2.6 lib/table_go/table.rb
table_go-0.2.5 lib/table_go/table.rb
table_go-0.2.4 lib/table_go/table.rb