]> git.openstreetmap.org Git - rails.git/commitdiff
Move mechanics of changeset subscriptions into the model
authorTom Hughes <tom@compton.nu>
Sat, 24 Feb 2024 10:11:57 +0000 (10:11 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 25 Feb 2024 09:48:11 +0000 (09:48 +0000)
app/controllers/api/changesets_controller.rb
app/models/changeset.rb
test/models/changeset_test.rb

index c9c806de6ecc4a7e19d2d345180c87ea3d643493..b4e864f187d4e76d3552186ec6e3e94631f4b528 100644 (file)
@@ -44,7 +44,7 @@ module Api
       cs.save_with_tags!
 
       # Subscribe user to changeset comments
-      cs.subscribers << current_user
+      cs.subscribe(current_user)
 
       render :plain => cs.id.to_s
     end
@@ -233,10 +233,10 @@ module Api
 
       # Find the changeset and check it is valid
       changeset = Changeset.find(id)
-      raise OSM::APIChangesetAlreadySubscribedError, changeset if changeset.subscribers.exists?(current_user.id)
+      raise OSM::APIChangesetAlreadySubscribedError, changeset if changeset.subscribed?(current_user)
 
       # Add the subscriber
-      changeset.subscribers << current_user
+      changeset.subscribe(current_user)
 
       # Return a copy of the updated changeset
       @changeset = changeset
@@ -259,10 +259,10 @@ module Api
 
       # Find the changeset and check it is valid
       changeset = Changeset.find(id)
-      raise OSM::APIChangesetNotSubscribedError, changeset unless changeset.subscribers.exists?(current_user.id)
+      raise OSM::APIChangesetNotSubscribedError, changeset unless changeset.subscribed?(current_user)
 
       # Remove the subscriber
-      changeset.subscribers.delete(current_user)
+      changeset.unsubscribe(current_user)
 
       # Return a copy of the updated changeset
       @changeset = changeset
index 7f70f4a383185b91d237aabe2223a90791ad6883..abb494de64af3333a761431d2191d9b92afa9759 100644 (file)
@@ -213,4 +213,16 @@ class Changeset < ApplicationRecord
 
     save_with_tags!
   end
+
+  def subscribe(user)
+    subscribers << user
+  end
+
+  def unsubscribe(user)
+    subscribers.delete(user)
+  end
+
+  def subscribed?(user)
+    subscribers.exists?(user.id)
+  end
 end
index 3be9a52e636fc8ba9d58d64c5022226a673f480f..affa773d19294f91fee7dcd4c2a4d5e8f8188fca 100644 (file)
@@ -71,4 +71,17 @@ class ChangesetTest < ActiveSupport::TestCase
       Changeset.from_xml(xml, :create => true)
     end
   end
+
+  def test_subscription
+    changeset = create(:changeset)
+    user = create(:user)
+
+    assert_not changeset.subscribed?(user)
+
+    changeset.subscribe(user)
+    assert changeset.subscribed?(user)
+
+    changeset.unsubscribe(changeset.subscribers.first)
+    assert_not changeset.subscribed?(user)
+  end
 end