Sha256: b3520f974d69757e419f3a6133f254ecb0a631e5037628518c4343eb1fefa167

Contents?: true

Size: 1.98 KB

Versions: 16

Compression:

Stored size: 1.98 KB

Contents

module ActiveGraph
  module Node
    # This mixin replace the original save method and performs validation before the save.
    module Validations
      extend ActiveSupport::Concern
      include ActiveGraph::Shared::Validations

      # @return [Boolean] true if valid
      def valid?(context = nil)
        context ||= (new_record? ? :create : :update)
        super(context)
        errors.empty?
      end

      module ClassMethods
        def validates_uniqueness_of(*attr_names)
          validates_with UniquenessValidator, _merge_attributes(attr_names)
        end
      end

      class UniquenessValidator < ::ActiveModel::EachValidator
        def initialize(options)
          super(options.reverse_merge(case_sensitive: true))
        end

        def validate_each(record, attribute, value)
          return unless found(record, attribute, value).exists?

          record.errors.add(attribute, :taken, **options.except(:case_sensitive, :scope).merge(value: value))
        end

        def found(record, attribute, value)
          scopes, attributes = Array(options[:scope] || []).partition { |s| s.is_a?(Proc) }
          conditions = scope_conditions(record, attributes)

          # TODO: Added as find(:name => nil) throws error
          value = '' if value.nil?

          conditions[attribute] = options[:case_sensitive] ? value : /#{Regexp.escape(value.to_s)}/i

          found = if scopes.empty?
                    record.class.as(:result)
                  else
                    scopes.reduce(record) { |proxy, scope| proxy.instance_eval(&scope) }
                  end.where(conditions)
          found = found.where_not(neo_id: record.neo_id) if record._persisted_obj
          found
        end

        def message(instance)
          super || 'has already been taken'
        end

        def scope_conditions(instance, attributes)
          attributes.inject({}) do |conditions, key|
            conditions.merge(key => instance[key])
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
activegraph-12.0.0.beta.4 lib/active_graph/node/validations.rb
activegraph-11.5.0.beta.3 lib/active_graph/node/validations.rb
activegraph-12.0.0.beta.3 lib/active_graph/node/validations.rb
activegraph-12.0.0.beta.2 lib/active_graph/node/validations.rb
activegraph-11.5.0.beta.2 lib/active_graph/node/validations.rb
activegraph-12.0.0.beta.1 lib/active_graph/node/validations.rb
activegraph-11.5.0.beta.1 lib/active_graph/node/validations.rb
activegraph-11.5.0.alpha.1 lib/active_graph/node/validations.rb
activegraph-11.4.0 lib/active_graph/node/validations.rb
activegraph-11.3.1 lib/active_graph/node/validations.rb
activegraph-11.3.0 lib/active_graph/node/validations.rb
activegraph-11.2.0 lib/active_graph/node/validations.rb
activegraph-11.1.0 lib/active_graph/node/validations.rb
activegraph-11.1.0.beta.1 lib/active_graph/node/validations.rb
activegraph-11.1.0.alpha.4 lib/active_graph/node/validations.rb
activegraph-11.1.0.alpha.3 lib/active_graph/node/validations.rb