Sha256: c9872cc4065d6fca75e8794c9378d0c22291fc8960b90c1e51f41d2471eea07d

Contents?: true

Size: 935 Bytes

Versions: 2

Compression:

Stored size: 935 Bytes

Contents

module FieldsValidator 

  extend ActiveSupport::Concern

  included do
    validate :validate_column_names if respond_to?(:validate)
  end

  class_methods do 
    def validate_required_attributes
      if required_attributes and required_attributes.any?
        required_attributes.each do |a|
          validates a.to_sym, presence: true 
        end 
      end
    end

    def required_attributes
      (defined?(super) and super.is_a?(Array)) ? super : []
    end

    def required_database_fields
      (defined?(super) and super.is_a?(Array)) ? super : []
    end

  end

  private

  def validate_column_names
    if self.class.required_database_fields and self.class.required_database_fields.any?
      self.class.required_database_fields.each { |rdf| errors.add(rdf, "does not have a column in the database.")  unless self.class.column_names.include?(rdf.to_s)} if self.class.required_database_fields.any?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morse_fields_validator-1.2.0 lib/fields_validator.rb
morse_fields_validator-1.1.10 lib/fields_validator.rb