Sha256: 39e307e3c65a6dc4bf23ff9ff4748ffe22f84d026183116591d32bd845c10e79
Contents?: true
Size: 984 Bytes
Versions: 16
Compression:
Stored size: 984 Bytes
Contents
module Gaku class ContactCreation attr_reader :errors, :contact def initialize(params = {}) @contact = Contact.new(params) @errors = [] end def save ensure_first_is_primary if @contact.save if @contact.contactable.respond_to?(:contacts) remove_other_primary end return true else @errors = @contact.errors return false end end def save! if save return @contact else fail 'Failed to save record' end end private def ensure_first_is_primary if @contact.contactable.respond_to?(:contacts) && @contact.contactable.contacts.blank? @contact.primary = true end end def remove_other_primary if @contact.primary? @contact.contactable.reload.contacts.where.not(id: @contact.id).update_all(primary: false) end end # Workaround performance issue by not using JOIN end end
Version data entries
16 entries across 16 versions & 2 rubygems