Sha256: 479a1defcccabb66abaf474653ac44789178df4f09c9bceceac8bcf7364e8545

Contents?: true

Size: 1003 Bytes

Versions: 27

Compression:

Stored size: 1003 Bytes

Contents

# frozen_string_literal: true

module EacRailsUtils
  module DataTableHelper
    class Column
      EMPTY_VALUE = '-'

      attr_reader :label

      def initialize(label, path, block)
        @label = label
        @path = path.to_s.split('.')
        @block = block
      end

      def record_value(record)
        v = Node.new(record, @path).value
        if v.present?
          @block ? @block.call(v) : v
        else
          EMPTY_VALUE
        end
      end

      private

      def node_value(node, subpath)
        return node if subpath.empty?
      end

      class Node
        def initialize(node, path)
          @node = node
          @path = path
        end

        def value
          return @node if @node.nil? || @path.empty?

          subpath = @path.dup
          n = subpath.shift
          return Node.new(@node.send(n), subpath).value if @node.respond_to?(n)

          raise "Instance of #{@node.class} does not respond to #{n}"
        end
      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/column.rb
eac_rails_utils-0.17.2 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.17.1 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.17.0 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.16.0 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.15.2 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.15.1 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.15.0 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.14.1 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.14.0 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.5 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.4 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.3 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.2 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.1 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.13.0 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.12.3 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.12.2 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.12.1 app/helpers/eac_rails_utils/data_table_helper/column.rb
eac_rails_utils-0.12.0 app/helpers/eac_rails_utils/data_table_helper/column.rb