Sha256: 1bcfc5d1dd16bc126129d3c3d3c9b7f9313cf152f5b5b9d6b1f167e9b58ce150
Contents?: true
Size: 1.29 KB
Versions: 8
Compression:
Stored size: 1.29 KB
Contents
require 'much-plugin' module Ardb module DefaultOrderBy include MuchPlugin DEFAULT_ATTRIBUTE = :order_by DEFAULT_SCOPE_PROC = proc{ self.class.scoped } plugin_included do extend ClassMethods include InstanceMethods @ardb_default_order_by_config = {} end module ClassMethods def default_order_by(options = nil) options ||= {} @ardb_default_order_by_config.merge!({ :attribute => options[:attribute] || DEFAULT_ATTRIBUTE, :scope_proc => options[:scope] || DEFAULT_SCOPE_PROC }) before_validation :ardb_default_order_by, :on => :create end def ardb_default_order_by_config @ardb_default_order_by_config end end module InstanceMethods private def reset_order_by attr_name = self.class.ardb_default_order_by_config[:attribute] scope_proc = self.class.ardb_default_order_by_config[:scope_proc] current_max = self.instance_eval(&scope_proc).maximum(attr_name) || 0 self.send("#{attr_name}=", current_max + 1) end def ardb_default_order_by attr_name = self.class.ardb_default_order_by_config[:attribute] reset_order_by if self.send(attr_name).to_s.empty? true end end end end
Version data entries
8 entries across 8 versions & 1 rubygems