Skip to content

Commit

Permalink
Use update instead of update_all
Browse files Browse the repository at this point in the history
`update_all` does not run validations
  • Loading branch information
garyhtou committed Aug 10, 2023
1 parent 0dba85f commit 07f1ccb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/controllers/hackathon/subscriptions/bulk_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class Hackathon::Subscriptions::BulkController < ApplicationController
# Marking subscriptions as active. Used for undoing an "Unsubscribe from all".
# PUT /users/:user_id/subscriptions/bulk
def update
@subscriptions.update_all(status: :active)
@subscriptions.each(&:resubscribe!)
redirect_to Hackathon::Subscription.manage_subscriptions_url_for @user
end

# Marking subscriptions as inactive.
# DELETE /users/:user_id/subscriptions/bulk
def destroy
@subscriptions.update_all(status: :inactive)
@subscriptions.each(&:unsubscribe!)
redirect_to Hackathon::Subscription.manage_subscriptions_url_for @user
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/hackathon/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def unsubscribe_all
@subscriptions = @user.subscriptions.active

@unsubscribed_ids = @subscriptions.pluck(:id)
@unsubscribe_count = @subscriptions.update_all(status: :inactive)
@unsubscribe_count = @subscriptions.map(&:unsubscribe!).count(true)
end

private
Expand Down
8 changes: 8 additions & 0 deletions app/models/hackathon/subscription/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def unsubscribe_all_url_for(user)
end
end

def unsubscribe!
update(status: :inactive)
end

def resubscribe!
update(status: :active)
end

private

def track_changes
Expand Down

0 comments on commit 07f1ccb

Please sign in to comment.