additionals/app/models/additionals_journal.rb

49 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-10-11 16:41:46 +03:00
class AdditionalsJournal
class << self
def save_journal_history(journal, prop_key, ids_old, ids)
ids_all = (ids_old + ids).uniq
ids_all.each do |id|
next if ids_old.include?(id) && ids.include?(id)
if ids.include? id
value = id
old_value = nil
else
old_value = id
value = nil
end
journal.details << JournalDetail.new(property: 'attr',
prop_key: prop_key,
old_value: old_value,
value: value)
journal.save
2019-10-11 16:41:46 +03:00
end
true
2019-10-11 16:41:46 +03:00
end
def validate_relation(entries, entry_id)
old_entries = entries.select { |entry| entry.id.present? }
new_entries = entries.select { |entry| entry.id.blank? }
return true if new_entries.blank?
new_entries.map! { |entry| entry.send entry_id }
return false if new_entries.count != new_entries.uniq.count
old_entries.map! { |entry| entry.send entry_id }
return false unless (old_entries & new_entries).count.zero?
true
end
2020-05-05 16:25:23 +03:00
def set_relation_detail(entity, detail, value_key)
value = detail.send value_key
detail[value_key] = (entity.find_by(id: value) || value) if value.present?
end
2019-10-11 16:41:46 +03:00
end
end