Module: ActionTabler::Utilities

Included in:
Handler
Defined in:
lib/action_tabler/utilities.rb

Instance Method Summary collapse

Instance Method Details

#column_for_sql(name, model = nil) ⇒ Object

Returns a fully qualified column name for SQL statements



15
16
17
# File 'lib/action_tabler/utilities.rb', line 15

def column_for_sql(name, model = nil)
  visit_attribute(name){|name, model| [model.arel_table.name, name].join(".")}
end

#exitisting_conditions(params) ⇒ Object

Existing search conditions if enabled



5
6
7
# File 'lib/action_tabler/utilities.rb', line 5

def exitisting_conditions(params)
  params[@model.name.parameterize] if @pass_params
end

#searchable_columnsObject

List searchable columns



10
11
12
# File 'lib/action_tabler/utilities.rb', line 10

def searchable_columns
  @columns.select{|column| column[:searchable] != false}
end

#visit_attribute(name, model = nil, &block) ⇒ Object

Helps work with fully qualified attributes, usually to generate SQL



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/action_tabler/utilities.rb', line 20

def visit_attribute(name, model = nil, &block)
  # Set the default model if not passed
  model ||= @model
  
  # We can really only do this if we have ActiveRecord or equivalent
  return nil unless model.respond_to?(:columns_hash)

  # Ensure we have a string
  name = name.to_s

  # Determine if we have a relation
  if name.include?(".")
    names = name.split(".")
    # Discover relation model
    reflection = model.reflect_on_association(names[0].to_sym)
    # Recurse with realtion model and name
    column_for_sql(name.gsub("#{names[0]}.", ""), reflection.klass)
  else
    block.call(name, model)
  end
end