Module: ActionTabler::HasTableAction::ClassMethods

Defined in:
lib/action_tabler/has_table_action.rb

Instance Method Summary collapse

Instance Method Details

#auto_table_columnsObject

Create column definitions using reflection



94
95
96
# File 'lib/action_tabler/has_table_action.rb', line 94

def auto_table_columns
  action_tabler_model.columns_hash.keys.collect{|k| column(k)}
end

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

Add a table column definition



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/action_tabler/has_table_action.rb', line 69

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.titleize

  # Add the column to out list
  self.action_tabler_columns << column
end

#has_table_action(columns = [], options = {}) ⇒ Object

Adds table display to a controller.



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/action_tabler/has_table_action.rb', line 13

def has_table_action(columns = [], options = {})
  self.action_tabler_columns = []
  self.action_tabler_table_options = {}

  # Load essential column-related options
  self.action_tabler_model = (options[:class_name] || controller_name).classify.constantize
  self.action_tabler_auto_type = options[:auto_type] || AbstractController::Base.action_tabler_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_table_columns
  elsif columns.kind_of?(Array)
    columns.collect{|c| column(c)}
  end

  # Populate columns from block
  yield if block_given?

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

  # Auto specify columns if they're empty and the config is set to auto
  if action_tabler_columns.empty? && AbstractController::Base.action_tabler_columns == :auto
    auto_table_columns
  end

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

  # Load options
  self.action_tabler_action = options[:action] || AbstractController::Base.action_tabler_action
  self.action_tabler_pass_params = options[:pass_params] || AbstractController::Base.action_tabler_pass_params
  self.action_tabler_table_class = options[:table_class] || AbstractController::Base.action_tabler_table_class
  
  self.action_tabler_table_options.update(AbstractController::Base.action_tabler_table_options)
  self.action_tabler_table_options.update(options[:table_options] || {})

  include DeclaredInstanceMethods

  # Define the table action method
  define_method(action_tabler_action) do
    action_tabler
  end

  # Make the column name formaters available in views 
  helper_method :column_name_for_json, :column_name_from_json, :render_datatables_table_definitions
end