diff --git a/app/models/customer.rb b/app/models/customer.rb index b4fd5d2..2b809e7 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -19,6 +19,7 @@ class Customer < QboBaseModel validates_presence_of :name before_validation :normalize_phone_numbers self.primary_key = :id + qbo_sync push: true acts_as_searchable columns: %w[name phone_number mobile_phone_number ], scope: ->(_context) { left_joins(:project) }, diff --git a/app/models/employee.rb b/app/models/employee.rb index a554f9d..406f8ac 100644 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -13,5 +13,6 @@ class Employee < QboBaseModel has_many :users validates_presence_of :id, :name self.primary_key = :id + qbo_sync push: false end \ No newline at end of file diff --git a/app/models/estimate.rb b/app/models/estimate.rb index 32aadef..bc41f27 100644 --- a/app/models/estimate.rb +++ b/app/models/estimate.rb @@ -14,6 +14,7 @@ class Estimate < QboBaseModel belongs_to :customer validates_presence_of :doc_number, :id self.primary_key = :id + qbo_sync push: false # returns a human readable string def to_s diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 12e1514..28ace31 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -15,6 +15,7 @@ class Invoice < QboBaseModel validates :id, presence: true, uniqueness: true validates :doc_number, :txn_date, presence: true self.primary_key = :id + qbo_sync push: false # Return the invoice's document number as its string representation def to_s diff --git a/app/models/qbo_base_model.rb b/app/models/qbo_base_model.rb index 824f122..f2e5dfc 100644 --- a/app/models/qbo_base_model.rb +++ b/app/models/qbo_base_model.rb @@ -14,10 +14,10 @@ class QboBaseModel < ActiveRecord::Base self.abstract_class = true validates_presence_of :id - + class_attribute :qbo_push_enabled, default: true attr_accessor :skip_qbo_push - before_validation :push_to_qbo, on: :create - after_commit :push_to_qbo, on: :update, unless: :skip_qbo_push? + before_validation :push_to_qbo, on: :create, if: :push_to_qbo? + after_commit :push_to_qbo, on: :update, if: :push_to_qbo? # Returns the details of the entity. # If the details have already been fetched, it returns the cached version. @@ -55,6 +55,13 @@ class QboBaseModel < ActiveRecord::Base end end + def push_to_qbo? + log "qbo_push_enabled #{self.class.qbo_push_enabled}" + log "skip_qbo_push #{skip_qbo_push}" + + self.class.qbo_push_enabled && skip_qbo_push != true + end + # Repsonds to missing methods by delegating to the QBO entity calss if the method is defined there. # This allows for dynamic access to any attributes or methods of the QBO customer without having to explicitly define them in the Subclass model, providing flexibility and reducing boilerplate code. def respond_to_missing?(method_name, include_private = false) @@ -76,7 +83,11 @@ class QboBaseModel < ActiveRecord::Base # Flag used to update local without pushing to QBO. # This is used to prevent loops with the webhook def skip_qbo_push? - !!skip_qbo_push + !!skip_qbo_push + end + + def self.qbo_sync(push: true) + self.qbo_push_enabled = push end private