From 903b505394a9b79415e66533c32da0ce43ea515d Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Tue, 14 Oct 2014 20:04:17 -0700
Subject: [PATCH 01/12] Rebuild Gemfile_master.lock based on the lastest
 Gemfile.lock

---
 Gemfile_master.lock | 172 ++++++++++++++++++++++----------------------
 1 file changed, 84 insertions(+), 88 deletions(-)

diff --git a/Gemfile_master.lock b/Gemfile_master.lock
index 16e366b7c..2ae98fe1c 100644
--- a/Gemfile_master.lock
+++ b/Gemfile_master.lock
@@ -7,57 +7,57 @@ GIT
 
 GIT
   remote: https://github.com/rails/rails.git
-  revision: 316962d0922992fbe756521bd7c94a751aa1253e
+  revision: aa6637d140c2ebd28bbd23fc250af033a065dbe8
   specs:
-    actionmailer (4.2.0.beta1)
-      actionpack (= 4.2.0.beta1)
-      actionview (= 4.2.0.beta1)
-      activejob (= 4.2.0.beta1)
+    actionmailer (4.2.0.beta4)
+      actionpack (= 4.2.0.beta4)
+      actionview (= 4.2.0.beta4)
+      activejob (= 4.2.0.beta4)
       mail (~> 2.5, >= 2.5.4)
