Sha256: 93fd20d91eccc3fbd194c8e0ccb22f1660896678ac74039fc2cbeabf5bba175c

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module MightyGrid
  class Column

    attr_reader :attribute, :attrs, :th_attrs, :options, :title, :model
    attr_accessor :render_value

    def initialize(attr_or_options=nil, options=nil, &block)
      @attrs = {}
      @th_attrs = {}
      if block_given?
        @options = attr_or_options || {}
        @render_value = block
      else
        @options = options || {}
        @attribute = attr_or_options
        @render_value = @attribute
      end

      @model = @options[:model]
      raise MightyGridArgumentError.new("Model of field for filtering should have type ActiveRecord") if @model && @model.superclass != ActiveRecord::Base
      
      @attrs = @options[:html] if @options.has_key?(:html)
      @th_attrs = @options[:th_html] if @options.has_key?(:th_html)
      @title = @options.has_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]
        when Proc
          value = @render_value.call(record)
          return ERB::Util.h(value).to_s.html_safe
        else
          # raise
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mighty_grid-0.4.0 lib/mighty_grid/column.rb