Sha256: cd7bdd0efbe223def8c805773040e88d8ab051f4183a33cf287be8c87346409d

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module Jacaranda 

  def self.included(base)
    base.extend(ClassMethods)
  end 

  module ClassMethods

    def acts_as_jacaranda(options = {})
      configuration.update(options)
      begin
        verify!
        create_predicate_methods
        create_scope_methods
      rescue => e
        if e.kind_of?(Jacaranda::JacarandaError)
          raise e
        else
          raise Jacaranda::JacarandaError, I18n.translate("jacaranda.errors.messages.unknown")
        end
      end
    end

    private

      def klazz
        self
      end   

      def configuration
        @configuration ||= { scoped: false }
      end

      def inclusion_validators
        klazz.validators.delete_if do |v| 
          v.class.to_s != "ActiveModel::Validations::InclusionValidator"
        end 
      end

      def validators_in 
        inclusion_validators.sum { |v| v.options[:in] }
      end

      def duplicate_validators 
        validators_in.select { |e| validators_in.count(e) > 1 }.uniq
      end

      def verify!
        verify_precedence!
        verify_duplicated!
      end

      def verify_precedence!
        return if validators_in.kind_of?(Array)
        if validators_in.zero?
          raise JacarandaError, I18n.translate("jacaranda.errors.messages.precedence", model: klazz.to_s)
        end
      end

      def verify_duplicated!
        if duplicate_validators.any?
          raise JacarandaError, I18n.translate("jacaranda.errors.messages.duplicated", model: klazz.to_s, validators: duplicate_validators.to_sentence)
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jacaranda-0.0.2 lib/jacaranda/base.rb
jacaranda-0.0.1 lib/jacaranda/base.rb