Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove Tariff validation helper Sequel plugin

No longer being used since we moved to conformance validator
  • Loading branch information...
commit 3c9e30a4726a028ade04a3fd70e515098b2b1ea6 1 parent edc47bd
@saulius saulius authored
View
1  config/initializers/sequel.rb
@@ -6,7 +6,6 @@
Sequel::Model.plugin :take
Sequel::Model.plugin :validation_class_methods
-Sequel::Model.plugin :tariff_validation_helper
Sequel::Model.db.extension :pagination
Sequel::Model.db.extension :server_block
View
106 lib/sequel/plugins/tariff_validation_helper.rb
@@ -1,106 +0,0 @@
-module Sequel
- module Plugins
- module TariffValidationHelper
- module ClassMethods
- def validates_validity_date_span_of(*atts)
- opts = {
- message: "%s does not span validity date of %s",
- tag: :validity_date_span,
- }.merge!(extract_options!(atts))
-
- reflect_validation(:validity_date_span, opts, atts)
- atts << opts
-
- validates_each(*atts) do |o, a, v|
- associated_record = o.send(a)
- error_message = opts[:message] % [associated_record.class.name, o.class.name]
-
- if associated_record.present?
- if o.validity_start_date.present? && associated_record.validity_start_date.present?
- o.errors.add(a, error_message) if associated_record.validity_start_date > o.validity_start_date
- end
- if o.validity_end_date.present? && associated_record.validity_end_date.present?
- o.errors.add(a, error_message) if associated_record.validity_end_date < o.validity_end_date
- end
- if o.validity_start_date.present? && associated_record.validity_end_date.present?
- o.errors.add(a, error_message) if associated_record.validity_end_date < o.validity_start_date
- end
- if o.validity_end_date.present? && associated_record.validity_start_date.present?
- o.errors.add(a, error_message) if associated_record.validity_start_date > o.validity_end_date
- end
- end
- end
- end
-
- def validates_validity_dates
- atts = [:validity_start_date, :validity_end_date]
- opts = {
- message: "validity end date must be greater or equal to validity start date",
- tag: :validity_date,
- }.merge!(extract_options!(atts))
-
- reflect_validation(:validity_date, opts, atts)
- atts << opts
-
- validates_each(*atts) do |o, a, v|
- if o.validity_start_date && o.validity_end_date && (o.validity_end_date < o.validity_start_date)
- o.errors.add(a, 'must be less then or equal to end date')
- end
- end
- end
-
- def validates_input_of(*atts)
- opts = {
- :message => "cannot be set unless condition is met",
- :requires => true,
- :tag => :input,
- }.merge!(extract_options!(atts))
-
- reflect_validation(:input, opts, atts)
-
- atts << opts
- validates_each(*atts) do |o, a, v|
- o.errors.add(a, opts[:message]) if o.send(a).present? && !o.send(opts[:requires])
- end
- end
-
- def validates_exclusion_of(*atts)
- opts = extract_options!(atts)
- n = opts[:from]
- unless n && (n.respond_to?(:cover?) || n.respond_to?(:include?))
- raise ArgumentError, "The :from parameter is required, and must respond to cover? or include?"
- end
- opts[:message] ||= "is in range or set: #{n.inspect}"
- reflect_validation(:exclusion, opts, atts)
- atts << opts
- validates_each(*atts) do |o, a, v|
- o.errors.add(a, opts[:message]) if n.send(n.respond_to?(:cover?) ? :cover? : :include?, v)
- end
- end
-
- def validates_associated(associations, config = {})
- atts = [associations].flatten
-
- opts = {
- message: "%s must be valid",
- tag: :associated,
- }.merge!(extract_options!(atts)).merge!(config)
-
- reflect_validation(:associated, opts, atts)
- atts << opts
-
- validates_each(*atts) do |object, association, value|
- if object.send(association).present?
- if opts[:ensure].present?
- object.errors.add(association, opts[:message] % [association]) unless object.send(opts[:ensure])
- else
- associated_records = [object.send(association)].flatten
- object.errors.add(association, opts[:message] % [association]) unless associated_records.all?(&:valid?)
- end
- end
- end
- end
- end
- end
- end
-end
View
242 spec/unit/sequel/extensions/tariff_validations_helper_spec.rb
@@ -1,242 +0,0 @@
-require 'spec_helper'
-require 'unit/sequel/spec_helper'
-
-# Modeled after https://github.com/jeremyevans/sequel/blob/master/spec/extensions/validation_class_methods_spec.rb
-
-model_class = proc do |klass, &block|
- c = Class.new(klass)
- c.plugin :validation_class_methods
- c.plugin :tariff_validation_helper
- c.class_eval(&block) if block
- c
-end
-
-describe Sequel::Model do
- describe 'validates_exclusion_of' do
- before do
- @c = model_class.call Sequel::Model do
- columns :value
- end
- @m = @c.new
- end
-
- it "should validate exclusion_of with an array" do
- @c.validates_exclusion_of :value, :from => [1,2]
- @m.should be_valid
- @m.value = 1
- @m.should_not be_valid
- @m.value = 1.5
- @m.should be_valid
- @m.value = 2
- @m.should_not be_valid
- @m.value = 3
- @m.should be_valid
- end
-
- it "should validate exclusion_of with a range" do
- @c.validates_exclusion_of :value, :from => 1..4
- @m.should be_valid
- @m.value = 1
- @m.should_not be_valid
- @m.value = 1.5
- @m.should_not be_valid
- @m.value = 0
- @m.should be_valid
- @m.value = 5
- @m.should be_valid
- end
-
- it "should raise an error if exclusion_of doesn't receive a valid :in option" do
- lambda{@c.validates_exclusion_of :value}.should raise_error(ArgumentError)
- lambda{@c.validates_exclusion_of :value, :from => 1}.should raise_error(ArgumentError)
- end
-
- it "should raise an error if exclusion_of handles :allow_nil too" do
- @c.validates_exclusion_of :value, :from => 1..4, :allow_nil => true
- @m.value = nil
- @m.should be_valid
- @m.value = 0
- @m.should be_valid
- end
- end
-
- describe 'validates_validity_date_span_of' do
- before do
- @c = model_class.call Sequel::Model do
- columns :validity_start_date, :validity_end_date, :associated_record
- end
- @m = @c.new
- end
-
- context 'associated record blank' do
- it 'should validate validity date span' do
- @c.validates_validity_date_span_of :associated_record
- @m.associated_record = nil
- @m.should be_valid
- end
- end
-
- context 'associated record present' do
- before {
- @m.validity_start_date = Date.new(2013,2,1)
- @m.validity_end_date = Date.new(2013,3,1)
- }
-
- context 'associated record spans objects dates' do
- it 'should validate validity date span' do
- @c.validates_validity_date_span_of :associated_record
- @m.associated_record = double(validity_start_date: Date.new(2013,1,1),
- validity_end_date: Date.new(2013,4,1))
- @m.should be_valid
- end
- end
-
- context 'associated record does not span objects dates' do
- it 'should validate validity date span (wrong start date)' do
- @c.validates_validity_date_span_of :associated_record
- @m.associated_record = double(validity_start_date: Date.new(2013,3,1),
- validity_end_date: Date.new(2013,4,1))
- @m.should_not be_valid
- end
-
- it 'should validate validity date span (wrong end date)' do
- @c.validates_validity_date_span_of :associated_record
- @m.associated_record = double(validity_start_date: Date.new(2013,1,1),
- validity_end_date: Date.new(2013,2,1))
- @m.should_not be_valid
- end
-
- it 'should validate validity date span (wrong both dates)' do
- @c.validates_validity_date_span_of :associated_record
- @m.associated_record = double(validity_start_date: Date.new(2013,2,15),
- validity_end_date: Date.new(2013,2,15))
- @m.should_not be_valid
- end
- end
- end
- end
-
- describe 'validates_validity_dates' do
- before do
- @c = model_class.call Sequel::Model do
- columns :validity_start_date, :validity_end_date
- end
- @m = @c.new
- end
-
- it 'should validate validity dates' do
- @c.validates_validity_dates
- @m.validity_start_date = Date.new(2013,2,1)
- @m.validity_end_date = Date.new(2013,3,1)
- @m.should be_valid
- @m.validity_start_date = Date.new(2013,2,1)
- @m.validity_end_date = Date.new(2013,1,1)
- @m.should_not be_valid
- end
- end
-
- describe 'validates_input_of' do
- before do
- @c = model_class.call Sequel::Model do
- columns :something
- end
- @m = @c.new
- end
-
- context 'attribute present' do
- it 'validates input of' do
- @c.validates_input_of :something, requires: :something_to_be_true
- @m.stub(:something_to_be_true).and_return(true)
- @m.something = true
- @m.should be_valid
- end
- end
-
- context 'attribute blank' do
- it 'validates input of' do
- @c.validates_input_of :something, requires: :something_to_be_true
- @m.something = nil
- @m.should be_valid
- end
- end
-
- context 'requirement not fullfilled' do
- it 'validates input of' do
- @c.validates_input_of :something, requires: :something_to_be_true
- @m.stub(:something_to_be_true).and_return(false)
- @m.something = true
- @m.should_not be_valid
- end
- end
- end
-
- describe 'validates_associated' do
- context 'associated records present' do
- before do
- @c = model_class.call Sequel::Model do
- columns :something
-
- def mushrooms
- ['shroom', 'boom']
- end
-
- def there_are_at_least_two_mushrooms?
- mushrooms.size >= 2
- end
-
- def there_are_at_least_three_mushrooms?
- mushrooms.size >= 3
- end
- end
- @m = @c.new
- end
-
- context 'ensure option provided' do
- it 'should validate associated' do
- @c.validates_associated :mushrooms, ensure: :there_are_at_least_two_mushrooms?
- @m.should be_valid
-
- @c.validates_associated :mushrooms, ensure: :there_are_at_least_three_mushrooms?
- @m.should_not be_valid
- end
- end
-
- context 'ensure option missing' do
- context 'associated records are all valid' do
- it 'should validate associated' do
- @c.validates_associated :mushrooms
- @m.stub(:mushrooms).and_return([double('record', valid?: true).as_null_object])
- @m.should be_valid
- end
- end
-
- context 'some associated records are not valid' do
- it 'should validate associated' do
- @c.validates_associated :mushrooms
- @m.stub(:mushrooms).and_return([double('record', valid?: false).as_null_object,
- double('record', valid?: false).as_null_object])
- @m.should_not be_valid
- end
- end
- end
- end
-
- context 'associated records missing' do
- before do
- @c = model_class.call Sequel::Model do
- columns :something
-
- def mushrooms
- nil
- end
- end
- @m = @c.new
- end
-
- it 'should validate associated' do
- @c.validates_associated :mushrooms
- @m.should be_valid
- end
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.