Sha256: bf315debc3fa9de710d3488a10c184de35cce6a12735978cfb6115aa23634a09

Contents?: true

Size: 1.82 KB

Versions: 27

Compression:

Stored size: 1.82 KB

Contents

module Octobat
  class Item < APIResource
    extend Octobat::APIOperations::List
    include Octobat::APIOperations::Create
    include Octobat::APIOperations::Update
    include Octobat::APIOperations::Delete
    
    
    def url
      !parent_obj.nil? ? parentize_url : super
    end
    
    
    def save_url
      if self[:id] == nil && self.class.respond_to?(:create)
        self.relative_save_url
      else
        url
      end
    end
    
    
    def parentize_url
      if parent_obj.include?(:transaction)
        "#{Transaction.url}/#{CGI.escape(parent_obj[:transaction])}/items/#{CGI.escape(id)}"
      elsif parent_obj.include?(:invoice)
        "#{Invoice.url}/#{CGI.escape(parent_obj[:invoice])}/items/#{CGI.escape(id)}"
      elsif parent_obj.include?(:credit_note)
        "#{CreditNote.url}/#{CGI.escape(parent_obj[:credit_note])}/items/#{CGI.escape(id)}"
      else
        url
      end
    end
    
    
    
    def relative_save_url
      if self[:transaction]
        "#{Transaction.url}/#{CGI.escape(self[:transaction])}/items"
      elsif self[:invoice]
        "#{Invoice.url}/#{CGI.escape(self[:invoice])}/items"
      elsif self[:credit_note]
        "#{CreditNote.url}/#{CGI.escape(self[:credit_note])}/items"
      end
    end
    

    def self.url
      if @parent_resource.include?(:transaction)
        "#{Transaction.url}/#{CGI.escape(@parent_resource[:transaction])}/items"
      elsif @parent_resource.include?(:invoice)
        "#{Invoice.url}/#{CGI.escape(@parent_resource[:invoice])}/items"
      elsif @parent_resource.include?(:credit_note)
        "#{CreditNote.url}/#{CGI.escape(@parent_resource[:credit_note])}/items"
      end
    end

    def self.set_parent_resource(filters)
      @parent_resource = filters.select{|k, v| [:transaction, :invoice, :credit_note].include?(k)}
    end
    
    
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
octobat-2.0.27 lib/octobat/item.rb
octobat-2.0.26 lib/octobat/item.rb
octobat-2.0.25 lib/octobat/item.rb
octobat-2.0.24 lib/octobat/item.rb
octobat-2.0.23 lib/octobat/item.rb
octobat-2.0.22 lib/octobat/item.rb
octobat-2.0.21 lib/octobat/item.rb
octobat-2.0.20 lib/octobat/item.rb
octobat-2.0.19 lib/octobat/item.rb
octobat-2.0.18 lib/octobat/item.rb
octobat-2.0.17 lib/octobat/item.rb
octobat-2.0.16 lib/octobat/item.rb
octobat-2.0.15 lib/octobat/item.rb
octobat-2.0.14 lib/octobat/item.rb
octobat-2.0.12 lib/octobat/item.rb
octobat-2.0.11 lib/octobat/item.rb
octobat-2.0.10 lib/octobat/item.rb
octobat-2.0.9 lib/octobat/item.rb
octobat-2.0.8 lib/octobat/item.rb
octobat-2.0.7 lib/octobat/item.rb