From 04016f0decfc37648ff2b9a9db94460aff0ea56b Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sat, 15 Apr 2017 12:11:02 +0800 Subject: [PATCH] Support Ruby 2.4. --- .travis.yml | 1 + Gemfile | 1 + Gemfile.lock | 12 ++++++++++ .../javascripts/discourse/templates/user.hbs | 4 ++-- app/controllers/application_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- app/models/category.rb | 4 ++-- app/models/category_featured_user.rb | 2 +- app/models/draft_sequence.rb | 4 ++-- app/models/plugin_store.rb | 2 +- app/models/topic.rb | 2 +- app/models/user.rb | 10 +++------ app/models/{warning.rb => user_warning.rb} | 2 +- db/migrate/20170413043152_rename_warnings.rb | 9 ++++++++ lib/pretty_text/helpers.rb | 3 +-- lib/site_setting_extension.rb | 22 +++++++++---------- lib/topic_creator.rb | 2 +- spec/components/email/receiver_spec.rb | 2 ++ spec/components/email_cook_spec.rb | 1 + .../engine/discourse_local_onebox_spec.rb | 2 +- spec/components/oneboxer_spec.rb | 3 ++- spec/components/post_creator_spec.rb | 12 +++++----- .../components/site_setting_extension_spec.rb | 12 +--------- spec/controllers/uploads_controller_spec.rb | 4 ++-- .../user_avatars_controller_spec.rb | 2 +- spec/jobs/pull_hotlinked_images_spec.rb | 3 ++- spec/jobs/update_gravatar_spec.rb | 2 +- spec/models/user_spec.rb | 1 + spec/rails_helper.rb | 5 +---- 29 files changed, 73 insertions(+), 60 deletions(-) rename app/models/{warning.rb => user_warning.rb} (92%) create mode 100644 db/migrate/20170413043152_rename_warnings.rb diff --git a/.travis.yml b/.travis.yml index 465c6366dfd..b984dfd2f40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,6 +24,7 @@ matrix: fast_finish: true rvm: + - 2.4.1 - 2.3.3 services: diff --git a/Gemfile b/Gemfile index 0ae1f8ff4c1..faa614e12d2 100644 --- a/Gemfile +++ b/Gemfile @@ -112,6 +112,7 @@ group :assets do end group :test do + gem 'webmock', require: false gem 'fakeweb', '~> 1.3.0', require: false gem 'minitest', require: false gem 'timecop' diff --git a/Gemfile.lock b/Gemfile.lock index 0222599a113..64a64c3dc61 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,8 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.5.1) + public_suffix (~> 2.0, >= 2.0.2) annotate (2.7.1) activerecord (>= 3.2, < 6.0) rake (>= 10.4, < 12.0) @@ -69,6 +71,8 @@ GEM coderay (1.1.1) concurrent-ruby (1.0.5) connection_pool (2.2.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) crass (1.0.2) debug_inspector (0.0.2) diff-lcs (1.2.5) @@ -113,6 +117,7 @@ GEM globalid (0.3.7) activesupport (>= 4.1.0) guess_html_encoding (0.0.11) + hashdiff (0.3.2) hashie (3.5.5) highline (1.7.8) hiredis (0.6.1) @@ -228,6 +233,7 @@ GEM pry (>= 0.9.10, < 0.11.0) pry-rails (0.3.4) pry (>= 0.9.10) + public_suffix (2.0.5) puma (3.6.0) r2 (0.2.6) rack (1.6.5) @@ -316,6 +322,7 @@ GEM guess_html_encoding (>= 0.0.4) nokogiri (>= 1.6.0) ruby_dep (1.5.0) + safe_yaml (1.0.4) sanitize (4.4.0) crass (~> 1.0.2) nokogiri (>= 1.4.4) @@ -377,6 +384,10 @@ GEM kgio (~> 2.6) raindrops (~> 0.7) uniform_notifier (1.10.0) + webmock (3.0.1) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff PLATFORMS ruby @@ -477,6 +488,7 @@ DEPENDENCIES uglifier unf unicorn + webmock BUNDLED WITH 1.14.6 diff --git a/app/assets/javascripts/discourse/templates/user.hbs b/app/assets/javascripts/discourse/templates/user.hbs index 13f1c6b222c..7970c3f9a00 100644 --- a/app/assets/javascripts/discourse/templates/user.hbs +++ b/app/assets/javascripts/discourse/templates/user.hbs @@ -24,8 +24,8 @@ {{#if model.number_of_suspensions}}
{{model.number_of_suspensions}} {{i18n 'user.staff_counters.suspensions'}}
{{/if}} - {{#if model.number_of_warnings}} -
{{model.number_of_warnings}} {{i18n 'user.staff_counters.warnings_received'}}
+ {{#if model.warnings_received_count}} +
{{model.warnings_received_count}} {{i18n 'user.staff_counters.warnings_received'}}
{{/if}} {{/unless}} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9f864c1be3e..7d3341fc560 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -465,7 +465,7 @@ class ApplicationController < ActionController::Base # type - a machine-readable description of the error # status - HTTP status code to return def render_json_error(obj, opts={}) - opts = { status: opts } if opts.is_a?(Fixnum) + opts = { status: opts } if opts.is_a?(Integer) render json: MultiJson.dump(create_errors_json(obj, opts[:type])), status: opts[:status] || 422 end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a9eaa127e11..d630da7442a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -744,7 +744,7 @@ class UsersController < ApplicationController result = {} - %W{number_of_deleted_posts number_of_flagged_posts number_of_flags_given number_of_suspensions number_of_warnings}.each do |info| + %W{number_of_deleted_posts number_of_flagged_posts number_of_flags_given number_of_suspensions warnings_received_count}.each do |info| result[info] = @user.send(info) end diff --git a/app/models/category.rb b/app/models/category.rb index a533a3863fa..0a50c69d7d3 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -388,8 +388,8 @@ SQL group = group.id if group.is_a?(Group) # subtle, using Group[] ensures the group exists in the DB - group = Group[group.to_sym].id unless group.is_a?(Fixnum) - permission = CategoryGroup.permission_types[permission] unless permission.is_a?(Fixnum) + group = Group[group.to_sym].id unless group.is_a?(Integer) + permission = CategoryGroup.permission_types[permission] unless permission.is_a?(Integer) [group, permission] end diff --git a/app/models/category_featured_user.rb b/app/models/category_featured_user.rb index db524db0b89..fb75cca87b5 100644 --- a/app/models/category_featured_user.rb +++ b/app/models/category_featured_user.rb @@ -8,7 +8,7 @@ class CategoryFeaturedUser < ActiveRecord::Base def self.feature_users_in(category_or_category_id) category_id = - if category_or_category_id.is_a?(Fixnum) + if category_or_category_id.is_a?(Integer) category_or_category_id else category_or_category_id.id diff --git a/app/models/draft_sequence.rb b/app/models/draft_sequence.rb index 0e1c44359ef..af4eb647c93 100644 --- a/app/models/draft_sequence.rb +++ b/app/models/draft_sequence.rb @@ -1,7 +1,7 @@ class DraftSequence < ActiveRecord::Base def self.next!(user,key) user_id = user - user_id = user.id unless user.class == Fixnum + user_id = user.id unless user.is_a?(Integer) return 0 if user_id < 0 @@ -19,7 +19,7 @@ class DraftSequence < ActiveRecord::Base return nil unless user user_id = user - user_id = user.id unless user.class == Fixnum + user_id = user.id unless user.is_a?(Integer) # perf critical path r = exec_sql('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key).values diff --git a/app/models/plugin_store.rb b/app/models/plugin_store.rb index ecf8d9c73e3..5a7675344ec 100644 --- a/app/models/plugin_store.rb +++ b/app/models/plugin_store.rb @@ -42,7 +42,7 @@ class PluginStore def self.cast_value(type, value) case type - when "Fixnum" then value.to_i + when "Integer", "Fixnum" then value.to_i when "TrueClass", "FalseClass" then value == "true" when "JSON" then map_json(::JSON.parse(value)) else value diff --git a/app/models/topic.rb b/app/models/topic.rb index ed3844c121a..604c491284a 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -118,7 +118,7 @@ class Topic < ActiveRecord::Base has_many :invites, through: :topic_invites, source: :invite has_many :topic_status_updates, dependent: :destroy - has_one :warning + has_one :user_warning has_one :first_post, -> {where post_number: 1}, class_name: Post # When we want to temporarily attach some data to a forum topic (usually before serialization) diff --git a/app/models/user.rb b/app/models/user.rb index 3c41e4c1e28..d47f6749cad 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,7 +37,7 @@ class User < ActiveRecord::Base has_many :invites, dependent: :destroy has_many :topic_links, dependent: :destroy has_many :uploads - has_many :warnings + has_many :user_warnings has_many :user_archived_messages, dependent: :destroy has_many :email_change_requests, dependent: :destroy has_many :directory_items, dependent: :delete_all @@ -276,7 +276,7 @@ class User < ActiveRecord::Base def approve(approved_by, send_mail=true) self.approved = true - if approved_by.is_a?(Fixnum) + if approved_by.is_a?(Integer) self.approved_by_id = approved_by else self.approved_by = approved_by @@ -614,7 +614,7 @@ class User < ActiveRecord::Base end def warnings_received_count - warnings.count + user_warnings.count end def flags_received_count @@ -889,10 +889,6 @@ class User < ActiveRecord::Base .count end - def number_of_warnings - self.warnings.count - end - def number_of_suspensions UserHistory.for(self, :suspend_user).count end diff --git a/app/models/warning.rb b/app/models/user_warning.rb similarity index 92% rename from app/models/warning.rb rename to app/models/user_warning.rb index 0d177fb0c66..6bc201a7bc8 100644 --- a/app/models/warning.rb +++ b/app/models/user_warning.rb @@ -1,4 +1,4 @@ -class Warning < ActiveRecord::Base +class UserWarning < ActiveRecord::Base belongs_to :user belongs_to :topic belongs_to :created_by, class_name: 'User' diff --git a/db/migrate/20170413043152_rename_warnings.rb b/db/migrate/20170413043152_rename_warnings.rb new file mode 100644 index 00000000000..d4397726be8 --- /dev/null +++ b/db/migrate/20170413043152_rename_warnings.rb @@ -0,0 +1,9 @@ +class RenameWarnings < ActiveRecord::Migration + def up + rename_table :warnings, :user_warnings + end + + def down + rename_table :user_warnings, :warnings + end +end diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb index 1c0c2f0e1a1..78378c5cbf7 100644 --- a/lib/pretty_text/helpers.rb +++ b/lib/pretty_text/helpers.rb @@ -45,7 +45,7 @@ module PrettyText end def get_topic_info(topic_id) - return unless Fixnum === topic_id + return unless topic_id.is_a?(Integer) # TODO this only handles public topics, secured one do not get this topic = Topic.find_by(id: topic_id) if topic && Guardian.new.can_see?(topic) @@ -76,4 +76,3 @@ module PrettyText end end end - diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 70e576f08de..20dd2818afc 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -23,7 +23,7 @@ module SiteSettingExtension def types @types ||= Enum.new(string: 1, time: 2, - fixnum: 3, + integer: 3, float: 4, bool: 5, null: 6, @@ -286,7 +286,7 @@ module SiteSettingExtension val = (val == "t" || val == "true") ? 't' : 'f' end - if type == types[:fixnum] && !val.is_a?(Fixnum) + if type == types[:integer] && !val.is_a?(Integer) val = val.to_i end @@ -295,7 +295,7 @@ module SiteSettingExtension end if type == types[:enum] - val = val.to_i if defaults[name.to_sym].is_a?(Fixnum) + val = val.to_i if defaults[name.to_sym].is_a?(Integer) if enum_class(name) raise Discourse::InvalidParameters.new(:value) unless enum_class(name).valid_value?(val) else @@ -340,9 +340,9 @@ module SiteSettingExtension valid = true type = get_data_type(name, defaults[name.to_sym]) - if type == types[:fixnum] - # validate fixnum - valid = false unless value.to_i.is_a?(Fixnum) + if type == types[:integer] + # validate integer + valid = false unless value.to_i.is_a?(Integer) end valid @@ -407,8 +407,8 @@ module SiteSettingExtension case val when String types[:string] - when Fixnum - types[:fixnum] + when Integer + types[:integer] when Float types[:float] when TrueClass, FalseClass @@ -422,14 +422,14 @@ module SiteSettingExtension case type when types[:float] value.to_f - when types[:fixnum] + when types[:integer] value.to_i when types[:bool] value == true || value == "t" || value == "true" when types[:null] nil when types[:enum] - defaults[name.to_sym].is_a?(Fixnum) ? value.to_i : value + defaults[name.to_sym].is_a?(Integer) ? value.to_i : value else return value if types[type] # Otherwise it's a type error @@ -441,7 +441,7 @@ module SiteSettingExtension @validator_mapping ||= { 'email' => EmailSettingValidator, 'username' => UsernameSettingValidator, - types[:fixnum] => IntegerSettingValidator, + types[:integer] => IntegerSettingValidator, types[:string] => StringSettingValidator, 'list' => StringSettingValidator, 'enum' => StringSettingValidator, diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 8c0621d507c..ecd35fd116b 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -64,7 +64,7 @@ class TopicCreator rollback_with!(topic, :too_many_users) if @added_users.size != 1 # Create a warning record - Warning.create(topic: topic, user: @added_users.first, created_by: @user) + UserWarning.create(topic: topic, user: @added_users.first, created_by: @user) end def watch_topic(topic) diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 3019c5a780f..5f0ea2fcf63 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -297,6 +297,8 @@ describe Email::Receiver do end it "supports attached images" do + SiteSetting.queue_jobs = true + expect { process(:no_body_with_image) }.to change { topic.posts.count } expect(topic.posts.last.raw).to match(/https://www.eviltrout.com
") end diff --git a/spec/components/onebox/engine/discourse_local_onebox_spec.rb b/spec/components/onebox/engine/discourse_local_onebox_spec.rb index 752eebc6e83..089c07a68bb 100644 --- a/spec/components/onebox/engine/discourse_local_onebox_spec.rb +++ b/spec/components/onebox/engine/discourse_local_onebox_spec.rb @@ -71,7 +71,7 @@ describe Onebox::Engine::DiscourseLocalOnebox do it "returns nil if file type is not audio or video" do url = "#{Discourse.base_url}#{path}.pdf" - FakeWeb.register_uri(:get, url, body: "") + stub_request(:get, url).to_return(body: '') expect(Onebox.preview(url).to_s).to eq("") end diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index 1df8b30027f..a01449f21ee 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -4,9 +4,10 @@ require_dependency 'oneboxer' describe Oneboxer do it "returns blank string for an invalid onebox" do + stub_request(:get, "http://boom.com").to_return(body: "") + expect(Oneboxer.preview("http://boom.com")).to eq("") expect(Oneboxer.onebox("http://boom.com")).to eq("") end end - diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 9c9f915f524..d2ee5299cf0 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -255,6 +255,8 @@ describe PostCreator do it 'creates a post with featured link' do SiteSetting.topic_featured_link_enabled = true SiteSetting.min_first_post_length = 100 + SiteSetting.queue_jobs = true + post = creator_with_featured_link.create expect(post.topic.featured_link).to eq('http://www.discourse.org') expect(post.valid?).to eq(true) @@ -585,7 +587,7 @@ describe PostCreator do it 'acts correctly' do # It's not a warning - expect(post.topic.warning).to be_blank + expect(post.topic.user_warning).to be_blank expect(post.topic.archetype).to eq(Archetype.private_message) expect(post.topic.subtype).to eq(TopicSubtype.user_to_user) @@ -657,11 +659,11 @@ describe PostCreator do topic = post.topic expect(topic).to be_present - expect(topic.warning).to be_present + expect(topic.user_warning).to be_present expect(topic.subtype).to eq(TopicSubtype.moderator_warning) - expect(topic.warning.user).to eq(target_user1) - expect(topic.warning.created_by).to eq(user) - expect(target_user1.warnings.count).to eq(1) + expect(topic.user_warning.user).to eq(target_user1) + expect(topic.user_warning.created_by).to eq(user) + expect(target_user1.user_warnings.count).to eq(1) end end diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rb index e981f4fbf72..353c04bd4cd 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -58,7 +58,7 @@ describe SiteSettingExtension do settings.hello = 100 expect(settings.hello).to eq(100) - settings.provider.save(:hello, 99, SiteSetting.types[:fixnum] ) + settings.provider.save(:hello, 99, SiteSetting.types[:integer] ) settings.refresh! expect(settings.hello).to eq(99) @@ -386,16 +386,6 @@ describe SiteSettingExtension do end end - describe "set for an invalid fixnum value" do - it "raises an error" do - settings.setting(:test_setting, 80) - settings.refresh! - expect { - settings.set("test_setting", 9999999999999999999) - }.to raise_error(ArgumentError) - end - end - describe "filter domain name" do before do settings.setting(:white_listed_spam_host_domains, "www.example.com") diff --git a/spec/controllers/uploads_controller_spec.rb b/spec/controllers/uploads_controller_spec.rb index 3dd88514a07..dc1b11c6034 100644 --- a/spec/controllers/uploads_controller_spec.rb +++ b/spec/controllers/uploads_controller_spec.rb @@ -75,12 +75,12 @@ describe UploadsController do end it 'is successful with synchronous api' do - SiteSetting.stubs(:authorized_extensions).returns("*") + SiteSetting.authorized_extensions = "*" controller.stubs(:is_api?).returns(true) Jobs.expects(:enqueue).with(:create_thumbnails, anything) - FakeWeb.register_uri(:get, "http://example.com/image.png", :body => File.read('spec/fixtures/images/logo.png')) + stub_request(:get, "http://example.com/image.png").to_return(body: File.read('spec/fixtures/images/logo.png')) xhr :post, :create, url: 'http://example.com/image.png', type: "avatar", synchronous: true diff --git a/spec/controllers/user_avatars_controller_spec.rb b/spec/controllers/user_avatars_controller_spec.rb index d09bc36568f..603a60a441e 100644 --- a/spec/controllers/user_avatars_controller_spec.rb +++ b/spec/controllers/user_avatars_controller_spec.rb @@ -24,7 +24,7 @@ describe UserAvatarsController do SiteSetting.s3_upload_bucket = "test" SiteSetting.s3_cdn_url = "http://cdn.com" - FakeWeb.register_uri(:get, "http://cdn.com/something/else", :body => 'image') + stub_request(:get, "http://cdn.com/something/else").to_return(body: 'image') GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom") diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index aa9473a4783..bdae4fd3d4d 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -5,8 +5,9 @@ describe Jobs::PullHotlinkedImages do before do png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") - FakeWeb.register_uri(:get, "http://wiki.mozilla.org/images/2/2e/Longcat1.png", body: png) + stub_request(:get, "http://wiki.mozilla.org/images/2/2e/Longcat1.png").to_return(body: png) SiteSetting.download_remote_images_to_local = true + FastImage.expects(:size).returns([100, 100]).at_least_once end it 'replaces image src' do diff --git a/spec/jobs/update_gravatar_spec.rb b/spec/jobs/update_gravatar_spec.rb index 0c80a3afcf6..bba53c724ab 100644 --- a/spec/jobs/update_gravatar_spec.rb +++ b/spec/jobs/update_gravatar_spec.rb @@ -8,7 +8,7 @@ describe Jobs::UpdateGravatar do expect(user.user_avatar.gravatar_upload_id).to eq(nil) png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") - FakeWeb.register_uri(:get, "http://www.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930.png?s=500&d=404", body: png) + stub_request(:get, "http://www.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930.png?s=360&d=404").to_return(body: png) SiteSetting.automatically_download_gravatars = true diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9409f66ae2c..8100e927820 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1155,6 +1155,7 @@ describe User do describe "refresh_avatar" do it "enqueues the update_gravatar job when automatically downloading gravatars" do SiteSetting.automatically_download_gravatars = true + SiteSetting.queue_jobs = true user = Fabricate(:user) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index cc2ac9b91be..985bea4fb6e 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -9,17 +9,14 @@ require 'rbtrace' #uncomment the following line to use spork with the debugger #require 'spork/ext/ruby-debug' -require 'fakeweb' -FakeWeb.allow_net_connect = false - Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. require 'fabrication' require 'mocha/api' - require 'fakeweb' require 'certified' + require 'webmock/rspec' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__)