lib/apisync/rails/model.rb in apisync-rails-0.0.2 vs lib/apisync/rails/model.rb in apisync-rails-0.0.3
- old
+ new
@@ -16,12 +16,17 @@
attr_reader :attributes
def initialize(model)
@model = model
@attributes = {}
+ @should_sync = true
end
+ def sync_if(method_name)
+ @should_sync = @model.send(method_name.to_sym)
+ end
+
def attribute(attr_name, from: nil, value: nil)
@attributes.delete(attr_name)
@attributes[attr_name] = attr_value(attr_name, from: from, value: value)
end
@@ -33,17 +38,33 @@
value: attr_value(attr_name, from: from, value: value)
}
end
def sync
- set_reference_id
- validate!
- log_warnings
- Apisync::Rails::Http.post(@attributes)
+ if sync?
+ set_reference_id
+ validate!
+ log_warnings
+
+ if defined?(::Sidekiq)
+ Apisync::Rails::SyncModelJob::Sidekiq.perform_async(
+ @model.class.name,
+ @model.id,
+ @attributes
+ )
+ else
+ Apisync::Rails::Http.post(
+ @attributes,
+ request_concurrency: :synchronous
+ )
+ end
+ end
end
def validate!
+ return unless sync?
+
REQUIRED_ATTRS.each do |attr, message|
if @attributes[attr].blank?
raise MissingAttribute, "Please specify #{attr}. #{message}"
end
end
@@ -56,9 +77,13 @@
end
end
end
private
+
+ def sync?
+ @should_sync
+ end
def set_reference_id
if @attributes[:reference_id].blank? && @model.id.present?
@attributes[:reference_id] = @model.id.to_s
end