Sha256: 417b4e8e7a24e4b69baded41020cd8440a416899d681fd4101fb804f4819f419
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
module OrderFu class AttributeOrder attr_accessor :attribute_name, :order_direction def initialize(attribute_name, order_direction) @attribute_name, @order_direction = attribute_name, order_direction end def to_s "#{@attribute_name} #{@order_direction.to_s.upcase}" end def <=>(other) [self.order_direction.to_s, self.attribute_name.to_s] <=> [other.order_direction.to_s, other.attribute_name.to_s] end def ==(other) (self <=> other) == 0 end def eql?(other) self.hash == other.hash end def hash ["AttributeOrder", self.order_direction.to_s, self.attribute_name.to_s].hash end end # ActiveRecord Extention module ActiveRecordExtentions def add_order_with_order_fu!(sql, order, scope = :auto) order = build_order(order) scope = scope(:find) if :auto == scope scoped_order = build_order(scope[:order]) if scope if order sql << " ORDER BY #{order}" sql << ", #{scoped_order}" if scoped_order else sql << " ORDER BY #{scoped_order}" if scoped_order end end def build_order(order) case order when AttributeOrder then order.to_s when Array then order.map { |o| o.to_s } * ',' else order end end end module SymbolExtensions [ :asc, :desc ].each do |order_direction| define_method(order_direction) do AttributeOrder.new(self, order_direction) end end end end require 'activerecord' Symbol.send(:include, OrderFu::SymbolExtensions) ActiveRecord::Base.send(:extend, OrderFu::ActiveRecordExtentions) module ::ActiveRecord class Base class << self # use the sanitize_sql_hash_for_conditions_with_attribute_queries alias_method_chain :add_order!, :order_fu end end end RAILS_DEFAULT_LOGGER.info("** OrderFu: initialized properly")
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sprsquish-order_fu-0.2.0 | lib/order_fu.rb |