Sha256: 89a62967529c7e67fcdc9bb3ee50724ccba82661e8a70e7e18080450164a95f5

Contents?: true

Size: 948 Bytes

Versions: 16

Compression:

Stored size: 948 Bytes

Contents

module Gaku
  class ContactUpdation
    attr_reader :contact, :errors

    def initialize(contact)
      @contact = contact
      @errors  = []
    end

    def update(params)
      @contact.assign_attributes(params)

      if @contact.save
        if @contact.contactable.respond_to?(:contacts)
          remove_other_primary
          # update_primary_contact_field
        end
        true
      else
        @errors = @contact.errors
        false
      end
    end

    private

    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
    # def update_primary_contact_field
    #   if @contact.contactable.has_attribute?(:primary_contact)
    #     @contact.contactable.update_column(:primary_contact, @contact.contactable.contact_widget)
    #   end
    # end

  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
gaku_core-0.3.0 app/services/gaku/contact_updation.rb
gaku_core-0.3.0.pre.4 app/services/gaku/contact_updation.rb
gaku_core-0.3.0.pre.3 app/services/gaku/contact_updation.rb
gaku_core-0.3.0.pre.2 app/services/gaku/contact_updation.rb
gaku_core-0.3.0.pre.1 app/services/gaku/contact_updation.rb
gaku_core-0.3.0.pre.0 app/services/gaku/contact_updation.rb
gaku-0.2.4 core/app/services/gaku/contact_updation.rb
gaku_core-0.2.4 app/services/gaku/contact_updation.rb
gaku-0.2.3 core/app/services/gaku/contact_updation.rb
gaku_core-0.2.3 app/services/gaku/contact_updation.rb
gaku-0.2.2 core/app/services/gaku/contact_updation.rb
gaku_core-0.2.2 app/services/gaku/contact_updation.rb
gaku-0.2.1 core/app/services/gaku/contact_updation.rb
gaku_core-0.2.1 app/services/gaku/contact_updation.rb
gaku-0.2.0 core/app/services/gaku/contact_updation.rb
gaku_core-0.2.0 app/services/gaku/contact_updation.rb