Sha256: 737c96e7e12e3f56ef58ed7e8d12979cbf3e516da751453fb4878c9e9633c9dd

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "active_record/validations"

module HasUniqueThreeLetterCode
  extend ActiveSupport::Concern

  module ClassMethods
    attr_reader :code_sources
    def has_unique_three_letter_code(*columns)
      options = columns.last.is_a?(Hash) ? columns.pop : {}
      columns.each do |column|
        validates column,
            :uniqueness => (options[:uniqueness] || true),
            :length => {:is => 3, :allow_nil => true}.merge(options[:length] || {}),
            :exclusion => { :in => Proc.new { HasUniqueThreeLetterCode.config.forbidden_codes }, :message => "'%{value}' is not an appropriate word" }
        (@code_sources ||= {})[column] = options[:source] || :name
        before_validation do
          if options[:leave_blank]
            leave_blank = options[:leave_blank][:if]
            if (leave_blank.blank? || (leave_blank.is_a?(Proc) && !leave_blank.call(self)) || (leave_blank.is_a?(Symbol) && !self.__send__(leave_blank)))
              dont_leave_blank = options[:leave_blank][:unless]
              if (dont_leave_blank.blank? || (dont_leave_blank.is_a?(Proc) && dont_leave_blank.call(self)) || (dont_leave_blank.is_a?(Symbol) && self.__send__(dont_leave_blank)))
                set_three_letter_column column if self.__send__(column).blank?
              end
            end
          else
            set_three_letter_column column if self.__send__(column).blank?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_unique_three_letter_code-0.0.1 lib/has_unique_three_letter_code/class_methods.rb