Add errbit recipe
Merge branch 'master-chef-11.10' of github.com:CruGlobal/cru-cookbooks into master-chef-11.10
require_dependency 'async'
module GlobalRegistryMethods
  extend ActiveSupport::Concern
  include Async

  included do
    after_commit :push_to_global_registry
    after_destroy :delete_from_global_registry
  end

  def delete_from_global_registry
    if global_registry_id
      Sidekiq::Client.enqueue(self.class, nil, :async_delete_from_global_registry, global_registry_id)
    end
  end

  # Define default push method
  def push_to_global_registry
    async(:async_push_to_global_registry)
  end

  def async_push_to_global_registry(parent_id = nil, parent_type = nil)
    self.class.push_structure_to_global_registry

    if global_registry_id
      begin
        update_in_global_registry(parent_id, parent_type)
      rescue RestClient::ResourceNotFound
        create_in_global_registry(parent_id, parent_type)
      end
    else
      create_in_global_registry(parent_id, parent_type)
    end
  end

  def attributes_to_push(*args)
    unless @attributes_to_push
      @attributes_to_push = {}
      attributes_to_push['client_integration_id'] = id unless self.class.skip_fields_for_gr.include?('client_integration_id')
      attributes_to_push['client_updated_at'] = updated_at if respond_to?(:updated_at)
      attributes.collect {|k, v| @attributes_to_push[k.underscore] = v}
      @attributes_to_push.select! {|k, v| v.present? && !self.class.skip_fields_for_gr.include?(k)}
    end
    @attributes_to_push
  end

  def update_in_global_registry(parent_id = nil, parent_type = nil)
    if parent_type
      create_in_global_registry(parent_id, parent_type)