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)


72
73
74
75
76
77
78
79
80
# File 'lib/service/finder.rb', line 72

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
  
  return self.find_all(options, 1)[0]
end

#find_all(options = {}, top = nil) ⇒ Object

Raises:

  • (QuickBookgatewayInvalid)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/service/finder.rb', line 34

def find_all(options = {}, top = nil)
  raise QuickBookgatewayInvalid unless quickbook_gateway
  
  top = options.delete(:top) unless top
  
  records = Array.new
  
  unless options.keys.index(:conditions)
    conditions = [""]
    options.keys.each{|column|
      conditions[0] += "AND #{column}=?"
      conditions << options.delete(column)
    }
  
    conditions[0] = conditions[0][4..conditions[0].length].to_s
    
    options[:conditions] = conditions
  end
  
  if top
    options[:top] = top 
    total_records = top
  else
    total_records = quickbook_gateway.query_data(create_query(options.merge({:select => "COUNT(*)"})))["totalCount"].to_i
  end
  
  if total_records > 0
    query_data(options, total_records, records)
  end
  
  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!


88
89
90
91
92
93
94
95
96
97
# File 'lib/service/finder.rb', line 88

def find_or_initialize(options)
  return new() if(options.keys.length == 0)
  
  qb_record = find(options.dup)
  
  options.delete(:conditions)
  qb_record = new(options) unless qb_record
  
  return qb_record
end