chore: Apply fixes for items in rubocop_todo [CW-1806] (#8864)

This PR addresses several items listed in our rubocop_todo by implementing the necessary corrections and enhancements. As a result, we are now able to remove the rubocop_todo file entirely, streamlining our codebase and ensuring adherence to our coding standards.

fixes: https://linear.app/chatwoot/issue/CW-1806/chore-rubocop-audit
This commit is contained in:
Sojan Jose
2024-02-07 13:36:04 +04:00
committed by GitHub
parent 3abb887445
commit 7776b74126
21 changed files with 67 additions and 398 deletions

View File

@@ -15,7 +15,7 @@ class AccountBuilder
end
[@user, @account]
rescue StandardError => e
puts e.inspect
Rails.logger.debug e.inspect
raise e
end

View File

@@ -40,7 +40,7 @@ class Api::V1::AccountsController < Api::BaseController
def cache_keys
expires_in 10.seconds, public: false, stale_while_revalidate: 5.minutes
render json: { cache_keys: get_cache_keys }, status: :ok
render json: { cache_keys: cache_keys_for_account }, status: :ok
end
def update
@@ -66,7 +66,7 @@ class Api::V1::AccountsController < Api::BaseController
raise CustomExceptions::Account::InvalidParams.new({})
end
def get_cache_keys
def cache_keys_for_account
{
label: fetch_value_for_key(params[:id], Label.name.underscore),
inbox: fetch_value_for_key(params[:id], Inbox.name.underscore),

View File

@@ -100,7 +100,7 @@ class Account < ApplicationRecord
.where(context: 'labels',
taggable_type: 'Conversation',
taggable_id: conversation_ids)
.map { |_| _.tag.name }
.map { |tagging| tagging.tag.name }
end
def webhook_data

View File

@@ -43,7 +43,7 @@ class Article < ApplicationRecord
belongs_to :account
belongs_to :category, optional: true
belongs_to :portal
belongs_to :author, class_name: 'User'
belongs_to :author, class_name: 'User', inverse_of: :articles
before_validation :ensure_account_id
before_validation :ensure_article_slug

View File

@@ -38,7 +38,8 @@ class Attachment < ApplicationRecord
has_one_attached :file
validate :acceptable_file
validates :external_url, length: { maximum: Limits::URL_LENGTH_LIMIT }
enum file_type: [:image, :audio, :video, :file, :location, :fallback, :share, :story_mention, :contact]
enum file_type: { :image => 0, :audio => 1, :video => 2, :file => 3, :location => 4, :fallback => 5, :share => 6, :story_mention => 7,
:contact => 8 }
def push_event_data
return unless file_type

View File

@@ -11,10 +11,10 @@
#
class CannedResponse < ApplicationRecord
validates_presence_of :content
validates_presence_of :short_code
validates_presence_of :account
validates_uniqueness_of :short_code, scope: :account_id
validates :content, presence: true
validates :short_code, presence: true
validates :account, presence: true
validates :short_code, uniqueness: { scope: :account_id }
belongs_to :account

View File

@@ -46,20 +46,20 @@ class Channel::FacebookPage < ApplicationRecord
def subscribe
# ref https://developers.facebook.com/docs/messenger-platform/reference/webhook-events
response = Facebook::Messenger::Subscriptions.subscribe(
Facebook::Messenger::Subscriptions.subscribe(
access_token: page_access_token,
subscribed_fields: %w[
messages message_deliveries message_echoes message_reads standby messaging_handovers
]
)
rescue => e
rescue StandardError => e
Rails.logger.debug { "Rescued: #{e.inspect}" }
true
end
def unsubscribe
Facebook::Messenger::Subscriptions.unsubscribe(access_token: page_access_token)
rescue => e
rescue StandardError => e
Rails.logger.debug { "Rescued: #{e.inspect}" }
true
end
@@ -73,6 +73,7 @@ class Channel::FacebookPage < ApplicationRecord
delete_instagram_story(message) if story_link.blank?
story_link
rescue Koala::Facebook::ClientError => e
Rails.logger.debug { "Instagram Story Expired: #{e.inspect}" }
delete_instagram_story(message)
end

View File

@@ -89,7 +89,7 @@ class Conversation < ApplicationRecord
belongs_to :account
belongs_to :inbox
belongs_to :assignee, class_name: 'User', optional: true
belongs_to :assignee, class_name: 'User', optional: true, inverse_of: :assigned_conversations
belongs_to :contact
belongs_to :contact_inbox
belongs_to :team, optional: true

View File

@@ -26,7 +26,7 @@ class CsatSurveyResponse < ApplicationRecord
belongs_to :conversation
belongs_to :contact
belongs_to :message
belongs_to :assigned_agent, class_name: 'User', optional: true
belongs_to :assigned_agent, class_name: 'User', optional: true, inverse_of: :csat_survey_responses
validates :rating, presence: true, inclusion: { in: [1, 2, 3, 4, 5] }
validates :account_id, presence: true

View File

@@ -21,7 +21,7 @@ class Macro < ApplicationRecord
belongs_to :account
belongs_to :created_by,
class_name: :User, optional: true
class_name: :User, optional: true, inverse_of: :macros
belongs_to :updated_by,
class_name: :User, optional: true
has_many_attached :files

View File

@@ -118,7 +118,7 @@ class Message < ApplicationRecord
belongs_to :account
belongs_to :inbox
belongs_to :conversation, touch: true
belongs_to :sender, polymorphic: true, required: false
belongs_to :sender, polymorphic: true, optional: true
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
has_one :csat_survey_response, dependent: :destroy_async
@@ -139,8 +139,8 @@ class Message < ApplicationRecord
conversation_id: conversation.display_id,
conversation: conversation_push_event_data
)
data.merge!(echo_id: echo_id) if echo_id.present?
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
data[:echo_id] = echo_id if echo_id.present?
data[:attachments] = attachments.map(&:push_event_data) if attachments.present?
merge_sender_attributes(data)
end
@@ -163,8 +163,8 @@ class Message < ApplicationRecord
end
def merge_sender_attributes(data)
data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot)
data.merge!(sender: sender.push_event_data(inbox)) if sender.is_a?(AgentBot)
data[:sender] = sender.push_event_data if sender && !sender.is_a?(AgentBot)
data[:sender] = sender.push_event_data(inbox) if sender.is_a?(AgentBot)
data
end
@@ -184,7 +184,7 @@ class Message < ApplicationRecord
sender: sender.try(:webhook_data),
source_id: source_id
}
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
data[:attachments] = attachments.map(&:push_event_data) if attachments.present?
data
end

