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

#active_record_procObject

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/table_sortable/concerns/proc.rb', line 65

def active_record_proc
  raise NotImplementedError
end

#array_procObject

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/table_sortable/concerns/proc.rb', line 61

def array_proc
  raise NotImplementedError
end

#detect_method(proc, scope = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/table_sortable/concerns/proc.rb', line 31

def detect_method(proc, scope = nil)
  begin
    [].instance_exec('', &proc)
    method = :array
  rescue NoMethodError
    method = :active_record
  end
  method
end

#disabled?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/table_sortable/concerns/proc.rb', line 57

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
28
29
# File 'lib/table_sortable/concerns/proc.rb', line 10

def initialize(option_name, *options)
  options = options.extract_options!
  unless options[option_name] == false
    @type = option_name
    @column = options[:column]
    the_proc = options[option_name] || @column.name
    @method = options["#{option_name.to_s}_method".to_sym] || :autodetect
    if the_proc.respond_to? :call
      @proc = proc_wrapper(the_proc)
      @method = detect_method(@proc)
    elsif !the_proc.nil?
      case @method
        when :array
          @proc = array_proc
        when :active_record
          @proc = active_record_proc
      end
    end
  end
end

#method(record = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/table_sortable/concerns/proc.rb', line 41

def method(record = nil)
  return @method if record.nil?
  if @method == :autodetect
    if record.class.columns.map{|col| col.name.to_sym}.include? @column.name
      method = :active_record
      @proc = active_record_proc
    else
      method = :array
      @proc = array_proc
    end
  else
    method = @method
  end
  method
end

#proc_wrapper(proc) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/table_sortable/concerns/proc.rb', line 69

def proc_wrapper(proc)
  raise NotImplementedError
end

#run(records) ⇒ Object

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/table_sortable/concerns/proc.rb', line 73

def run(records)
  raise NotImplementedError
end

#used?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/table_sortable/concerns/proc.rb', line 77

def used?
  raise NotImplementedError
end