Class: Netzke::Basepack::DataAdapters::ActiveRecordAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/marty/monkey.rb

Instance Method Summary collapse

Instance Method Details

#count_records(params, columns = []) ⇒ Object

FIXME: another giant hack to handle lazy_load columns. Modified original count_records to call count on first passed column.name when lazy-loaded. Otherwise, we run into issues with counting records in the default_scope placed by the lazy_load module.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/marty/monkey.rb', line 114

def count_records(params, columns=[])

  relation = @relation || get_relation(params)
  columns.each do |c|
    assoc, method = c[:name].split('__')
    relation = relation.includes(assoc.to_sym).references(assoc.to_sym) if method
  end

  @model.const_defined?(:LAZY_LOADED) ? relation.count(columns.first.name) :
    relation.count
end

#predicates_for_and_conditions(conditions) ⇒ Object

The following is a hack to get around Netzke’s broken handling of filtering on PostgreSQL enums columns.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/marty/monkey.rb', line 129

def predicates_for_and_conditions(conditions)
  return nil if conditions.empty?

  predicates = conditions.map do |q|
    q = HashWithIndifferentAccess.new(q)

    attr = q[:attr]
    method, assoc = method_and_assoc(attr)

    arel_table = assoc ? Arel::Table.new(assoc.klass.table_name.to_sym) :
                   @model.arel_table

    value = q["value"]
    op = q["operator"]

    attr_type = attr_type(attr)

    case attr_type
    when :datetime
      update_predecate_for_datetime(arel_table[method], op, value.to_date)
    when :string, :text
      update_predecate_for_string(arel_table[method], op, value)
    when :boolean
      update_predecate_for_boolean(arel_table[method], op, value)
    when :date
      update_predecate_for_rest(arel_table[method], op, value.to_date)
    when :enum
      # HACKY! monkey patching happens here...
      update_predecate_for_enum(arel_table[method], op, value)
    else
      update_predecate_for_rest(arel_table[method], op, value)
    end
  end

  # join them by AND
  predicates[1..-1].inject(predicates.first){ |r,p| r.and(p)  }
end

#update_predecate_for_enum(table, op, value) ⇒ Object



167
168
169
170
# File 'lib/marty/monkey.rb', line 167

def update_predecate_for_enum(table, op, value)
  col = Arel::Nodes::NamedFunction.new("CAST", [table.as("TEXT")])
  col.matches "%#{value}%"
end