Skip to content

Commit

Permalink
Reducing subtle duplication of effort/logic
Browse files Browse the repository at this point in the history
Prior to this commit, when we change attributes for a user we might
resave each article.  The purpose of the resave is to update some of the
cached attributes of an article.

This removes some redundant code.  The following removed code has
an `article.save` call.  Which, the callbacks in the article (see below)
already clear the cache.

```ruby
def resave_articles
  articles.find_each do |article|
    if article.path
      cache_bust = EdgeCache::Bust.new
      cache_bust.call(article.path)
      cache_bust.call("#{article.path}?i=i")
    end
    article.save
  end
end
```

https://github.com/forem/forem/blob/8e6981aac59e86a9a2c69db1e50fb1119f509c8d/app/models/article.rb#L164

https://github.com/forem/forem/blob/8e6981aac59e86a9a2c69db1e50fb1119f509c8d/app/models/article.rb#L881-L888

This was discovered in triaging #17041
  • Loading branch information
jeremyf committed Apr 1, 2022
1 parent 0fc1a40 commit 7d3ab82
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 12 deletions.
11 changes: 0 additions & 11 deletions app/models/user.rb
Expand Up @@ -497,17 +497,6 @@ def a_sustaining_member?
monthly_dues.positive?
end

def resave_articles
articles.find_each do |article|
if article.path
cache_bust = EdgeCache::Bust.new
cache_bust.call(article.path)
cache_bust.call("#{article.path}?i=i")
end
article.save
end
end

def profile_image_90
profile_image_url_for(length: 90)
end
Expand Down
2 changes: 1 addition & 1 deletion app/workers/users/resave_articles_worker.rb
Expand Up @@ -8,7 +8,7 @@ def perform(user_id)
user = User.find_by(id: user_id)
return unless user

user.resave_articles
user.articles.find_each(&:save)
end
end
end

0 comments on commit 7d3ab82

Please sign in to comment.