Sha256: 541837202673e39aac31e4b29e5cc8689db3af1437826c4a40ef52fb455dd213

Contents?: true

Size: 1.38 KB

Versions: 11

Compression:

Stored size: 1.38 KB

Contents

#temp patch as_json on decimals introduced by rails. which collpases with ruby-yail
class BigDecimal
  def as_json
    self.to_f
  end
end

module ActiveResource
  # Overridden methods to suit SalesKing.
  # Some changes might be kicked when AR 3.0 is out
  class Base

    # override encode because json is also returned nested by SalesKing
    def encode(options={})
      case self.class.format
        when ActiveResource::Formats[:xml]
          self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options))
        else # json also nested
          self.class.format.encode( {self.class.element_name => attributes}, options)
      end
    end

    # override ARes method to parse only the client part
    def load_attributes_from_response(response)
      if response['Content-Length'] != "0" && response.body.strip.size > 0
        load( self.class.format.decode(response.body)[self.class.element_name] )
      end
    end

    # Overridden to grab the data(= clients-collection) from json:
    # { 'collection'=> will_paginate infos,
    #   'links' => prev/next links
    #   'clients'=> [data], << what we need
    # }
    def self.instantiate_collection(collection, prefix_options = {})
      collection = collection[ self.element_name.pluralize ] if collection.is_a?(Hash)
      collection.collect! { |record| instantiate_record(record, prefix_options) }
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
sk_sdk-0.2.2 lib/sk_sdk/ar_patches/ar2/base.rb
sk_sdk-0.2.1 lib/sk_sdk/ar_patches/ar2/base.rb
sk_sdk-0.1.1 lib/sk_sdk/ar_patches/ar2/base.rb
sk_sdk-0.1.0 lib/sk_sdk/ar_patches/ar2/base.rb
sk_sdk-0.0.8 lib/sk_sdk/ar_patches/ar2/base.rb
sk_sdk-0.0.7 lib/sk_sdk/ar_cli/patches/ar2/base.rb
sk_sdk-0.0.6 lib/sk_sdk/ar_cli/patches/ar2/base.rb
sk_sdk-0.0.5 lib/sk_sdk/ar_cli/patches/ar2/base.rb
sk_sdk-0.0.4 lib/sk_sdk/ar_cli/patches/ar2/base.rb
sk_sdk-0.0.3 lib/sk_sdk/ar_cli/patches/ar2/base.rb
sk-api-1.1.0 lib/patches/ar2/base.rb