Class: RSpectacular::ActiveRecord::Matchers::DateRangeMatcher

Inherits:
Object
  • Object
show all
Includes:
Shoulda::ActiveRecord::Helpers
Defined in:
lib/rspectacular/matchers/active_record/date_range_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(dates) ⇒ DateRangeMatcher

Returns a new instance of DateRangeMatcher.



7
8
9
10
11
12
13
# File 'lib/rspectacular/matchers/active_record/date_range_matcher.rb', line 7

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

Instance Method Details

#descriptionObject



37
38
39
# File 'lib/rspectacular/matchers/active_record/date_range_matcher.rb', line 37

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

#failure_messageObject



29
30
31
# File 'lib/rspectacular/matchers/active_record/date_range_matcher.rb', line 29

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

#matches?(rangeable) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspectacular/matchers/active_record/date_range_matcher.rb', line 15

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

#negative_failure_messageObject



33
34
35
# File 'lib/rspectacular/matchers/active_record/date_range_matcher.rb', line 33

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