DEV: Add support for Rails 6

DEV: Add support for Rails 6

Minor fixes to add Rails 6 support to Discourse, we now will boot with RAILS_MASTER=1, all specs pass

Only one tiny deprecation left

Largest change was the way ActiveModel:Errors changed interface a bit but there is a simple backwards compat way of working it

diff --git a/app/controllers/extra_locales_controller.rb b/app/controllers/extra_locales_controller.rb
index 72e1cd3..dfcc647 100644
--- a/app/controllers/extra_locales_controller.rb
+++ b/app/controllers/extra_locales_controller.rb
@@ -3,7 +3,11 @@
 class ExtraLocalesController < ApplicationController
 
   layout :false
-  skip_before_action :check_xhr, :preload_json, :redirect_to_login_if_required
+
+  skip_before_action :check_xhr,
+    :preload_json,
+    :redirect_to_login_if_required,
+    :verify_authenticity_token
 
   def show
     bundle = params[:bundle]
diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb
index a3af31c..1e8ca6b 100644
--- a/app/controllers/uploads_controller.rb
+++ b/app/controllers/uploads_controller.rb
@@ -152,7 +152,7 @@ class UploadsController < ApplicationController
       upload.update_columns(retain_hours: retain_hours) if retain_hours > 0
     end
 
-    upload.errors.empty? ? upload : { errors: upload.errors.values.flatten }
+    upload.errors.empty? ? upload : { errors: upload.errors.to_hash.values.flatten }
   ensure
     tempfile&.close!
   end
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 523ab0c..53392cc 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -584,7 +584,7 @@ class UserNotifications < ActionMailer::Base
       )
 
       unless translation_override_exists
