Sha256: ffa4e8a14027ce2cc739cefde488f37b51cd74de34301b7e02915e64f0715fb6

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

module Rspectacular
  module ActiveRecord
    module Matchers
      class DateRangeMatcher
        include Shoulda::ActiveRecord::Helpers

        def initialize(dates)
          @begin_field  = dates[:from].to_s
          @end_field    = dates[:to].to_s

          @begin_method = "#{dates[:from].to_s}=".to_sym
          @end_method   = "#{dates[:to].to_s}=".to_sym
        end

        def matches?(rangeable)
          @rangeable = rangeable
          beginning_date = Date.today
          ending_date = Date.today - 1

          rangeable.send(@begin_method, beginning_date)
          rangeable.send(@end_method, ending_date)

          rangeable.valid?

          rangeable.errors[@begin_field].include?("must be on or before #{ending_date.to_s}") &&
          rangeable.errors[@end_field].include?("must be on or after #{beginning_date.to_s}")
        end

        def failure_message
          "#{@rangeable_class} should only accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
        end

        def negative_failure_message
          "#{@rangeable_class} shouldn't accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
        end

        def description
          "should be contain a valid range for #{@rangeable_class}"
        end
      end

      def validate_date_range(dates)
        DateRangeMatcher.new(dates)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rspectacular-0.59.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.58.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.57.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.56.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.55.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.54.0 lib/rspectacular/matchers/active_record/date_range_matcher.rb
rspectacular-0.53.1 lib/rspectacular/matchers/active_record/date_range_matcher.rb