Sha256: 9bc6d923b5ddc6fc14609efb7931a57db3b9e8736592a55bba18c256544877bc

Contents?: true

Size: 1.28 KB

Versions: 27

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module EacRailsUtils
  module DataTableHelper
    class DataTable
      def initialize(view, dataset)
        @view = view
        @dataset = dataset
        @setup = ::EacRailsUtils::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(record)
        @view.content_tag(:tr) do
          @view.safe_join(
            @setup.columns.map { |c| @view.content_tag('td', c.record_value(record)) << "\n" }
          )
        end << "\n"
      end

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

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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
eac_rails_utils-0.18.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.17.2 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.17.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.17.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.16.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.15.2 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.15.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.15.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.14.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.14.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.5 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.4 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.3 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.2 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.13.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.12.3 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.12.2 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.12.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.12.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb