Sha256: 37b62a130ccdb1cdc880251346ed57d08d676a03f3cc6c9e76dfd8d7a14de1ff

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacRailsUtils
  module DataTableHelper
    class DataTable
      common_constructor :view, :dataset, :setup_block, block_arg: true

      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| value_cell(c, record) << "\n" }
          )
        end << "\n"
      end

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

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

      # @return [EacRailsUtils::DataTableHelper::Setup]
      def setup
        @setup ||= begin
          r = ::EacRailsUtils::DataTableHelper::Setup.new
          setup_block.call(r)
          r
        end
      end

      require_sub __FILE__, require_mode: :kernel
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
eac_rails_utils-0.24.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.23.4 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.23.3 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.23.2 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.23.1 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.23.0 app/helpers/eac_rails_utils/data_table_helper/data_table.rb
eac_rails_utils-0.22.3 app/helpers/eac_rails_utils/data_table_helper/data_table.rb