Sha256: b41b15f62e482c29b66c42b5545ec6c331b55a613e014acedfa06cd4817abb3a

Contents?: true

Size: 1.22 KB

Versions: 9

Compression:

Stored size: 1.22 KB

Contents

module Maestrano::Connector::Rails::Concerns::Synchronization
  extend ActiveSupport::Concern

  included do
    # Keeping only 100 synchronizations per organization
    after_create :clean_synchronizations

    RUNNING_STATUS = 'RUNNING'.freeze
    ERROR_STATUS = 'ERROR'.freeze
    SUCCESS_STATUS = 'SUCCESS'.freeze

    #===================================
    # Associations
    #===================================
    belongs_to :organization

    validates :status, presence: true
  end

  module ClassMethods
    def create_running(organization)
      Maestrano::Connector::Rails::Synchronization.create(organization_id: organization.id, status: RUNNING_STATUS)
    end
  end

  def running?
    status == RUNNING_STATUS
  end

  def error?
    status == ERROR_STATUS
  end

  def success?
    status == SUCCESS_STATUS
  end

  def mark_as_success
    update_attributes(status: SUCCESS_STATUS)
  end

  def mark_as_error(msg)
    update_attributes(status: ERROR_STATUS, message: msg)
  end

  def mark_as_partial
    update_attributes(partial: true)
  end

  def clean_synchronizations
    count = organization.synchronizations.count
    organization.synchronizations.order('id ASC').limit(count - 100).destroy_all if count > 100
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
maestrano-connector-rails-2.2.1 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.2.0 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.1.3 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.1.2 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.1.1 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.1.0 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.0.2.pre.RC12 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.0.2.pre.RC11 app/models/maestrano/connector/rails/concerns/synchronization.rb
maestrano-connector-rails-2.0.2.pre.RC10 app/models/maestrano/connector/rails/concerns/synchronization.rb