Sha256: 26f328af5a8947e6f1eabb266c0f40738e8781460dc4e0522d9a93023efaaa05

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

module MightyGrid
  class Column
    attr_reader :attribute, :attrs, :th_attrs, :options, :title, :model, :partial
    attr_accessor :render_value

    def initialize(options = {}, &block)
      @attrs = {}
      @th_attrs = {}

      @attribute = options.delete(:attribute) if options.key?(:attribute)

      @options ||= options

      if block_given?
        @render_value = block
      else
        @render_value = @attribute
      end

      @model = @options[:model]
      fail MightyGrid::Exceptions::ArgumentError.new('Model of field for filtering should have type ActiveRecord') if @model && @model.superclass != ActiveRecord::Base

      @attrs = @options[:html] if @options.key?(:html)
      @th_attrs = @options[:th_html] if @options.key?(:th_html)
      @title = @options.key?(:title) && @options[:title] || ''
    end

    def render(record)
      case @render_value
      when String, Symbol
        rec = @model ? record.send(@model.to_s.underscore) : record
        return rec[@render_value.to_sym]
      when Proc
        value, attrs = @render_value.call(record)
        @attrs.merge!(attrs || {})
        return ERB::Util.h(value).to_s.html_safe
      else
        # raise
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mighty_grid-2.1.1 lib/mighty_grid/column.rb
mighty_grid-2.1.0 lib/mighty_grid/column.rb
mighty_grid-2.0.0 lib/mighty_grid/column.rb
mighty_grid-2.0.0.rc1 lib/mighty_grid/column.rb