From 2beaeed36d3d51f3e66cf2b79d6b1bea4a51465e Mon Sep 17 00:00:00 2001 From: Thomas Cioppettini Date: Tue, 25 Mar 2014 15:56:21 -0700 Subject: [PATCH 1/2] More idiomatic approach to finding drafts --- app/models/draft.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/draft.rb b/app/models/draft.rb index 0972eb25e..3f0527c88 100644 --- a/app/models/draft.rb +++ b/app/models/draft.rb @@ -29,10 +29,12 @@ class Draft < ActiveRecord::Base protected - def self.find_draft(user,key) - user_id = user - user_id = user.id if User === user - Draft.where(user_id: user_id, draft_key: key).first + def self.find_draft(user, key) + if user.is_a?(User) + find_by(user_id: user.id, draft_key: key) + else + find_by(user_id: user, draft_key: key) + end end end From 38882eb1a7f13377d2b38f0a964aae6e6eae652d Mon Sep 17 00:00:00 2001 From: Thomas Cioppettini Date: Wed, 26 Mar 2014 12:20:41 -0700 Subject: [PATCH 2/2] Remove threequals from ruby files --- app/models/category.rb | 6 +++--- app/models/category_featured_user.rb | 2 +- app/models/topic_user.rb | 13 ++++++------- app/models/user.rb | 2 +- lib/guardian.rb | 4 ++-- lib/js_locale_helper.rb | 14 +++++++------- lib/search.rb | 2 +- lib/site_setting_extension.rb | 2 +- lib/tasks/qunit.rake | 2 +- spec/support/helpers.rb | 4 ++-- 10 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index c137be6b7..2b1a3966e 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -276,11 +276,11 @@ SQL full = CategoryGroup.permission_types[:full] mapped = permissions.map do |group,permission| - group = group.id if Group === group + 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 Fixnum === group - permission = CategoryGroup.permission_types[permission] unless Fixnum === permission + group = Group[group.to_sym].id unless group.is_a?(Fixnum) + permission = CategoryGroup.permission_types[permission] unless permission.is_a?(Fixnum) [group, permission] end diff --git a/app/models/category_featured_user.rb b/app/models/category_featured_user.rb index 3bb639c16..33268879b 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 Fixnum === category_or_category_id + if category_or_category_id.is_a?(Fixnum) category_or_category_id else category_or_category_id.id diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index f9a3c0674..4053dd78a 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -66,13 +66,12 @@ class TopicUser < ActiveRecord::Base result end - def get(topic,user) - topic = topic.id if Topic === topic - user = user.id if User === user - TopicUser.where('topic_id = ? and user_id = ?', topic, user).first + def get(topic, user) + topic = topic.id if topic.is_a?(Topic) + user = user.id if user.is_a?(User) + TopicUser.find_by(topic_id: topic, user_id: user) end - # Change attributes for a user (creates a record when none is present). First it tries an update # since there's more likely to be an existing record than not. If the update returns 0 rows affected # it then creates the row instead. @@ -117,8 +116,8 @@ class TopicUser < ActiveRecord::Base end def track_visit!(topic,user) - topic_id = Topic === topic ? topic.id : topic - user_id = User === user ? user.id : topic + topic_id = topic.is_a?(Topic) ? topic.id : topic + user_id = user.is_a?(User) ? user.id : topic now = DateTime.now rows = TopicUser.where({topic_id: topic_id, user_id: user_id}).update_all({last_visited_at: now}) diff --git a/app/models/user.rb b/app/models/user.rb index 08c561b51..533d882be 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -200,7 +200,7 @@ class User < ActiveRecord::Base def approve(approved_by, send_mail=true) self.approved = true - if Fixnum === approved_by + if approved_by.is_a?(Fixnum) self.approved_by_id = approved_by else self.approved_by = approved_by diff --git a/lib/guardian.rb b/lib/guardian.rb index b80860a72..1350c971b 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -197,7 +197,7 @@ class Guardian end def can_send_private_message?(target) - (User === target || Group === target) && + (target.is_a?(Group) || target.is_a?(User)) && # User is authenticated authenticated? && # Can't send message to yourself @@ -221,7 +221,7 @@ class Guardian end def is_me?(other) - other && authenticated? && User === other && @user == other + other && authenticated? && other.is_a?(User) && @user == other end def is_not_me?(other) diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index 6642a0dfd..a558d7246 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -96,13 +96,13 @@ module JsLocaleHelper end def self.strip_out_message_formats!(hash, prefix = "", rval = {}) - if Hash === hash - hash.each do |k,v| - if Hash === v - rval.merge!(strip_out_message_formats!(v, prefix + (prefix.length > 0 ? "." : "") << k, rval)) - elsif k.to_s().end_with?("_MF") - rval[prefix + (prefix.length > 0 ? "." : "") << k] = v - hash.delete(k) + if hash.is_a?(Hash) + hash.each do |key, value| + if value.is_a?(Hash) + rval.merge!(strip_out_message_formats!(value, prefix + (prefix.length > 0 ? "." : "") << key, rval)) + elsif key.to_s.end_with?("_MF") + rval[prefix + (prefix.length > 0 ? "." : "") << key] = value + hash.delete(key) end end end diff --git a/lib/search.rb b/lib/search.rb index 2579e6b70..7b2c270ca 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -47,7 +47,7 @@ class Search @limit = Search.per_facet * Search.facets.size @results = GroupedSearchResults.new(@opts[:type_filter]) - if Topic === @search_context && @search_context.posts_count < SiteSetting.min_posts_for_search_in_topic + if @search_context.is_a?(Topic) && @search_context.posts_count < SiteSetting.min_posts_for_search_in_topic @search_context = nil end end diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index d42d615b3..317fb99eb 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -191,7 +191,7 @@ module SiteSettingExtension val = (val == "t" || val == "true") ? 't' : 'f' end - if type == types[:fixnum] && !(Fixnum === val) + if type == types[:fixnum] && !val.is_a?(Fixnum) val = val.to_i end diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake index 31cbe2a2d..b39b37e4c 100644 --- a/lib/tasks/qunit.rake +++ b/lib/tasks/qunit.rake @@ -48,7 +48,7 @@ task "qunit:test" => :environment do # A bit of a hack until we can figure this out on Travis tries = 0 - while tries < 3 && $?.exitstatus === 124 && !quit + while tries < 3 && $?.exitstatus == 124 && !quit tries += 1 puts "\nTimed Out. Trying again...\n" rake_system(cmd) diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index a3753d732..1ae65ea4b 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -28,7 +28,7 @@ module Helpers args[:title] ||= "This is my title #{Helpers.next_seq}" user = args.delete(:user) || Fabricate(:user) guardian = Guardian.new(user) - args[:category] = args[:category].name if Category === args[:category] + args[:category] = args[:category].name if args[:category].is_a?(Category) TopicCreator.create(user, guardian, args) end @@ -37,7 +37,7 @@ module Helpers args[:raw] ||= "This is the raw body of my post, it is cool #{Helpers.next_seq}" args[:topic_id] = args[:topic].id if args[:topic] user = args.delete(:user) || Fabricate(:user) - args[:category] = args[:category].name if Category === args[:category] + args[:category] = args[:category].name if args[:category].is_a?(Category) PostCreator.create(user, args) end