Sha256: a084e88b53a0597738630238e23fda7b32fe2163dbe97f3e68ba8295fb1f1396

Contents?: true

Size: 1.16 KB

Versions: 12

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Eac
  module DataTableHelper
    class DataTable
      def initialize(view, dataset)
        @view = view
        @dataset = dataset
        @setup = ::Eac::DataTableHelper::Setup.new
        yield(@setup)
      end

      def output
        @view.content_tag(:table, id: id) do
          head << body
        end << script
      end

      private

      def head
        @view.content_tag(:thead) do
          @view.content_tag(:tr) do
            @view.safe_join(@setup.columns.map { |c| @view.content_tag('th', c.label) })
          end
        end
      end

      def body
        @view.content_tag(:tbody) do
          @view.safe_join(@dataset.map { |r| row(r) })
        end
      end

      def row(r)
        @view.content_tag(:tr) do
          @view.safe_join(@setup.columns.map { |c| @view.content_tag('td', c.record_value(r)) << "\n" })
        end << "\n"
      end

      def script
        @view.javascript_tag <<EOS
$(document).ready(function () {
  $('##{id}').DataTable({
    paging: #{@setup.paging ? 'true' : 'false'}
        });
  });
EOS
      end

      def id
        @id ||= SecureRandom.hex(32)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
eac_rails_utils-0.8.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.7.1 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.7.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.6.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.5.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.4.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.3.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.2.2 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.2.1 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.2.0 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.1.15 lib/eac/data_table_helper/data_table.rb
eac_rails_utils-0.1.14 lib/eac/data_table_helper/data_table.rb