Module: Service::Finder

Included in:
Record
Defined in:
lib/service/finder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
# File 'lib/service/finder.rb', line 3

def self.extended(base)
  base.send :extend, Query
end

Instance Method Details

#find(options = {}) ⇒ Object

Only fetch first record as per options provided Options are as below

Same as Find All
  Or you can directly provide Id of record.
  For ex. if you want to fetch customer with Id 1, then Customer.find(1)


36
37
38
39
40
41
42
43
44
45
# File 'lib/service/finder.rb', line 36

def find(options = {})
  if(options.class == Fixnum)
    model = self.new(quickbook_gateway.get("#{entity.downcase}/#{options}")[entity]) 
    model.is_new_record = false
    return model
  end
  
  options[:top] = 1
  return self.find_all(options)[0]
end

#find_all(options = {}) ⇒ Object

Find All records related to options provided Options are as below

:conditions => where clause. Default is NOTHING
            For example, if you want to fetch customer with DisplayName 'Kunal' then :conditions => ["DisplayName = ?", "Kunal"]

:select => Needed column list. Default is ALL
        For ex. if you want Id, DisplayName of all customers then :select => "Id, DisplayName"

Raises:

  • (QuickBookgatewayInvalid)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/service/finder.rb', line 14

def find_all(options = {})
  raise QuickBookgatewayInvalid unless quickbook_gateway
  
  records = Array.new
  
  qb_data = quickbook_gateway.query_data(create_query(options))[entity]
  
  qb_data.each{|qb_record|
    model = self.new(qb_record)
    model.is_new_record = false
    
    records << model
  } if qb_data
  
  return records
end

#find_or_initialize(options) ⇒ Object

Try to find record on QuickBook Online as per options provided else create instance of new Options should be an Hash contains columns and values to find record.

For ex. if you want to update customer(Kunal) country to India if exist or create new:
    kunal = Customer.find_or_initialize({"DisplayName" => "Kunal"})
    kunal.Country = "India"
    kunal.save!


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/service/finder.rb', line 53

def find_or_initialize(options)
  return new() if(options.keys.length == 0)
  
  conditions = [""]
  options.keys.each{|column|
    conditions[0] += "AND #{column}=?"
    conditions << options[column]
  }
  
  conditions[0] = conditions[0][4..conditions[0].length].to_s
  
  qb_record = find({:conditions => conditions})
  
  qb_record = new(options) unless qb_record
  
  return qb_record
end