Sha256: e515c928c0fc380c2e31360296e7bd58c46c468fc76b8076e95fe9192c640f9e

Contents?: true

Size: 907 Bytes

Versions: 1

Compression:

Stored size: 907 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
      required_attributes.each do |a|
        validates a.to_sym, presence: true 
      end if required_attributes and required_attributes.any?
    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)} if self.class.required_database_fields.any?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
morse_fields_validator-1.1.6 lib/fields_validator.rb