diff --git a/lib/dekiru.rb b/lib/dekiru.rb index ce7c16c..e718de4 100644 --- a/lib/dekiru.rb +++ b/lib/dekiru.rb @@ -1,7 +1,6 @@ require 'dekiru/version' require 'dekiru/railtie' if defined?(::Rails) require 'dekiru/helper' -require 'dekiru/validators/existence' require 'dekiru/data_migration_operator' require 'dekiru/mail_security_interceptor' require 'dekiru/camelize_hash' diff --git a/lib/dekiru/validators/existence.rb b/lib/dekiru/validators/existence.rb deleted file mode 100644 index 9435f66..0000000 --- a/lib/dekiru/validators/existence.rb +++ /dev/null @@ -1,27 +0,0 @@ -# class Post < ActiveRecord::Base -# belongs_to :company -# belongs_to :user -# validates :user_id, presence: true, existence: { in: -> { company.users } } -# end - -module ActiveModel - module Validations - class ExistenceValidator < EachValidator - def validate_each(record, attribute, value) - ActiveSupport::Deprecation.warn('`ExistenceValidator` is deprecated and will be removed in v0.5. Please use `inclusion: { in: xx }` instead.') - - unless exists?(record, value) - record.errors.add(attribute, :existence, **options.except(:in).merge!(value: value)) - end - end - - def exists?(record, value) - unless options[:in].respond_to?(:call) - raise ArgumentError, '`in` option should be proc' - end - collection = record.instance_exec(&options[:in]) - collection.exists?(value) - end - end - end -end diff --git a/spec/dekiru/validators/existence_spec.rb b/spec/dekiru/validators/existence_spec.rb deleted file mode 100644 index d94730b..0000000 --- a/spec/dekiru/validators/existence_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -require 'spec_helper' - -describe ActiveModel::Validations::ExistenceValidator do - let(:user_class) do - Class.new do - def self.exists?(id) - %w[valid_id].member?(id) - end - end - end - let(:model_class) do - _options = options - - Struct.new(:user_id, :users) do - include ActiveModel::Validations - - def self.name - 'DummyModel' - end - - validates :user_id, existence: _options - end - end - - describe 'validate' do - subject(:valid?) do - model_class.new(user_id, user_class).valid? - end - - context 'with exists id' do - let(:user_id) { 'valid_id' } - let(:options) { { in: -> { users } } } - - it { is_expected.to eq true } - end - - context 'with exists not id' do - let(:user_id) { 'invalid_id' } - let(:options) { { in: -> { users } } } - - it { is_expected.to eq false } - end - - context 'with invalid option' do - let(:user_id) { 'valid_id' } - let(:options) { { in: user_class } } - - it { expect { valid? }.to raise_error(ArgumentError) } - end - end -end