Module: Puffer::OrmAdapter::Mongoid

Defined in:
lib/puffer/orm_adapter/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#columns_hashObject



5
6
7
8
9
# File 'lib/puffer/orm_adapter/mongoid.rb', line 5

def columns_hash
  klass.fields.inject({}) do |result, (name, object)|
    result.merge name => {:type => object.type.to_s.underscore.to_sym}
  end
end

#filter(scope, fields, options = {}) ⇒ Object



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
# File 'lib/puffer/orm_adapter/mongoid.rb', line 22

def filter scope, fields, options = {}
  conditions, order = extract_conditions_and_order!(options)

  order = order.map { |o| f = fields[o.first]; [query_order(f), o.last] if f && f.column }.compact

  conditions_fields = fields.select {|f| f.column && conditions.keys.include?(f.field_name)}.to_fieldset
  search_fields = fields.select {|f| f.column && !conditions_fields.include?(f) && search_types.include?(f.column_type)}
  all_fields = conditions_fields + search_fields

  scope = scope.any_of(searches(search_fields, options[:search])) if options[:search].present?
  scope = scope.order_by(order)

  conditions.each do |name, value|
    field = conditions_fields[name]
    scope = if value.is_a?(Puffer::Filters::Diapason)
      case
      when value.from.blank? then scope.where(name.to_sym.lt => value.till)
      when value.till.blank? then scope.where(name.to_sym.gt => value.from)
      else scope.where(name => Range.new(value.from, value.till))
      end
    else
      scope.where(name => value)
    end if field
  end

  scope
end

#reflection(name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/puffer/orm_adapter/mongoid.rb', line 11

def reflection name
  reflection = klass.reflect_on_association(name.to_sym)
  Reflection.new(
    :klass => reflection.klass,
    :macro => reflection.macro,
    :through? => false,
    :accessor => accessor_for(reflection),
    :primary_key => :_id
  ) if reflection
end