Skip to content

Commit

Permalink
Stop using alias_method_chain
Browse files Browse the repository at this point in the history
alias_method_chain was deprecated in Rails 5.0 and removed in 5.1 [1]

Class methods are spilt between two modules because of a RSpec mock
issue [2]. We're using:
  - include: to allow the `alert_survey` method to still be mocked in
    our specs
  - prepend: to allows us to override a method and call `super` to run
    the original implementation of the method in Alaveteli core.

[1] rails/rails#19434
[2] rspec/rspec-mocks#1213
  • Loading branch information
lizconlan authored and gbp committed Nov 1, 2019
1 parent 15e908a commit 332f865
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/model_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def is_school?
end
end

# Add survey methods to RequestMailer
RequestMailer.class_eval do
module SurveyMethods
def survey_alert(info_request)
user = info_request.user

Expand All @@ -111,7 +110,7 @@ def survey_alert(info_request)
:subject => "Can you help us improve WhatDoTheyKnow?")
end

class << self
module ClassMethods
# Send an email with a link to the survey two weeks after a request was made,
# if the user has not already completed the survey.
def alert_survey
Expand Down Expand Up @@ -149,13 +148,29 @@ def alert_survey
store_sent.save!
end
end
end

def alert_new_response_reminders_with_alert_survey
alert_new_response_reminders_without_alert_survey
module OverrideClassMethods
def alert_new_response_reminders
super
alert_survey if AlaveteliConfiguration::send_survey_mails
end
end
end

alias_method_chain :alert_new_response_reminders, :alert_survey
# Add survey methods to RequestMailer
RequestMailer.class_eval do
include SurveyMethods

class << self
# Class methods are spilt between two modules because of a RSpec
# mock issue.
# We're using `include` to allow the `alert_survey` method to still
# be mocked in our specs.
# Using `prepend` to allows us to override a method and call `super`
# to run the original implementation of the method in Alaveteli core
include SurveyMethods::ClassMethods
prepend SurveyMethods::OverrideClassMethods
end
end

Expand Down

0 comments on commit 332f865

Please sign in to comment.