Sha256: e860a26f5823a220c1137008f31027c75912bbe908982298c9c557483a1c6bee

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module TableCloth
  class Base
    NoPresenterError = Class.new(Exception)

    attr_reader :collection, :view
    class_attribute :config, instance_accessor: false
    self.config = Class.new { include ConfigurableElements }

    def initialize(collection, view)
      @collection = collection
      @view       = view
    end

    def column_names
      @column_names ||= columns.each_with_object([]) do |(column_name, column), names|
        names << column.human_name
      end
    end

    def columns
      @columns ||= self.class.columns.each_with_object({}) do |(column_name, column), columns|
        columns[column_name] = column if column.available?(self)
      end
    end

    def has_actions?
      self.class.has_actions?
    end

    class << self
      def presenter(klass=nil)
        return @presenter = klass if klass
        return @presenter if @presenter

        if superclass.respond_to?(:presenter)
          superclass.presenter
        else
          raise NoPresenterError, "No Presenter"
        end
      end

      def column(*args, &block)
        options = args.extract_options! || {}
        options[:proc] = block if block_given?

        column_class = options.delete(:using) || Column

        args.each do |name|
          add_column name, column_class.new(name, options)
        end
      end

      def columns
        @columns ||= {}
        if superclass.respond_to? :columns
          @columns = superclass.columns.merge(@columns)
        end

        @columns
      end

      def add_column(name, column)
        @columns ||= {}
        @columns[name] = column
      end

      def actions(options={}, &block)
        if block_given?
          actions = Actions.new(options, &block)
          columns[:actions] = actions.column
        end

        columns[:actions].actions
      end

      def has_actions?
        columns[:actions].present?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_cloth-0.2.2 lib/table_cloth/base.rb