-      rails-dom-testing (~> 1.0, >= 1.0.2)
-    actionpack (4.2.0.beta1)
-      actionview (= 4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
+      rails-dom-testing (~> 1.0, >= 1.0.4)
+    actionpack (4.2.0.beta4)
+      actionview (= 4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
       rack (~> 1.6.0.beta)
       rack-test (~> 0.6.2)
-      rails-dom-testing (~> 1.0, >= 1.0.2)
-      rails-html-sanitizer (~> 1.0)
-    actionview (4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
+      rails-dom-testing (~> 1.0, >= 1.0.4)
+      rails-html-sanitizer (~> 1.0, >= 1.0.1)
+    actionview (4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
       builder (~> 3.1)
       erubis (~> 2.7.0)
-      rails-dom-testing (~> 1.0, >= 1.0.2)
-      rails-html-sanitizer (~> 1.0)
-    activejob (4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
-      globalid (>= 0.2.3)
-    activemodel (4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
+      rails-dom-testing (~> 1.0, >= 1.0.4)
+      rails-html-sanitizer (~> 1.0, >= 1.0.1)
+    activejob (4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
+      globalid (>= 0.3.0)
+    activemodel (4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
       builder (~> 3.1)
-    activerecord (4.2.0.beta1)
-      activemodel (= 4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
-      arel (>= 6.0.0.beta1, < 6.1)
-    activesupport (4.2.0.beta1)
+    activerecord (4.2.0.beta4)
+      activemodel (= 4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
+      arel (>= 6.0.0.beta2, < 6.1)
+    activesupport (4.2.0.beta4)
       i18n (>= 0.7.0.beta1, < 0.8)
       json (~> 1.7, >= 1.7.7)
       minitest (~> 5.1)
       thread_safe (~> 0.1)
       tzinfo (~> 1.1)
-    rails (4.2.0.beta1)
-      actionmailer (= 4.2.0.beta1)
-      actionpack (= 4.2.0.beta1)
-      actionview (= 4.2.0.beta1)
-      activejob (= 4.2.0.beta1)
-      activemodel (= 4.2.0.beta1)
-      activerecord (= 4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
+    rails (4.2.0.beta4)
+      actionmailer (= 4.2.0.beta4)
+      actionpack (= 4.2.0.beta4)
+      actionview (= 4.2.0.beta4)
+      activejob (= 4.2.0.beta4)
+      activemodel (= 4.2.0.beta4)
+      activerecord (= 4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
       bundler (>= 1.3.0, < 2.0)
-      railties (= 4.2.0.beta1)
+      railties (= 4.2.0.beta4)
       sprockets-rails (~> 3.0.0.beta1)
-    railties (4.2.0.beta1)
-      actionpack (= 4.2.0.beta1)
-      activesupport (= 4.2.0.beta1)
+    railties (4.2.0.beta4)
+      actionpack (= 4.2.0.beta4)
+      activesupport (= 4.2.0.beta4)
       rake (>= 0.8.7)
       thor (>= 0.18.1, < 2.0)
 
@@ -82,13 +82,10 @@ GEM
     active_model_serializers (0.8.2)
       activemodel (>= 3.0)
     addressable (2.3.6)
-    airbrake (3.1.2)
-      activesupport
-      builder
     annotate (2.6.5)
       activerecord (>= 2.3.0)
       rake (>= 0.8.7)
-    arel (6.0.0.beta1)
+    arel (6.0.0.beta2)
     barber (0.4.2)
       ember-source
       execjs
@@ -114,25 +111,25 @@ GEM
       dotenv-deployment (~> 0.0.2)
     dotenv-deployment (0.0.2)
     email_reply_parser-discourse (0.6)
-    ember-data-source (1.0.0.beta.9)
+    ember-data-source (0.14)
       ember-source
-    ember-rails (0.15.0)
+    ember-rails (0.14.1)
       active_model_serializers
       barber (>= 0.4.1)
-      ember-data-source (>= 1.0.0.beta.5)
-      ember-source (>= 1.1.0)
+      ember-data-source
+      ember-source
       execjs (>= 1.2)
-      handlebars-source (> 1.0.0)
+      handlebars-source
       jquery-rails (>= 1.0.17)
       railties (>= 3.1)
     ember-source (1.6.0.beta.2)
       handlebars-source (~> 1.0)
     erubis (2.7.0)
     eventmachine (1.0.3)
-    excon (0.39.5)
+    excon (0.39.6)
     execjs (2.2.1)
     exifr (1.1.3)
-    fabrication (2.11.3)
+    fabrication (2.9.8)
     fakeweb (1.3.0)
     faraday (0.9.0)
       multipart-post (>= 1.2, < 3)
@@ -146,7 +143,7 @@ GEM
     fast_xs (0.8.0)
     fastimage (1.6.3)
       addressable (~> 2.3, >= 2.3.5)
-    ffi (1.9.3)
+    ffi (1.9.5)
     flamegraph (0.0.5)
       fast_stack
     fog (1.22.1)
@@ -155,7 +152,7 @@ GEM
       fog-json
       ipaddress (~> 0.5)
       nokogiri (~> 1.5, >= 1.5.11)
-    fog-brightbox (0.5.0)
+    fog-brightbox (0.5.1)
       fog-core (~> 1.22)
       fog-json
       inflecto
@@ -176,7 +173,7 @@ GEM
     gctools (0.2.3)
     given_core (3.5.4)
       sorcerer (>= 0.3.7)
-    globalid (0.2.3)
+    globalid (0.3.0)
       activesupport (>= 4.1.0)
     guess_html_encoding (0.0.9)
     handlebars-source (1.3.0)
@@ -205,7 +202,7 @@ GEM
     librarian (0.1.2)
       highline
       thor (~> 0.15)
-    libv8 (3.16.14.3)
+    libv8 (3.16.14.7)
     listen (0.7.3)
     logster (0.1.6)
     loofah (2.0.1)
@@ -215,15 +212,15 @@ GEM
       mime-types (~> 1.16)
       treetop (~> 1.4.8)
     memory_profiler (0.0.4)
-    message_bus (0.9.5)
+    message_bus (1.0.5)
       eventmachine
       rack (>= 1.1.3)
       redis
     metaclass (0.0.4)
     method_source (0.8.2)
     mime-types (1.25.1)
-    mini_portile (0.6.0)
-    minitest (5.4.1)
+    mini_portile (0.6.1)
+    minitest (5.4.2)
     mocha (1.1.0)
       metaclass (~> 0.0.1)
     mock_redis (0.13.2)
@@ -237,8 +234,8 @@ GEM
       net-ssh (>= 2.6.5)
     net-ssh (2.9.1)
     netrc (0.7.7)
-    nokogiri (1.6.3.1)
-      mini_portile (= 0.6.0)
+    nokogiri (1.6.4.1)
+      mini_portile (~> 0.6.0)
     nokogumbo (1.1.12)
       nokogiri
     oauth (0.4.7)
@@ -274,7 +271,7 @@ GEM
     omniauth-twitter (1.0.1)
       multi_json (~> 1.3)
       omniauth-oauth (~> 1.0)
-    onebox (1.4.9)
+    onebox (1.5.3)
       moneta (~> 0.7)
       multi_json (~> 1.7)
       mustache (~> 0.99)
@@ -293,7 +290,7 @@ GEM
       pry (>= 0.9.10, < 0.11.0)
     pry-rails (0.3.2)
       pry (>= 0.9.10)
-    puma (2.9.0)
+    puma (2.9.1)
       rack (>= 1.1, < 2.0)
     qunit-rails (0.0.7)
       railties
@@ -307,13 +304,13 @@ GEM
       rack
     rack-test (0.6.2)
       rack (>= 1.0)
-    rails-deprecated_sanitizer (1.0.2)
+    rails-deprecated_sanitizer (1.0.3)
       activesupport (>= 4.2.0.alpha)
-    rails-dom-testing (1.0.2)
-      activesupport
+    rails-dom-testing (1.0.4)
+      activesupport (>= 4.2.0.beta, < 5.0)
       nokogiri (~> 1.6.0)
       rails-deprecated_sanitizer (>= 1.0.1)
-    rails-html-sanitizer (1.0.0)
+    rails-html-sanitizer (1.0.1)
       loofah (~> 2.0)
     rails-observers (0.1.2)
       activemodel (~> 4.0)
@@ -338,29 +335,28 @@ GEM
       netrc (~> 0.7)
     rinku (1.7.3)
     rmmseg-cpp (0.2.9)
-    rspec (3.0.0)
-      rspec-core (~> 3.0.0)
-      rspec-expectations (~> 3.0.0)
-      rspec-mocks (~> 3.0.0)
-    rspec-core (3.0.4)
-      rspec-support (~> 3.0.0)
-    rspec-expectations (3.0.4)
-      diff-lcs (>= 1.2.0, < 2.0)
-      rspec-support (~> 3.0.0)
+    rspec (2.99.0)
+      rspec-core (~> 2.99.0)
+      rspec-expectations (~> 2.99.0)
+      rspec-mocks (~> 2.99.0)
+    rspec-collection_matchers (1.0.0)
+      rspec-expectations (>= 2.99.0.beta1)
+    rspec-core (2.99.2)
+    rspec-expectations (2.99.2)
+      diff-lcs (>= 1.1.3, < 2.0)
     rspec-given (3.5.4)
       given_core (= 3.5.4)
       rspec (>= 2.12)
-    rspec-mocks (3.0.4)
-      rspec-support (~> 3.0.0)
-    rspec-rails (3.0.2)
+    rspec-mocks (2.99.2)
+    rspec-rails (2.99.0)
       actionpack (>= 3.0)
+      activemodel (>= 3.0)
       activesupport (>= 3.0)
       railties (>= 3.0)
-      rspec-core (~> 3.0.0)
-      rspec-expectations (~> 3.0.0)
-      rspec-mocks (~> 3.0.0)
-      rspec-support (~> 3.0.0)
-    rspec-support (3.0.4)
+      rspec-collection_matchers
+      rspec-core (~> 2.99.0)
+      rspec-expectations (~> 2.99.0)
+      rspec-mocks (~> 2.99.0)
     rtlit (0.0.5)
     ruby-openid (2.5.0)
     ruby-readability (0.7.0)
@@ -370,7 +366,7 @@ GEM
       crass (~> 0.2.0)
       nokogiri (>= 1.4.4)
       nokogumbo (= 1.1.12)
-    sass (3.4.2)
+    sass (3.2.19)
     seed-fu (2.3.3)
       activerecord (>= 3.1, < 4.2)
       activesupport (>= 3.1, < 4.2)
@@ -380,16 +376,16 @@ GEM
     shoulda-context (1.2.1)
     shoulda-matchers (2.7.0)
       activesupport (>= 3.0.0)
-    sidekiq (3.2.3)
-      celluloid (>= 0.15.2)
+    sidekiq (3.2.5)
+      celluloid (= 0.15.2)
       connection_pool (>= 2.0.0)
       json
       redis (>= 3.0.6)
       redis-namespace (>= 1.3.1)
     simple-rss (1.3.1)
-    simplecov (0.9.0)
+    simplecov (0.9.1)
       docile (~> 1.1.0)
-      multi_json
+      multi_json (~> 1.0)
       simplecov-html (~> 0.8.0)
     simplecov-html (0.8.0)
     sinatra (1.4.5)
@@ -402,7 +398,7 @@ GEM
     spork-rails (4.0.0)
       rails (>= 3.0.0, < 5)
       spork (>= 1.0rc0)
-    sprockets (2.12.1)
+    sprockets (2.12.3)
       hike (~> 1.2)
       multi_json (~> 1.0)
       rack (~> 1.0)
@@ -447,7 +443,6 @@ PLATFORMS
 DEPENDENCIES
   actionpack-action_caching!
   active_model_serializers (~> 0.8.0)
-  airbrake (= 3.1.2)
   annotate
   barber
   better_errors
@@ -457,7 +452,7 @@ DEPENDENCIES
   ember-rails
   ember-source (= 1.6.0.beta.2)
   eventmachine
-  fabrication
+  fabrication (= 2.9.8)
   fakeweb (~> 1.3.0)
   fast_blank
   fast_xor
@@ -514,6 +509,7 @@ DEPENDENCIES
   rest-client
   rinku
   rmmseg-cpp
+  rspec (= 2.99.0)
   rspec-given
   rspec-rails
   rtlit

From 84d4307b1c45cd1b90c4dd32903d5435e9b73b9e Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Tue, 14 Oct 2014 20:05:38 -0700
Subject: [PATCH 02/12] actionpack-action_caching was released a long time ago

---
 Gemfile             |  4 ++--
 Gemfile_master.lock | 11 +++--------
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/Gemfile b/Gemfile
index b3aa24712..b6dfd8fb1 100644
--- a/Gemfile
+++ b/Gemfile
@@ -71,11 +71,11 @@ gem 'seed-fu', '~> 2.3.3'
 
 if rails_master?
   gem 'rails', git: 'https://github.com/rails/rails.git'
-  gem 'actionpack-action_caching', git: 'https://github.com/rails/actionpack-action_caching.git'
 else
   gem 'rails'
-  gem 'actionpack-action_caching'
 end
+
+gem 'actionpack-action_caching'
 gem 'rails-observers'
 
 # Rails 4.1.6+ will relax the mail gem version requirement to `~> 2.5, >= 2.5.4`.
diff --git a/Gemfile_master.lock b/Gemfile_master.lock
index 2ae98fe1c..c0e27f439 100644
--- a/Gemfile_master.lock
+++ b/Gemfile_master.lock
@@ -1,10 +1,3 @@
-GIT
-  remote: https://github.com/rails/actionpack-action_caching.git
-  revision: a9f3f09477b12b51faa6756005eee4103f7f4030
-  specs:
-    actionpack-action_caching (1.1.0)
-      actionpack (>= 4.0.0, < 5.0)
-
 GIT
   remote: https://github.com/rails/rails.git
   revision: aa6637d140c2ebd28bbd23fc250af033a065dbe8
@@ -79,6 +72,8 @@ PATH
 GEM
   remote: https://rubygems.org/
   specs:
+    actionpack-action_caching (1.1.1)
+      actionpack (>= 4.0.0, < 5.0)
     active_model_serializers (0.8.2)
       activemodel (>= 3.0)
     addressable (2.3.6)
@@ -441,7 +436,7 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
-  actionpack-action_caching!
+  actionpack-action_caching
   active_model_serializers (~> 0.8.0)
   annotate
   barber

From f955ec6018fcf47fbc3699141314fd2260cb42b5 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Sun, 17 Aug 2014 18:46:53 -0700
Subject: [PATCH 03/12] Use Rails 4.2+ API for typecasting on master

Note: this is still considered a private (internal) API on Rails side and is
subject to change in the future.
---
 lib/sql_builder.rb | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/lib/sql_builder.rb b/lib/sql_builder.rb
index 5a99f2771..6521afeb0 100644
--- a/lib/sql_builder.rb
+++ b/lib/sql_builder.rb
@@ -71,11 +71,20 @@ class SqlBuilder
 
   #AS reloads this on tests
   remove_const :FTYPE_MAP if defined? FTYPE_MAP
-  FTYPE_MAP = {
-    23 => :value_to_integer,
-    1114 => :string_to_time,
-    16 => :value_to_boolean
-  }
+
+  if rails_master?
+    FTYPE_MAP = {
+      23 => ActiveRecord::Type::Integer.new,
+      1114 => ActiveRecord::Type::DateTime.new,
+      16 => ActiveRecord::Type::Boolean.new
+    }
+  else
+    FTYPE_MAP = {
+      23 => :value_to_integer,
+      1114 => :string_to_time,
+      16 => :value_to_boolean
+    }
+  end
 
   def self.map_exec(klass, sql, args = {})
     self.new(sql).map_exec(klass, args)
@@ -93,7 +102,11 @@ class SqlBuilder
       setters.each_with_index do |mapper, index|
         translated = row[index]
         if mapper[1] && !translated.nil?
-          translated = ActiveRecord::ConnectionAdapters::Column.send mapper[1], translated
+          if rails_master?
+            translated = mapper[1].type_cast_from_database(translated)
+          else
+            translated = ActiveRecord::ConnectionAdapters::Column.send mapper[1], translated
+          end
         end
         mapped.send mapper[0], translated
       end

From 7351917ee400101fc13ff34c5db437c3ed416234 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Thu, 11 Sep 2014 11:05:29 -0700
Subject: [PATCH 04/12] Boolean -> String quoting has changed on Rails master

CustomField.create(name: 'zomg', value: true).reload.value # => 't' on Rails 4.1, '1' on 4.2

rails/rails@42be84ba
---
 app/models/concerns/has_custom_fields.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb
index cd9349539..cfe0db590 100644
--- a/app/models/concerns/has_custom_fields.rb
+++ b/app/models/concerns/has_custom_fields.rb
@@ -12,7 +12,7 @@ module HasCustomFields
       end
     end
 
-    CUSTOM_FIELD_TRUE = ['t','true', 'T', 'True', 'TRUE'].freeze unless defined? CUSTOM_FIELD_TRUE
+    CUSTOM_FIELD_TRUE = ['1', 't', 'true', 'T', 'True', 'TRUE'].freeze unless defined? CUSTOM_FIELD_TRUE
 
     def self.cast_custom_field(key, value, types)
       return value unless types && type = types[key]

From b1a0cd417dd5e82709fb8027b7c29de180152bfe Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Wed, 15 Oct 2014 00:04:47 -0700
Subject: [PATCH 05/12] Avoid a deprecation warning by poly-filling
 #deliver_now and #deliver_now

---
 config/application.rb                            |  5 +++++
 config/initializers/00-rails-master-polyfills.rb |  8 ++++++++
 lib/email/sender.rb                              |  2 +-
 script/test_email_settings.rb                    |  2 +-
 spec/components/email/sender_spec.rb             | 14 +++++++-------
 5 files changed, 22 insertions(+), 9 deletions(-)
 create mode 100644 config/initializers/00-rails-master-polyfills.rb

diff --git a/config/application.rb b/config/application.rb
index 3e0010aef..acc2d5017 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -110,6 +110,11 @@ module Discourse
     # see: http://stackoverflow.com/questions/11894180/how-does-one-correctly-add-custom-sql-dml-in-migrations/11894420#11894420
     config.active_record.schema_format = :sql
 
+    if rails_master?
+      # Opt-into the default behavior in Rails 5
+      # config.active_record.raise_in_transactional_callbacks = true
+    end
+
     # per https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
     config.pbkdf2_iterations = 64000
     config.pbkdf2_algorithm = "sha256"
diff --git a/config/initializers/00-rails-master-polyfills.rb b/config/initializers/00-rails-master-polyfills.rb
new file mode 100644
index 000000000..c32131a9a
--- /dev/null
+++ b/config/initializers/00-rails-master-polyfills.rb
@@ -0,0 +1,8 @@
+unless rails_master?
+
+class Mail::Message
+  alias_method :deliver_now,  :deliver
+  alias_method :deliver_now!, :deliver!
+end
+
+end
diff --git a/lib/email/sender.rb b/lib/email/sender.rb
index bc16b2bf9..8fcbd1c5f 100644
--- a/lib/email/sender.rb
+++ b/lib/email/sender.rb
@@ -115,7 +115,7 @@ module Email
       end
 
       begin
-        @message.deliver
+        @message.deliver_now
       rescue *SMTP_CLIENT_ERRORS => e
         return skip(e.message)
       end
diff --git a/script/test_email_settings.rb b/script/test_email_settings.rb
index fe7fdb54d..dc8422edd 100644
--- a/script/test_email_settings.rb
+++ b/script/test_email_settings.rb
@@ -38,7 +38,7 @@ end
 message = EmailTestMailer.email_test(MAILFROM, MAILTO)
 
 begin
-  message.deliver()
+  message.deliver_now()
 rescue SocketError => e
   print "Delivery failed: "+e.message.strip()+"\n"
   print " Is the server hostname correct?\n"
diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb
index 3853f82ca..f3b027d8e 100644
--- a/spec/components/email/sender_spec.rb
+++ b/spec/components/email/sender_spec.rb
@@ -5,25 +5,25 @@ describe Email::Sender do
 
   it "doesn't deliver mail when mails are disabled" do
     SiteSetting.expects(:disable_emails).returns(true)
-    Mail::Message.any_instance.expects(:deliver).never
+    Mail::Message.any_instance.expects(:deliver_now).never
     message = Mail::Message.new(to: "hello@world.com" , body: "hello")
     Email::Sender.new(message, :hello).send
   end
 
   it "doesn't deliver mail when the message is nil" do
-    Mail::Message.any_instance.expects(:deliver).never
+    Mail::Message.any_instance.expects(:deliver_now).never
     Email::Sender.new(nil, :hello).send
   end
 
   it "doesn't deliver when the to address is nil" do
     message = Mail::Message.new(body: 'hello')
-    message.expects(:deliver).never
+    message.expects(:deliver_now).never
     Email::Sender.new(message, :hello).send
   end
 
   it "doesn't deliver when the body is nil" do
     message = Mail::Message.new(to: 'eviltrout@test.domain')
-    message.expects(:deliver).never
+    message.expects(:deliver_now).never
     Email::Sender.new(message, :hello).send
   end
 
@@ -53,14 +53,14 @@ describe Email::Sender do
     let(:message) do
       message = Mail::Message.new to: 'eviltrout@test.domain',
                                   body: '**hello**'
-      message.stubs(:deliver)
+      message.stubs(:deliver_now)
       message
     end
 
     let(:email_sender) { Email::Sender.new(message, :valid_type) }
 
     it 'calls deliver' do
-      message.expects(:deliver).once
+      message.expects(:deliver_now).once
       email_sender.send
     end
 
@@ -131,7 +131,7 @@ describe Email::Sender do
   context 'with a user' do
     let(:message) do
       message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body'
-      message.stubs(:deliver)
+      message.stubs(:deliver_now)
       message
     end
 

From 568ed3beaac0165f2025a799c55dfb31fe9ec5ee Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Wed, 15 Oct 2014 00:14:32 -0700
Subject: [PATCH 06/12] HAX: force the lazy `MessageDelivery` object to create
 the mailer

Starting from Rails 4.2, calling MyMailer.some_method no longer result in an
immediate call to MyMailer#some_method. Instead, a "lazy proxy" is returned
(this is changed to support #deliver_later). As a quick hack to fix the test,
calling #message (or anything, really) would force the Mailer object to be
created and the method invoked.
---
 spec/mailers/user_notifications_spec.rb | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index cdde34812..f1b3728ad 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -183,7 +183,16 @@ describe UserNotifications do
 
   def expects_build_with(condition)
     UserNotifications.any_instance.expects(:build_email).with(user.email, condition)
-    UserNotifications.send(mail_type, user, notification: notification, post: notification.post)
+    mailer = UserNotifications.send(mail_type, user, notification: notification, post: notification.post)
+
+    if rails_master?
+      # Starting from Rails 4.2, calling MyMailer.some_method no longer result
+      # in an immediate call to MyMailer#some_method. Instead, a "lazy proxy" is
+      # returned (this is changed to support #deliver_later). As a quick hack to
+      # fix the test, calling #message (or anything, really) would force the
+      # Mailer object to be created and the method invoked.
+      mailer.message
+    end
   end
 
   shared_examples "supports reply by email" do

From f749961d9717efb5a055bfee23ae9dd1e8aaaa13 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Wed, 15 Oct 2014 01:23:29 -0700
Subject: [PATCH 07/12] `Sprockets::Context` takes a `Pathname` for its third
 argument

As far as I can tell, sprockets has always expected the third argument to be a
`Pathname` object. Not sure what actually changed and why it was working before,
but it now throws a `NoMethodError` for `dirname` on `String` in
`Context#resolve`

    # , not sure why this was working before
---
 lib/sass/discourse_sass_compiler.rb | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/sass/discourse_sass_compiler.rb b/lib/sass/discourse_sass_compiler.rb
index ec8d729fc..ebb799ecc 100644
--- a/lib/sass/discourse_sass_compiler.rb
+++ b/lib/sass/discourse_sass_compiler.rb
@@ -1,4 +1,5 @@
 require_dependency 'sass/discourse_sass_importer'
+require 'pathname'
 
 class DiscourseSassCompiler
 
@@ -41,7 +42,8 @@ class DiscourseSassCompiler
       env = env.instance_variable_get('@environment')
     end
 
-    context = env.context_class.new(env, "#{@target}.scss", "app/assets/stylesheets/#{@target}.scss")
+    pathname = Pathname.new("app/assets/stylesheets/#{@target}.scss")
+    context = env.context_class.new(env, "#{@target}.scss", pathname)
 
     debug_opts = Rails.env.production? ? {} : {
       line_numbers: true,

From 5d7aca3cc6cd5956d422be92c99946cd9828856c Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Wed, 15 Oct 2014 01:56:41 -0700
Subject: [PATCH 08/12] Use *_url instead of *_path to avoid a deprecation
 warning

---
 app/views/user_notifications/digest.html.erb | 2 +-
 app/views/user_notifications/digest.text.erb | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/app/views/user_notifications/digest.html.erb b/app/views/user_notifications/digest.html.erb
index 9cd965230..9c7c64d3f 100644
--- a/app/views/user_notifications/digest.html.erb
+++ b/app/views/user_notifications/digest.html.erb
@@ -70,7 +70,7 @@
   <span class='footer-notice'>
     <%=raw(t :'user_notifications.digest.unsubscribe',
        site_link: html_site_link,
-       unsubscribe_link: link_to(t('user_notifications.digest.click_here'), email_unsubscribe_path(host: Discourse.base_url, key: @user.temporary_key, only_path: false)))  %>
+       unsubscribe_link: link_to(t('user_notifications.digest.click_here'), email_unsubscribe_url(host: Discourse.base_url, key: @user.temporary_key)))  %>
   </span>
   </td>
 </tr>
diff --git a/app/views/user_notifications/digest.text.erb b/app/views/user_notifications/digest.text.erb
index b0735a373..8c86148aa 100644
--- a/app/views/user_notifications/digest.text.erb
+++ b/app/views/user_notifications/digest.text.erb
@@ -33,7 +33,6 @@
 
 <%=raw(t :'user_notifications.digest.unsubscribe',
      site_link: site_link,
-     unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), email_unsubscribe_path(key: @user.temporary_key))))  %>
+     unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), email_unsubscribe_url(key: @user.temporary_key, only_path: true))))  %>
 
 <%= raw(@markdown_linker.references) %>
-

From bddaf8193aa473fd127dd500f82f06399d31c62a Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Wed, 15 Oct 2014 02:09:56 -0700
Subject: [PATCH 09/12] HAX: check the `message` object, not the
 `MessageDelivery` object

See 669bf73 for background. It's probably better to rewrite these test without
using the internal `NullMail` class anyway.
---
 spec/mailers/user_notifications_spec.rb | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index f1b3728ad..a8ddc4246 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -112,14 +112,21 @@ describe UserNotifications do
       # in mailing list mode user_replies is not sent through
       response.user.mailing_list_mode = true
       mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
-      mail.class.should == ActionMailer::Base::NullMail
 
+      if rails_master?
+        mail.message.class.should == ActionMailer::Base::NullMail
+      else
+        mail.class.should == ActionMailer::Base::NullMail
+      end
 
       response.user.mailing_list_mode = nil
       mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
 
-      mail.class.should_not == ActionMailer::Base::NullMail
-
+      if rails_master?
+        mail.message.class.should_not == ActionMailer::Base::NullMail
+      else
+        mail.class.should_not == ActionMailer::Base::NullMail
+      end
     end
   end
 

From 4d188ca5b0a0c26d60bd196511affa5d1cf6a582 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Sat, 25 Oct 2014 02:37:57 -0700
Subject: [PATCH 10/12] Added back the `last_use` timestamp to the
 `AbstractAdapter`

This was removed in https://github.com/rails/rails/commit/9e457a8654fa89fe329719f88ae3679aefb21e56
---
 lib/freedom_patches/pool_drainer.rb | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/freedom_patches/pool_drainer.rb b/lib/freedom_patches/pool_drainer.rb
index f953b32a4..f6e2498a5 100644
--- a/lib/freedom_patches/pool_drainer.rb
+++ b/lib/freedom_patches/pool_drainer.rb
@@ -1,6 +1,29 @@
 
+if rails_master?
+  class ActiveRecord::ConnectionAdapters::AbstractAdapter
+    module LastUseExtension
+      attr_reader :last_use
+
+      def initialize(connection, logger = nil, pool = nil)
+        super
+        @last_use = false
+      end
+
+      def lease
+        synchronize do
+          unless in_use?
+            @last_use = Time.now
+            super
+          end
+        end
+      end
+    end
+
+    prepend LastUseExtension
+  end
+end
+
 class ActiveRecord::ConnectionAdapters::ConnectionPool
-
   # drain all idle connections
   # if idle_time is specified only connections idle for N seconds will be drained
   def drain(idle_time=nil)
@@ -29,5 +52,4 @@ class ActiveRecord::ConnectionAdapters::ConnectionPool
 
     false
   end
-
 end

From b4679075dee702c012860f25fb3328f5bdc49719 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Sat, 25 Oct 2014 03:08:40 -0700
Subject: [PATCH 11/12] Upgrade to the latest AMS 0.8.x (unreleased)

The current released version (0.8.2) does not work with Rails master at all. In
fact, it was quite surprising to me that this is the only test that broke...

See https://github.com/rails-api/active_model_serializers/pull/655
---
 Gemfile             |  6 +++++-
 Gemfile_master.lock | 12 +++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/Gemfile b/Gemfile
index b6dfd8fb1..bec5dccf9 100644
--- a/Gemfile
+++ b/Gemfile
@@ -91,7 +91,11 @@ gem 'redis', require:  ["redis", "redis/connection/hiredis"]
 # We use some ams 0.8.0 features, need to amend code
 # to support 0.9 etc, bench needs to run and ensure no
 # perf regressions
-gem 'active_model_serializers', '~> 0.8.0'
+if rails_master?
+  gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-8-stable'
+else
+  gem 'active_model_serializers', '~> 0.8.0'
+end
 
 
 gem 'onebox'
diff --git a/Gemfile_master.lock b/Gemfile_master.lock
index c0e27f439..d3fce3147 100644
--- a/Gemfile_master.lock
+++ b/Gemfile_master.lock
@@ -1,3 +1,11 @@
+GIT
+  remote: git://github.com/rails-api/active_model_serializers.git
+  revision: b6b01d0b7396f3deaa6e661cedf4bc5efe2f4525
+  branch: 0-8-stable
+  specs:
+    active_model_serializers (0.8.2)
+      activemodel (>= 3.0)
+
 GIT
   remote: https://github.com/rails/rails.git
   revision: aa6637d140c2ebd28bbd23fc250af033a065dbe8
@@ -74,8 +82,6 @@ GEM
   specs:
     actionpack-action_caching (1.1.1)
       actionpack (>= 4.0.0, < 5.0)
-    active_model_serializers (0.8.2)
-      activemodel (>= 3.0)
     addressable (2.3.6)
     annotate (2.6.5)
       activerecord (>= 2.3.0)
@@ -437,7 +443,7 @@ PLATFORMS
 
 DEPENDENCIES
   actionpack-action_caching
-  active_model_serializers (~> 0.8.0)
+  active_model_serializers!
   annotate
   barber
   better_errors

From 5cc9f5bff574a4588b1b569b272626d9c3d31559 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Tue, 28 Oct 2014 15:48:57 -0700
Subject: [PATCH 12/12] Add RAILS_MASTER to the travis build matrix

---
 .travis.yml | 21 ++++++++++++++++-----
 Gemfile     |  2 +-
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index fcc03d093..46bfbd465 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,21 @@
 language: ruby
 
+env:
+  global:
+    - DISCOURSE_HOSTNAME=www.example.com
+    - RUBY_GC_MALLOC_LIMIT=50000000
+  matrix:
+    - "RAILS_MASTER=1"
+    - "RAILS_MASTER=0"
+
+matrix:
+  allow_failures:
+    - rvm: 2.0.0
+      env: "RAILS_MASTER=1"
+    - rvm: 2.1
+      env: "RAILS_MASTER=1"
+  fast_finish: true
+
 rvm:
   - 2.0.0
   - 2.1
@@ -11,11 +27,6 @@ sudo: false
 
 cache: bundler
 
-env:
-  global:
-    - DISCOURSE_HOSTNAME=www.example.com
-    - RUBY_GC_MALLOC_LIMIT=50000000
-
 before_install:
   - npm i -g jshint
   - jshint .
diff --git a/Gemfile b/Gemfile
index bec5dccf9..8f64452f8 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,7 +4,7 @@ source 'https://rubygems.org'
 
 module ::Kernel
   def rails_master?
-    ENV["RAILS_MASTER"]
+    ENV["RAILS_MASTER"] == '1'
   end
 end