Sha256: f114accfbb989277b646ca9898045bd64d2a2757d30e4fab52426762fc424f3f

Contents?: true

Size: 687 Bytes

Versions: 3

Compression:

Stored size: 687 Bytes

Contents

# frozen_string_literal: true

class Gutentag::TagValidations
  DEFAULTS = {
    :presence   => true,
    :uniqueness => {:case_sensitive => false}
  }.freeze

  def self.call(klass)
    new(klass).call
  end

  def initialize(klass)
    @klass = klass
  end

  def call
    klass.validates :name, validation_options
  end

  private

  attr_reader :klass

  def add_length_validation?
    klass.table_exists? && limit.present?
  rescue ActiveRecord::NoDatabaseError
    false
  end

  def limit
    @limit ||= klass.columns_hash["name"].limit
  end

  def validation_options
    return DEFAULTS unless add_length_validation?

    DEFAULTS.merge :length => {:maximum => limit}
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gutentag-2.2.1 lib/gutentag/tag_validations.rb
gutentag-2.2.0 lib/gutentag/tag_validations.rb
gutentag-2.1.0 lib/gutentag/tag_validations.rb