Sha256: 534ebec94edb41cca6ae2e41ade6c4616f257090c33f714f94916857d0bd1ec8

Contents?: true

Size: 681 Bytes

Versions: 3

Compression:

Stored size: 681 Bytes

Contents

require 'singleton'
require 'contact_manager/model/contact'

class ContactRepository
  include Singleton
  
  def all
    Contact.all
  end
  
  def search(query_value)
    if query_value.present?
      attribute_names = Contact.new.attributes.keys
      conditions = attribute_names.reduce('') do |conditions, attribute|
        if conditions.blank?
          conditions += "lower(#{attribute}) like ?"
        else
          conditions += " OR lower(#{attribute}) like ?"
        end
      end
      Contact.where(conditions, *(["%#{query_value.downcase}%"]*attribute_names.count))
    else
      all
    end
  end
  
  def destroy_all_contacts
    Contact.destroy_all
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
contact_manager-1.0.2 app/contact_manager/model/contact_repository.rb
contact_manager-1.0.1 app/contact_manager/model/contact_repository.rb
contact_manager-1.0.0 app/contact_manager/model/contact_repository.rb