Module: Service::Finder
- Included in:
- Record
- Defined in:
- lib/service/finder.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
- #find_all(options = {}, top = nil) ⇒ Object
-
#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.
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( = {}) if(.class == Fixnum) model = self.new(quickbook_gateway.get("#{entity.downcase}/#{}")[entity]) model.is_new_record = false return model end return self.find_all(, 1)[0] end |
#find_all(options = {}, top = nil) ⇒ Object
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( = {}, top = nil) raise QuickBookgatewayInvalid unless quickbook_gateway top = .delete(:top) unless top records = Array.new unless .keys.index(:conditions) conditions = [""] .keys.each{|column| conditions[0] += "AND #{column}=?" conditions << .delete(column) } conditions[0] = conditions[0][4..conditions[0].length].to_s [:conditions] = conditions end if top [:top] = top total_records = top else total_records = quickbook_gateway.query_data(create_query(.merge({:select => "COUNT(*)"})))["totalCount"].to_i end if total_records > 0 query_data(, 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() return new() if(.keys.length == 0) qb_record = find(.dup) .delete(:conditions) qb_record = new() unless qb_record return qb_record end |