Sha256: fccd8e716cabddb0033fa76b24118363effd08b987798cdf6499be3b6424a887
Contents?: true
Size: 1.27 KB
Versions: 1
Compression:
Stored size: 1.27 KB
Contents
require 'active_record' module AutoIncrement def auto_increment(options = {}) raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty? default_options = { :column => :code, :scope => nil, :initial => 1, :force => false } options = default_options.merge(options) unless options.nil? options[:scope] = [options[:scope]] if options[:scope].class != Array before_create "auto_increment_#{options[:column]}" define_method "auto_increment_#{options[:column]}" do return if self.send(options[:column]).present? and !options[:force] query = self.class.scoped options[:scope].each do |scope| if scope.present? and respond_to?(scope) query = query.where "#{scope} = ?", send(scope) end end if options[:initial].class == String max = query.select("#{options[:column]} max").order("LENGTH(#{options[:column]}) DESC, #{options[:column]} DESC").first max = max.max if max.present? else max = query.maximum options[:column] end max = max.blank? ? options[:initial] : max.next write_attribute options[:column], max end end end # Extend ActiveRecord's functionality ActiveRecord::Base.send :extend, AutoIncrement
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
auto_increment-0.2 | lib/auto_increment/active_record.rb |