Rubocop: fix Performance/OpenStruct (#9241)
* Fix Performance/OpenStruct in Article and Organization * Fix Performance/OpenStruct in Notifications::Reactions::Send * Add missing rubocop disable for Performance/OpenStruct * Fix serialization
This commit is contained in:
parent
25f739b389
commit
101cca4a68
7 changed files with 32 additions and 36 deletions
|
|
@ -12,15 +12,6 @@ require:
|
|||
# Note that changes in the inspected code, or installation of new
|
||||
# versions of RuboCop, may require this file to be generated again.
|
||||
|
||||
# Offense count: 8
|
||||
Performance/OpenStruct:
|
||||
Exclude:
|
||||
- 'app/models/article.rb'
|
||||
- 'app/models/organization.rb'
|
||||
- 'app/services/notifications/reactions/send.rb'
|
||||
- 'spec/models/github_repo_spec.rb'
|
||||
- 'spec/requests/github_repos_spec.rb'
|
||||
|
||||
# Offense count: 16
|
||||
Rails/OutputSafety:
|
||||
Exclude:
|
||||
|
|
|
|||
|
|
@ -584,23 +584,19 @@ class Article < ApplicationRecord
|
|||
end
|
||||
|
||||
def update_cached_user
|
||||
if organization
|
||||
self.cached_organization = OpenStruct.new(set_cached_object(organization))
|
||||
end
|
||||
|
||||
return unless user
|
||||
|
||||
self.cached_user = OpenStruct.new(set_cached_object(user))
|
||||
self.cached_organization = set_cached_object(organization) if organization
|
||||
self.cached_user = set_cached_object(user) if user
|
||||
end
|
||||
|
||||
# TODO: [thepracticaldev/oss] this should eventually be moved to JSON, to avoid storing a native Ruby object in the DB
|
||||
def set_cached_object(object)
|
||||
{
|
||||
name: object.name,
|
||||
username: object.username,
|
||||
slug: object == organization ? object.slug : object.username,
|
||||
profile_image_90: object.profile_image_90,
|
||||
profile_image_url: object.profile_image_url
|
||||
}
|
||||
Struct.new(:name, :username, :slug, :profile_image_90, :profile_image_url).new.tap do |struct|
|
||||
struct.name = object.name
|
||||
struct.username = object.username
|
||||
struct.slug = object.respond_to?(:slug) ? object.slug : object.username
|
||||
struct.profile_image_90 = object.profile_image_90
|
||||
struct.profile_image_url = object.profile_image_url
|
||||
end
|
||||
end
|
||||
|
||||
def set_all_dates
|
||||
|
|
|
|||
|
|
@ -122,14 +122,16 @@ class Organization < ApplicationRecord
|
|||
def update_articles
|
||||
return unless saved_change_to_slug || saved_change_to_name || saved_change_to_profile_image
|
||||
|
||||
cached_org_object = {
|
||||
name: name,
|
||||
username: username,
|
||||
slug: slug,
|
||||
profile_image_90: profile_image_90,
|
||||
profile_image_url: profile_image_url
|
||||
}
|
||||
articles.update(cached_organization: OpenStruct.new(cached_org_object))
|
||||
# TODO: [thepracticaldev/oss] this should eventually be moved to JSON,
|
||||
# to avoid storing a native Ruby object in the DB
|
||||
cached_org_object = Struct.new(:name, :username, :slug, :profile_image_90, :profile_image_url).new
|
||||
cached_org_object.name = name
|
||||
cached_org_object.username = username
|
||||
cached_org_object.slug = slug
|
||||
cached_org_object.profile_image_90 = profile_image_90
|
||||
cached_org_object.profile_image_url = profile_image_url
|
||||
|
||||
articles.update(cached_organization: cached_org_object)
|
||||
end
|
||||
|
||||
def bust_cache
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
# send notifications about the new reaction
|
||||
module Notifications
|
||||
module Reactions
|
||||
SendResult = Struct.new(:action, :notification_id)
|
||||
|
||||
class Send
|
||||
# @param reaction_data [Hash]
|
||||
# * :reactable_id [Integer] - article or comment id
|
||||
|
|
@ -18,7 +20,7 @@ module Notifications
|
|||
new(*args).call
|
||||
end
|
||||
|
||||
# @return [OpenStruct, #action, #notification_id]
|
||||
# @return [Struct, #action, #notification_id]
|
||||
def call
|
||||
return unless receiver.is_a?(User) || receiver.is_a?(Organization)
|
||||
|
||||
|
|
@ -45,7 +47,8 @@ module Notifications
|
|||
|
||||
if aggregated_reaction_siblings.size.zero?
|
||||
Notification.where(notification_params).delete_all
|
||||
OpenStruct.new(action: :deleted)
|
||||
|
||||
SendResult.new(:deleted, nil)
|
||||
else
|
||||
recent_reaction = reaction_siblings.first
|
||||
|
||||
|
|
@ -63,7 +66,7 @@ module Notifications
|
|||
|
||||
notification_id = save_notification(notification_params, notification)
|
||||
|
||||
OpenStruct.new(action: :saved, notification_id: notification_id)
|
||||
SendResult.new(:saved, notification_id)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ article_methods_to_include = %i[
|
|||
|
||||
json.array!(@stories) do |article|
|
||||
json.extract! article, *article_attributes_to_include
|
||||
json.user article.cached_user.as_json["table"]
|
||||
json.user article.cached_user.as_json
|
||||
|
||||
if article.cached_organization?
|
||||
json.organization article.cached_organization.as_json["table"]
|
||||
json.organization article.cached_organization.as_json
|
||||
end
|
||||
|
||||
if article.main_image?
|
||||
|
|
|
|||
|
|
@ -86,7 +86,9 @@ RSpec.describe GithubRepo, type: :model do
|
|||
end
|
||||
|
||||
let(:stubbed_github_repo) do
|
||||
# rubocop:disable Performance/OpenStruct
|
||||
OpenStruct.new(repo.attributes.merge(id: repo.github_id_code, html_url: repo.url))
|
||||
# rubocop:enable Performance/OpenStruct
|
||||
end
|
||||
let(:github_client) { instance_double(fake_github_client, repository: stubbed_github_repo) }
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,9 @@ RSpec.describe "GithubRepos", type: :request do
|
|||
html_url: Faker::Internet.url,
|
||||
)
|
||||
|
||||
# rubocop:disable Performance/OpenStruct
|
||||
[OpenStruct.new(repo1_params), OpenStruct.new(repo2_params)]
|
||||
# rubocop:enable Performance/OpenStruct
|
||||
end
|
||||
let(:github_client) do
|
||||
instance_double(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue