lib/pagarme/nested_model.rb in pagarme-2.1.2 vs lib/pagarme/nested_model.rb in pagarme-2.1.3
- old
+ new
@@ -1,8 +1,8 @@
module PagarMe
class NestedModel < Model
- attr_reader :parent_id
+ attr_accessor :parent_id
def initialize(hash = Hash.new)
hash = hash.dup
@parent_id = hash.delete(:parent_id) || hash.delete('parent_id')
super hash
@@ -26,20 +26,27 @@
def find_by_id(parent_id, id)
raise RequestError.new('Invalid ID') unless id.present?
raise RequestError.new('Invalid parent ID') unless parent_id.present?
- PagarMe::Request.get(url parent_id, id).call
+ object = PagarMe::Request.get(url parent_id, id).call
+ if object
+ object.parent_id = parent_id
+ end
+ object
end
alias :find :find_by_id
def find_by(parent_id, hash, page = 1, count = 10)
raise RequestError.new('Invalid page count') if page < 1 or count < 1
PagarMe::Request.get(url(parent_id), params: hash.merge(
page: page,
count: count
- )).call
+ )).call.map do |object|
+ object.parent_id = parent_id
+ object
+ end
end
alias :find_by_hash :find_by
def all(parent_id, page = 1, count = 10)
find_by parent_id, Hash.new, page, count
\ No newline at end of file