Class: Tabulatr::Adapter::ActiveRecordAdapter

Inherits:
Tabulatr::Adapter show all
Defined in:
lib/tabulatr/tabulatr/adapter/active_record.rb

Instance Method Summary collapse

Methods inherited from Tabulatr::Adapter

#class_to_param, #order, #preconditions_scope, #sort_params, #to_sql

Constructor Details

#initialize(klaz) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



3
4
5
6
7
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 3

def initialize(klaz)
  set_like_statement unless Tabulatr::SQL_OPTIONS[:like]

  super klaz
end

Instance Method Details

#add_conditions_from(n, v) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 42

def add_conditions_from(n,v)
  like ||= Tabulatr.sql_options[:like]
  if v.is_a?(String)
    @relation = @relation.where(n => v) unless v.blank?
  elsif v.is_a?(Hash)
    if v[:like].present?
      @relation = @relation.where("#{n} #{like} ?", "%#{v[:like]}%")
    else
      @relation = @relation.where("#{n} >= ?", "#{v[:from]}") if v[:from].present?
      @relation = @relation.where("#{n} <= ?", "#{v[:to]}") if v[:to].present?
    end
  else
    raise "Wrong filter type: #{v.class}"
  end
end

#includes(inc) ⇒ Object



34
35
36
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 34

def includes(inc)
  @relation.includes(inc)
end

#includes!(inc) ⇒ Object



38
39
40
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 38

def includes!(inc)
  @relation = includes(includes)
end

#key_typeObject



13
14
15
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 13

def key_type
  @relation.columns_hash[primary_key.to_s].type
end

#order_for_query(sortparam, default) ⇒ Object



29
30
31
32
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 29

def order_for_query(sortparam, default)
  context = order(sortparam, default)
  "#{context[:by]} #{context[:direction]}" if context
end

#primary_keyObject



9
10
11
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 9

def primary_key
  @relation.primary_key.to_sym
end

#selected_ids(opts) ⇒ Object



17
18
19
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 17

def selected_ids(opts)
  preconditions_scope(opts).select(:id)
end

#table_nameObject



21
22
23
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 21

def table_name
  @relation.table_name
end

#table_name_for_association(assoc) ⇒ Object



25
26
27
# File 'lib/tabulatr/tabulatr/adapter/active_record.rb', line 25

def table_name_for_association(assoc)
  @base.reflect_on_association(assoc).table_name
end