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

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