Class: ActionTabler::Handler

Inherits:
Object
  • Object
show all
Includes:
DataTables, Utilities
Defined in:
lib/action_tabler/table_handler.rb

Instance Method Summary collapse

Methods included from DataTables::DisplayHandler

#columns, #table_class, #table_definition, #table_id

Methods included from DataTables::DataHandler

#data_package

Methods included from Utilities

#column_for_sql, #exitisting_conditions, #searchable_columns, #visit_attribute

Constructor Details

#initialize(controller, columns = [], options = {}, &block) ⇒ Handler

Returns a new instance of Handler.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/action_tabler/table_handler.rb', line 8

def initialize(controller, columns = [], options = {}, &block)
  @columns = []
  @table_options = {}

  # Load essential column-related options
  @model = (options[:class_name] || controller.controller_name).classify.constantize
  @auto_type = options[:auto_type] || configuration[:auto_type]

  # Check to see what we've been passed
  if columns.kind_of?(Hash)
    # Set options to columns if columns not passed in parameters
    options = columns
  elsif columns == :auto
    # Auto populate columns if specified
    auto_columns
  elsif columns.kind_of?(Array)
    columns.collect{|c| column(c)}
  end

  # Populate columns from block
  instance_eval(&block) if block.kind_of?(Proc)

  # Add configuration columns if specified
  if configuration[:columns].kind_of?(Array)
    configuration[:columns].collect{|c| column(c)}
  end

  # Auto specify columns if they're empty and the config is set to auto
  if @columns.empty? && configuration[:columns] == :auto
    auto_columns
  end

  if @columns.empty?
    raise ArgumentError, "No columns defined for has_table_action"
  end

  # Load options
  @pass_params = options[:pass_params] || configuration[:pass_params]
  @table_class = options[:table_class] || configuration[:table_class]
  
  @table_options.update(configuration[:table_options])
  @table_options.update(options[:table_options] || {})
end

Instance Method Details

#auto_columnsObject

Create column definitions using reflection



78
79
80
# File 'lib/action_tabler/table_handler.rb', line 78

def auto_columns
  @model.columns_hash.keys.collect{|k| column(k)}
end

#column(name, opts = {}) ⇒ Object

Add a table column definition



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/action_tabler/table_handler.rb', line 53

def column(name, opts={})
  # Check what we're getting
  if name.kind_of?(Hash)
    # If it's a hash, leave as is
    column = name
    raise ArgumentError, "column definition requires a name" if column[:name].to_s.empty?
  elsif name.kind_of?(Symbol) || name.kind_of?(String)
    # If it's a string or symbol, create a definition
    column = {:name => name}
    column.update(opts) if opts.kind_of?(Hash)
  else
    raise ArgumentError, "table_column name expects String, Symbol, or Hash (got #{name.class.name})"
  end

  # Ensure name is a string
  column[:name] = column[:name].to_s

  # Add label if not specified
  column[:label] ||= column[:name].to_s.gsub(".", " ").titleize

  # Add the column to out list
  @columns << column
end