View File

@@ -13,5 +13,5 @@
class TelegramBot < ApplicationRecord
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy_async
validates_uniqueness_of :auth_key, scope: :account_id
validates :auth_key, uniqueness: { scope: :account_id }
end

View File

@@ -74,15 +74,15 @@ class User < ApplicationRecord
has_many :accounts, through: :account_users
accepts_nested_attributes_for :account_users
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify, inverse_of: :assignee
alias_attribute :conversations, :assigned_conversations
has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify
has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify, inverse_of: :assigned_agent
has_many :conversation_participants, dependent: :destroy_async
has_many :participating_conversations, through: :conversation_participants, source: :conversation
has_many :inbox_members, dependent: :destroy_async
has_many :inboxes, through: :inbox_members, source: :inbox
has_many :messages, as: :sender
has_many :messages, as: :sender, dependent: :nullify
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', source: :inviter, dependent: :nullify
has_many :custom_filters, dependent: :destroy_async
@@ -94,12 +94,16 @@ class User < ApplicationRecord
has_many :notifications, dependent: :destroy_async
has_many :team_members, dependent: :destroy_async
has_many :teams, through: :team_members
has_many :articles, foreign_key: 'author_id', dependent: :nullify
has_many :articles, foreign_key: 'author_id', dependent: :nullify, inverse_of: :author
has_many :portal_members, class_name: :PortalMember, dependent: :destroy_async
has_many :portals, through: :portal_members, source: :portal,
class_name: :Portal,
dependent: :nullify
has_many :macros, foreign_key: 'created_by_id'
# rubocop:disable Rails/HasManyOrHasOneDependent
# we are handling this in `remove_macros` callback
has_many :macros, foreign_key: 'created_by_id', inverse_of: :created_by
# rubocop:enable Rails/HasManyOrHasOneDependent
before_validation :set_password_and_uid, on: :create
after_destroy :remove_macros