Class: Tabulatr::Adapter::MongoidAdapter

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

Instance Method Summary collapse

Methods inherited from Tabulatr::Adapter

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

Constructor Details

This class inherits a constructor from Tabulatr::Adapter

Instance Method Details

#add_conditions_from(n, v) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 35

def add_conditions_from(n,v)
  if v.is_a?(String)
    nn = n.split('.').last
    @relation = @relation.where(nn => v) unless v.blank?
  elsif v.is_a?(Hash)
    if v[:like].present?
      nn = n.split('.').last
      @relation = @relation.where(nn => Regexp.new(v[:like]))
    else
      nn = n.split('.').last.to_sym
      @relation = @relation.where(nn.gte => v[:from]) if v[:from].present?
      @relation = @relation.where(nn.lte => v[:to]) if v[:to].present?
    end
  else
    raise "Wrong filter type: #{v.class}"
  end

end

#includes(includes) ⇒ Object



31
32
33
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 31

def includes(includes)
  @relation   # do nothing with includes
end

#key_typeObject



6
7
8
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 6

def key_type
  :string
end

#order_for_query(sortparam, default) ⇒ Object



26
27
28
29
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 26

def order_for_query(sortparam, default)
  context = order(sortparam, default)
  [context[:by], context[:direction]].map(&:to_s) if context
end

#primary_keyObject



2
3
4
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 2

def primary_key
  :id
end

#selected_ids(opts) ⇒ Object



10
11
12
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 10

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

#table_nameObject



14
15
16
17
18
19
20
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 14

def table_name
  if Object.const_defined?("Mongoid") && @relation.is_a?(Mongoid::Criteria)
    @relation.klass
  else
    @relation
  end.to_s.tableize.gsub('/','_')
end

#table_name_for_association(assoc) ⇒ Object



22
23
24
# File 'lib/tabulatr/tabulatr/adapter/mongoid.rb', line 22

def table_name_for_association(assoc)
  assoc.to_s.tableize
end