Add rubocop to our build. (#5004)

This commit is contained in:
Guo Xiang Tan
2017-07-28 10:20:09 +09:00
committed by GitHub
parent ff4e295c4f
commit 5012d46cbd
871 changed files with 5480 additions and 6056 deletions

View File

@ -74,13 +74,13 @@ class User < ActiveRecord::Base
has_many :acting_group_histories, dependent: :destroy, foreign_key: :acting_user_id, class_name: GroupHistory
has_many :targeted_group_histories, dependent: :destroy, foreign_key: :target_user_id, class_name: GroupHistory
delegate :last_sent_email_address, :to => :email_logs
delegate :last_sent_email_address, to: :email_logs
validates_presence_of :username
validate :username_validator, if: :username_changed?
validate :password_validator
validates :name, user_full_name: true, if: :name_changed?, length: { maximum: 255 }
validates :ip_address, allowed_ip_address: {on: :create, message: :signup_not_allowed}
validates :ip_address, allowed_ip_address: { on: :create, message: :signup_not_allowed }
validates :primary_email, presence: true, if: :should_validate_primary_email?
validates_associated :primary_email, if: :should_validate_primary_email?
@ -240,13 +240,12 @@ class User < ActiveRecord::Base
find_by(username_lower: username.downcase)
end
def enqueue_welcome_message(message_type)
return unless SiteSetting.send_welcome_message?
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
end
def change_username(new_username, actor=nil)
def change_username(new_username, actor = nil)
UsernameChanger.change(self, new_username, actor)
end
@ -273,7 +272,7 @@ class User < ActiveRecord::Base
end
# Approve this user
def approve(approved_by, send_mail=true)
def approve(approved_by, send_mail = true)
self.approved = true
if approved_by.is_a?(Integer)
@ -318,7 +317,7 @@ class User < ActiveRecord::Base
n.user_id = :user_id AND
NOT read"
User.exec_sql(sql, user_id: id, type: notification_type).getvalue(0,0).to_i
User.exec_sql(sql, user_id: id, type: notification_type).getvalue(0, 0).to_i
end
def unread_private_messages
@ -342,7 +341,7 @@ class User < ActiveRecord::Base
User.exec_sql(sql, user_id: id,
seen_notification_id: seen_notification_id,
pm: Notification.types[:private_message])
.getvalue(0,0).to_i
.getvalue(0, 0).to_i
end
end
@ -377,7 +376,6 @@ class User < ActiveRecord::Base
notification = notifications.visible.order('notifications.id desc').first
json = NotificationSerializer.new(notification).as_json if notification
sql = "
SELECT * FROM (
SELECT n.id, n.read FROM notifications n
@ -404,18 +402,18 @@ class User < ActiveRecord::Base
"
recent = User.exec_sql(sql, user_id: id,
type: Notification.types[:private_message]).values.map do |id, read|
type: Notification.types[:private_message]).values.map do |id, read|
[id.to_i, read == 't'.freeze]
end
MessageBus.publish("/notification/#{id}",
{unread_notifications: unread_notifications,
unread_private_messages: unread_private_messages,
total_unread_notifications: total_unread_notifications,
read_first_notification: read_first_notification?,
last_notification: json,
recent: recent,
seen_notification_id: seen_notification_id
{ unread_notifications: unread_notifications,
unread_private_messages: unread_private_messages,
total_unread_notifications: total_unread_notifications,
read_first_notification: read_first_notification?,
last_notification: json,
recent: recent,
seen_notification_id: seen_notification_id
},
user_ids: [id] # only publish the notification to this user
)
@ -475,7 +473,7 @@ class User < ActiveRecord::Base
last_seen_at.present?
end
def create_visit_record!(date, opts={})
def create_visit_record!(date, opts = {})
user_stat.update_column(:days_visited, user_stat.days_visited + 1)
user_visits.create!(visited_at: date, posts_read: opts[:posts_read] || 0, mobile: opts[:mobile] || false)
end
@ -488,7 +486,7 @@ class User < ActiveRecord::Base
create_visit_record!(date) unless visit_record_for(date)
end
def update_posts_read!(num_posts, opts={})
def update_posts_read!(num_posts, opts = {})
now = opts[:at] || Time.zone.now
_retry = opts[:retry] || false
@ -502,7 +500,7 @@ class User < ActiveRecord::Base
create_visit_record!(now.to_date, posts_read: num_posts, mobile: opts.fetch(:mobile, false))
rescue ActiveRecord::RecordNotUnique
if !_retry
update_posts_read!(num_posts, opts.merge( retry: true ))
update_posts_read!(num_posts, opts.merge(retry: true))
else
raise
end
@ -516,7 +514,7 @@ class User < ActiveRecord::Base
end
end
def update_last_seen!(now=Time.zone.now)
def update_last_seen!(now = Time.zone.now)
now_date = now.to_date
# Only update last seen once every minute
redis_key = "user:#{id}:#{now_date}"
@ -703,7 +701,7 @@ class User < ActiveRecord::Base
save
end
def change_trust_level!(level, opts=nil)
def change_trust_level!(level, opts = nil)
Promotion.new(self).change_trust_level!(level, opts)
end
@ -716,26 +714,26 @@ class User < ActiveRecord::Base
user_badges.select('distinct badge_id').count
end
def featured_user_badges(limit=3)
def featured_user_badges(limit = 3)
tl_badge_ids = Badge.trust_level_badge_ids
query = user_badges
.group(:badge_id)
.select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" },
.group(:badge_id)
.select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" },
'COUNT(*) AS "count"',
'MAX(badges.badge_type_id) AS badges_badge_type_id',
'MAX(badges.grant_count) AS badges_grant_count')
.joins(:badge)
.order('badges_badge_type_id ASC, badges_grant_count ASC, badge_id DESC')
.includes(:user, :granted_by, { badge: :badge_type }, { post: :topic })
.joins(:badge)
.order('badges_badge_type_id ASC, badges_grant_count ASC, badge_id DESC')
.includes(:user, :granted_by, { badge: :badge_type }, post: :topic)
tl_badge = query.where("user_badges.badge_id IN (:tl_badge_ids)",
tl_badge_ids: tl_badge_ids)
.limit(1)
.limit(1)
other_badges = query.where("user_badges.badge_id NOT IN (:tl_badge_ids)",
tl_badge_ids: tl_badge_ids)
.limit(limit)
.limit(limit)
(tl_badge + other_badges).take(limit)
end
@ -750,7 +748,6 @@ class User < ActiveRecord::Base
result.group('date(users.created_at)').order('date(users.created_at)').count
end
def secure_category_ids
cats = self.admin? ? Category.where(read_restricted: true) : secure_categories.references(:categories)
cats.pluck('categories.id').sort
@ -760,16 +757,15 @@ class User < ActiveRecord::Base
Category.topic_create_allowed(self.id).select(:id)
end
# Flag all posts from a user as spam
def flag_linked_posts_as_spam
disagreed_flag_post_ids = PostAction.where(post_action_type_id: PostActionType.types[:spam])
.where.not(disagreed_at: nil)
.pluck(:post_id)
.where.not(disagreed_at: nil)
.pluck(:post_id)
topic_links.includes(:post)
.where.not(post_id: disagreed_flag_post_ids)
.each do |tl|
.where.not(post_id: disagreed_flag_post_ids)
.each do |tl|
begin
message = I18n.t('flag_reason.spam_hosts', domain: tl.domain)
PostAction.act(Discourse.system_user, tl.post, PostActionType.types[:spam], message: message)
@ -812,7 +808,6 @@ class User < ActiveRecord::Base
.exists?
end
def refresh_avatar
return if @import_mode
@ -868,25 +863,25 @@ class User < ActiveRecord::Base
def number_of_deleted_posts
Post.with_deleted
.where(user_id: self.id)
.where.not(deleted_at: nil)
.count
.where(user_id: self.id)
.where.not(deleted_at: nil)
.count
end
def number_of_flagged_posts
Post.with_deleted
.where(user_id: self.id)
.where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids)
.where(user_id: self.id)
.where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids)
.where(disagreed_at: nil)
.select(:post_id))
.count
.count
end
def number_of_flags_given
PostAction.where(user_id: self.id)
.where(disagreed_at: nil)
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
.count
.where(disagreed_at: nil)
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
.count
end
def number_of_suspensions
@ -924,8 +919,8 @@ class User < ActiveRecord::Base
return unless active && email_confirmed? && !staged
Group.where(automatic: false)
.where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0")
.each do |group|
.where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0")
.each do |group|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
@ -1064,10 +1059,10 @@ class User < ActiveRecord::Base
# Delete unactivated accounts (without verified email) that are over a week old
def self.purge_unactivated
to_destroy = User.where(active: false)
.joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
.where("created_at < ?", SiteSetting.purge_unactivated_users_grace_period_days.days.ago)
.where('NOT admin AND NOT moderator')
.limit(200)
.joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
.where("created_at < ?", SiteSetting.purge_unactivated_users_grace_period_days.days.ago)
.where('NOT admin AND NOT moderator')
.limit(200)
destroyer = UserDestroyer.new(Discourse.system_user)
to_destroy.each do |u|