Merge pull request #2180 from tomciopp/master

More idiomatic approach to finding drafts
This commit is contained in:
Sam 2014-04-08 16:46:24 +10:00
commit d71f62a9f9
11 changed files with 31 additions and 30 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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})

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -199,7 +199,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

View file

@ -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)

View file

@ -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