Sha256: 47d20c5cb6789e9360eb0d3348edb802489b5b097d8677156a980ee0091f7efd

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

# +AutoIncrement+
module AutoIncrement
  # +AutoIncrement::Incrementor+
  class Incrementor
    def initialize(column = nil, options = {})
      if column.is_a? Hash
        options = column
        column = nil
      end

      @column = column || options[:column] || :code
      @options = options.reverse_merge initial: 1, force: false
      @options[:scope] = [@options[:scope]].compact unless @options[:scope].is_a?(Array)
      @options[:model_scope] = [@options[:model_scope]].compact unless @options[:model_scope].is_a?(Array)
    end

    def before_create(record)
      @record = record
      write if can_write?
    end

    alias before_validation before_create
    alias before_save before_create

    private

    def can_write?
      @record.send(@column).blank? || @options[:force]
    end

    def write
      @record.send :write_attribute, @column, increment
    end

    def build_scopes(query)
      @options[:scope].each do |scope|
        query = query.where(scope => @record.send(scope)) if scope.present? && @record.respond_to?(scope)
      end

      query
    end

    def build_model_scope(query)
      @options[:model_scope].reject(&:nil?).each do |scope|
        query = query.send(scope)
      end

      query
    end

    def maximum
      query = build_scopes(build_model_scope(@record.class))
      query.lock if lock?

      if string?
        query.select("#{@column} max")
             .order(Arel.sql("LENGTH(#{@column}) DESC, #{@column} DESC"))
             .first.try :max
      else
        query.maximum @column
      end
    end

    def lock?
      @options[:lock] == true
    end

    def increment
      max = maximum

      max.blank? ? @options[:initial] : max.next
    end

    def string?
      @options[:initial].instance_of?(String)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auto_increment-1.6.1 lib/auto_increment/incrementor.rb