Module: Puffer::OrmAdapter::ActiveRecord

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

Instance Method Summary collapse

Instance Method Details

#columns_hashObject



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

def columns_hash
  klass.columns_hash.inject({}) do |result, (name, object)|
    result.merge name => {:type => object.type}
  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
# File 'lib/puffer/orm_adapter/active_record.rb', line 22

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

  order = order.map { |name, dir| field = fields[name]; "#{query_order(field)} #{dir}" if field && field.column }.compact.join(', ')

  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.includes(includes(all_fields)).includes(reflection_includes(fields)).where(searches(search_fields, options[:search])).order(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(["#{query_column(field)} < ?", value.till])
      when value.till.blank? then scope.where(["#{query_column(field)} > ?", 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/active_record.rb', line 11

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