-        html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
+        html = UserNotificationRenderer.with_view_paths(Rails.configuration.paths["app/views"]).render(
           template: 'email/invite',
           format: :html,
           locals: { message: PrettyText.cook(message, sanitize: false).html_safe,
@@ -611,7 +611,8 @@ class UserNotifications < ActionMailer::Base
       end
 
       unless translation_override_exists
-        html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
+
+        html = UserNotificationRenderer.with_view_paths(Rails.configuration.paths["app/views"]).render(
           template: 'email/notification',
           format: :html,
           locals: { context_posts: context_posts,
diff --git a/app/models/group.rb b/app/models/group.rb
index 82ceee1..bb0adf7 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -99,7 +99,7 @@ class Group < ActiveRecord::Base
   validates :messageable_level, inclusion: { in: ALIAS_LEVELS.values }
 
   scope :visible_groups, Proc.new { |user, order, opts|
-    groups = Group.order(order || "name ASC")
+    groups = self.order(order || "name ASC")
 
     if !opts || !opts[:include_everyone]
       groups = groups.where("groups.id > 0")
diff --git a/app/models/user.rb b/app/models/user.rb
index 1c6d9ad..7245e80 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -988,7 +988,7 @@ class User < ActiveRecord::Base
   end
 
   def secure_category_ids
-    cats = self.admin? ? Category.where(read_restricted: true) : secure_categories.references(:categories)
+    cats = self.admin? ? Category.unscoped.where(read_restricted: true) : secure_categories.references(:categories)
     cats.pluck('categories.id').sort
   end
 
diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb
index 12334ba..02bbb01 100644
--- a/app/views/layouts/_head.html.erb
+++ b/app/views/layouts/_head.html.erb
@@ -5,7 +5,7 @@
 <%- end %>
 <% site_apple_touch_icon_url = SiteSetting.site_apple_touch_icon_url %>
 <%- if site_apple_touch_icon_url.present? %>
-<link rel="apple-touch-icon" type="image/png" href="<%= UrlHelper.absolute(site_apple_touch_icon_url) %>">
+<link rel="apple-touch-icon" type="image/png" href="<%= ::UrlHelper.absolute(site_apple_touch_icon_url) %>">
 <%- end %>
 <meta name="theme-color" content="#<%= ColorScheme.hex_for_name('header_background', scheme_id) %>">
 <% if mobile_view? %>
diff --git a/app/views/metadata/opensearch.xml.erb b/app/views/metadata/opensearch.xml.erb
index 5495c3f..1ce0ae4 100644
--- a/app/views/metadata/opensearch.xml.erb
+++ b/app/views/metadata/opensearch.xml.erb
@@ -4,7 +4,7 @@
   <Description>Search for posts on <%= SiteSetting.title %></Description>
   <Tags>discourse forum</Tags>
   <% site_favicon_url = SiteSetting.site_favicon_url %>
-  <% absolute_site_favicon_url = UrlHelper.absolute(site_favicon_url) %>
+  <% absolute_site_favicon_url = ::UrlHelper.absolute(site_favicon_url) %>
   <% if site_favicon_url =~ /\.ico$/ -%>
     <Image height="16" width="16" type="image/vnd.microsoft.icon"><%= absolute_site_favicon_url %></Image>
   <%- else -%>
diff --git a/app/views/users/password_reset.html.erb b/app/views/users/password_reset.html.erb
index 3a3c433..8a982ae 100644
--- a/app/views/users/password_reset.html.erb
+++ b/app/views/users/password_reset.html.erb
@@ -4,7 +4,7 @@
       <%= @error %>
     </div>
   <%end%>
-  <% if @user.present? and @user.errors.any? %>
+  <% if @user.present? and @user.errors.present? %>
     <div class='alert alert-error'>
       <% @user.errors.full_messages.each do |msg| %>
         <li><%= msg %></li>
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index 4ddc887..0ef5fba 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -27,13 +27,13 @@ module DiscourseTagging
         staff_tags = new_tag_names & all_staff_tags
         staff_tags += new_tag_names & hidden_tags
         if staff_tags.present?
-          topic.errors[:base] << I18n.t("tags.staff_tag_disallowed", tag: staff_tags.join(" "))
+          topic.errors.add(:base, I18n.t("tags.staff_tag_disallowed", tag: staff_tags.join(" ")))
           return false
         end
 
         staff_tags = removed_tag_names & all_staff_tags
         if staff_tags.present?
-          topic.errors[:base] << I18n.t("tags.staff_tag_remove_disallowed", tag: staff_tags.join(" "))
+          topic.errors.add(:base, I18n.t("tags.staff_tag_remove_disallowed", tag: staff_tags.join(" ")))
           return false
         end
 
@@ -77,7 +77,7 @@ module DiscourseTagging
 
         # validate minimum required tags for a category
         if !guardian.is_staff? && category && category.minimum_required_tags > 0 && tags.length < category.minimum_required_tags
-          topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
+          topic.errors.add(:base, I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
           return false
         end
 
@@ -85,7 +85,7 @@ module DiscourseTagging
       else
         # validate minimum required tags for a category
         if !guardian.is_staff? && category && category.minimum_required_tags > 0
-          topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
+          topic.errors.add(:base, I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
           return false
         end
 
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 669f70d..68a5fa8 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -1112,7 +1112,9 @@ module Email
         raise TooShortPost
       end
 
-      raise InvalidPost, errors.join("\n") if result.errors.any?
+      if result.errors.present?
+        raise InvalidPost, errors.join("\n")
+      end
 
       if result.post
         @incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id)
diff --git a/lib/freedom_patches/active_record_attribute_methods.rb b/lib/freedom_patches/active_record_attribute_methods.rb
index 71fd5d4..2271061 100644
--- a/lib/freedom_patches/active_record_attribute_methods.rb
+++ b/lib/freedom_patches/active_record_attribute_methods.rb
@@ -10,9 +10,19 @@
 module ActiveRecord
   module AttributeMethods
     module ClassMethods
+      # this is for Rails 5
       def enforce_raw_sql_whitelist(*args)
         return
       end
+
+      BLANK_ARRAY = [].freeze
+

[... diff too long, it was truncated ...]

GitHub sha: 1be01f8d

2 Likes