refactoring

This commit is contained in:
Sam 2013-05-28 09:13:53 +10:00
parent bf4bdbefe0
commit d2f2a8e218
2 changed files with 24 additions and 33 deletions

View file

@ -162,20 +162,15 @@ ORDER BY p.created_at desc
end end
action.save! action.save!
action_type = hash[:action_type]
user_id = hash[:user_id] user_id = hash[:user_id]
if action_type == LIKE update_like_count(user_id, hash[:action_type], 1)
User.update_all('likes_given = likes_given + 1', id: user_id)
elsif action_type == WAS_LIKED
User.update_all('likes_received = likes_received + 1', id: user_id)
end
topic = Topic.includes(:category).where(id: hash[:target_topic_id]).first topic = Topic.includes(:category).where(id: hash[:target_topic_id]).first
# move into Topic perhaps # move into Topic perhaps
group_ids = nil group_ids = nil
if topic && topic.category && topic.category.secure if topic && topic.category && topic.category.secure
group_ids = topic.category.groups.select("groups.id").map{|g| g.id} group_ids = topic.category.groups.pluck("groups.id")
end end
MessageBus.publish("/users/#{action.user.username.downcase}", MessageBus.publish("/users/#{action.user.username.downcase}",
@ -197,17 +192,19 @@ ORDER BY p.created_at desc
MessageBus.publish("/user/#{hash[:user_id]}", {user_action_id: action.id, remove: true}) MessageBus.publish("/user/#{hash[:user_id]}", {user_action_id: action.id, remove: true})
end end
action_type = hash[:action_type] update_like_count(hash[:user_id], hash[:action_type], -1)
user_id = hash[:user_id]
if action_type == LIKE
User.update_all('likes_given = likes_given - 1', id: user_id)
elsif action_type == WAS_LIKED
User.update_all('likes_received = likes_received - 1', id: user_id)
end
end end
protected protected
def self.update_like_count(user_id, action_type, delta)
if action_type == LIKE
User.update_all("likes_given = likes_given + #{delta.to_i}", id: user_id)
elsif action_type == WAS_LIKED
User.update_all("likes_received = likes_received + #{delta.to_i}", id: user_id)
end
end
def self.apply_common_filters(builder,user_id,guardian,ignore_private_messages=false) def self.apply_common_filters(builder,user_id,guardian,ignore_private_messages=false)
unless guardian.can_see_deleted_posts? unless guardian.can_see_deleted_posts?

View file

@ -252,31 +252,25 @@ class PostCreator
def add_users(topic, usernames) def add_users(topic, usernames)
return unless usernames return unless usernames
usernames = usernames.split(',') User.where(username: usernames.split(',')).each do |user|
User.where(username: usernames).each do |u| check_can_send_permission!(topic,user)
topic.topic_allowed_users.build(user_id: user.id)
unless guardian.can_send_private_message?(u)
topic.errors.add(:archetype, :cant_send_pm)
@errors = topic.errors
raise ActiveRecord::Rollback.new
end
topic.topic_allowed_users.build(user_id: u.id)
end end
end end
def add_groups(topic, groups) def add_groups(topic, groups)
return unless groups return unless groups
groups = groups.split(',') Group.where(name: groups.split(',')).each do |group|
Group.where(name: groups).each do |g| check_can_send_permission!(topic,group)
topic.topic_allowed_groups.build(group_id: group.id)
end
end
unless guardian.can_send_private_message?(g) def check_can_send_permission!(topic,item)
topic.errors.add(:archetype, :cant_send_pm) unless guardian.can_send_private_message?(item)
@errors = topic.errors topic.errors.add(:archetype, :cant_send_pm)
raise ActiveRecord::Rollback.new @errors = topic.errors
end raise ActiveRecord::Rollback.new
topic.topic_allowed_groups.build(group_id: g.id)
end end
end end
end end