Skip to content

Commit

Permalink
enhance the overwrite attributes method
Browse files Browse the repository at this point in the history
  • Loading branch information
mortbauer committed Jun 3, 2024
1 parent 5ae30de commit 301a6bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Makefile
.git
.gitignore

Expand Down
2 changes: 1 addition & 1 deletion docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
foodsoft_worker:
build:
context: .
dockerfile: Dockerfile-dev
dockerfile: Dockerfile
platform: linux/x86_64
command: ./proc-start worker
volumes:
Expand Down
29 changes: 25 additions & 4 deletions plugins/messages/app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class Message < ApplicationRecord
has_many :message_recipients, dependent: :destroy
has_many :recipients, through: :message_recipients, source: :user

attr_accessor :send_method, :recipient_tokens, :order_id
validate :recipients_ids_must_be_valid

attr_accessor :send_method, :recipient_tokens, :order_id, :recipients_ids

scope :threads, -> { where(reply_to: nil) }
scope :thread, ->(id) { where('id = ? OR reply_to = ?', id, id) }
Expand All @@ -33,10 +35,27 @@ class Message < ApplicationRecord

# Override the `attributes=` method to exclude `recipients_ids`
def attributes=(new_attributes)
new_attributes = new_attributes.reject { |key, _| key.to_sym == :recipients_ids }
if new_attributes.respond_to?(:with_indifferent_access)
new_attributes = new_attributes.with_indifferent_access
end

# Log the attributes for debugging purposes
Rails.logger.debug "Original attributes: #{new_attributes.inspect}"

# Remove `recipients_ids` from the attributes hash
new_attributes = new_attributes.except(:recipients_ids, 'recipients_ids')

# Log the sanitized attributes for debugging purposes
Rails.logger.debug "Sanitized attributes: #{new_attributes.inspect}"

super(new_attributes)
end

def recipients_ids=(value)
@recipients_ids = value
Rails.logger.debug "Setting recipients_ids to: #{value.inspect}"
end

after_initialize do
@recipients_ids ||= []
@send_method ||= 'recipients'
Expand Down Expand Up @@ -130,8 +149,6 @@ def sender_name
'?'
end

attr_reader :recipients_ids

def last_reply
Message.where(reply_to: id).order(:created_at).last
end
Expand All @@ -152,4 +169,8 @@ def can_toggle_private?(user)
def create_salt
self.salt = [Array.new(6) { rand(256).chr }.join].pack('m').chomp
end

def recipients_ids_must_be_valid
errors.add(:recipients_ids, "must be valid") if recipients_ids.blank?
end
end
1 change: 0 additions & 1 deletion tmp/.gitignore

This file was deleted.

0 comments on commit 301a6bf

Please sign in to comment.