Module: TableSortable::Concerns::Proc

Extended by:
ActiveSupport::Concern
Included in:
TableSortable::Column::Filter, TableSortable::Column::Sorter
Defined in:
lib/table_sortable/concerns/proc.rb

Instance Method Summary collapse

Instance Method Details

#array_procObject

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/table_sortable/concerns/proc.rb', line 38

def array_proc
  raise NotImplementedError
end

#detect_method(proc) ⇒ Object



29
30
31
32
# File 'lib/table_sortable/concerns/proc.rb', line 29

def detect_method(proc)
  method = [].instance_exec('', &proc) rescue :failed
  method == :failed ? :sql : :array
end

#disabled?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/table_sortable/concerns/proc.rb', line 34

def disabled?
  method.nil?
end

#initialize(option_name, *options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/table_sortable/concerns/proc.rb', line 10

def initialize(option_name, *options)
  options = options.extract_options!
  unless options[option_name] == false
    filter = options[option_name] || options[:column_name]
    @method = options["#{option_name.to_s}_method".to_sym] || :array
    if filter.respond_to? :call
      @proc = -> (records, col=nil) { instance_exec(records, &filter) }
      @method = detect_method(@proc)
    elsif !filter.nil?
      case @method
        when :array
          @proc = array_proc
        when :sql
          @proc = sql_proc
      end
    end
  end
end

#sql_procObject

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/table_sortable/concerns/proc.rb', line 42

def sql_proc
  raise NotImplementedError
end