Skip to content

Commit

Permalink
Merge pull request #1198 from bgeuken/watchlist
Browse files Browse the repository at this point in the history
Refactor watchlist updates
  • Loading branch information
bgeuken committed Oct 5, 2015
2 parents 5c62068 + 8210e51 commit e802d82
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/api/app/controllers/person_controller.rb
Expand Up @@ -162,25 +162,14 @@ def internal_register

def update_watchlist( user, xml )
new_watchlist = []
old_watchlist = []

xml.get('watchlist').elements("project") do |e|
new_watchlist << e['name']
end

user.watched_projects.each do |wp|
old_watchlist << wp.project.name
end
add_to_watchlist = new_watchlist.collect {|i| old_watchlist.include?(i) ? nil : i}.compact
remove_from_watchlist = old_watchlist.collect {|i| new_watchlist.include?(i) ? nil : i}.compact

remove_from_watchlist.each do |name|
user.watched_projects.where(project_id: Project.find_by_name(name).id).delete_all
end

add_to_watchlist.each do |name|
user.watched_projects.new(project_id: Project.find_by_name(name).id)
new_watchlist.map! do |name|
WatchedProject.find_or_create_by(project: Project.find_by_name!(name), user: user)
end
user.watched_projects.replace(new_watchlist)

return true
end
Expand Down

0 comments on commit e802d82

Please sign in to comment.