leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 213 forks source link

Subclass existing column processor #268

Closed brian-vogogo closed 8 years ago

brian-vogogo commented 8 years ago

Goal:

class ViewColumnMoney < Wice::Columns::ViewColumnFloat
  # ... new behaviour ...
end

but the Wice class is not loaded until later. It feels like I need to define my new class in the middle of load_column_processors in lib/wice/column.rb:9

Any options besides copying the source of the filter?

brian-vogogo commented 8 years ago

Figured out a solution.

Added this in an initializer:

# Allow filters on money columns stored in cents.
# Converts a user-entered filter of "20.00" into a SQL query for 2000 cents.
#
def define_conditions_generator_column_money
  Class.new(Wice::Columns::ConditionsGeneratorColumnInteger) {
    def get_op_and_value(val)
      op, num = super
      num = (num * 100.0).round.to_i if num
      return op, num
    end
  }
end

and in the Wice config:

  Wice::Defaults::ADDITIONAL_COLUMN_PROCESSORS = {
    :money =>  ['Wice::Columns::ViewColumnFloat', 'define_conditions_generator_column_money']
  }