Skip to content

Instantly share code, notes, and snippets.

@israelb
Last active December 2, 2017 01:28
Show Gist options
  • Save israelb/579e062fc1b8a3500036c538382e01d9 to your computer and use it in GitHub Desktop.
Save israelb/579e062fc1b8a3500036c538382e01d9 to your computer and use it in GitHub Desktop.
# app/models/request_manager.rb
class RequestManager < ApplicationRecord
# associations
belongs_to :requestable, polymorphic: true
# constants
DELIVERS = %w(now later system).freeze
ACTIONS_ALLOWED_TO_DUPLICATE = %(
save_requisition
save_documents
update_status_a05
).freeze
# validations
validates :action, presence: true
validates :deliver, presence: true, inclusion: { in: DELIVERS }
# scopes
scope :not_success_by_requestable, ->(type, id) {
where(requestable_type: type, requestable_id: id, success: false, deliver: 'later')
.order('id')
}
scope :exists, ->(requestable, action) {
where(requestable: requestable, action: action, deliver: 'later')
}
scope :not_success, -> { where(success: false) }
scope :not_success_requestables, ->{
select(:requestable_type, :requestable_id)
.group(:requestable_type, :requestable_id)
.where(success: false, deliver: 'later')
}
def self.add_multiple_deliver_later(requestable, actions)
actions.each do |action|
create_request_later(requestable, action, requestable.status)
end
deliver_by_queque(requestable.class.to_s, requestable.id)
end
def self.add_deliver_later(requestable, action, status)
create_request_later(requestable, action, status)
deliver_by_queque(requestable.class.to_s, requestable.id)
end
def self.add_deliver_now(requestable, action, params)
request = create(requestable: requestable, action: action, deliver: 'now')
request.deliver_now(params)
end
def self.add_deliver_system(requestable, action, status)
request = create(requestable: requestable, action: action, deliver: 'system', status: status)
deliver_later(request)
end
def self.deliver_by_queque(requestable_type, requestable_id)
request = not_success_by_requestable(requestable_type, requestable_id).first
return true if request.nil?
deliver_later(request)
end
def deliver_now(params)
action = Zell::Action.new(self)
response = action.send(self.action, *params)
set_request_successful
response
end
def finish_deliver_later
set_request_successful
return unless deliver == 'later'
RequestManager.deliver_by_queque(requestable_type, requestable_id)
end
private
def self.create_request_later(requestable, action, status)
request_exists(requestable, action).first_or_create do |request|
request.status = status
end
end
def self.request_exists(requestable, action)
if ACTIONS_ALLOWED_TO_DUPLICATE.include?(action)
exists(requestable, action).not_success
else
exists(requestable, action)
end
end
def self.deliver_later(request)
Zell::Action.later(request.id)
end
def set_request_successful
return if success
update(success: true)
end
end
# app/models/sync_request_manager.rb
class SyncRequestManager < RequestManager
extend SyncRequestManagerRequisition
def self.incompleted
requestables = RequestManager.not_success_requestables
requestables.each do |requestable|
CustomLogger.requisition_sync_status("requestable: #{requestable.inspect}")
RequestManager.deliver_by_queque(requestable.requestable_type, requestable.requestable_id)
end
end
end
## salida:
> [[false, "sync_status", 4669573], [false, "sync_status", 4664214], [false, "sync_status", 4657047],
[false, "sync_status", 4650319], [false, "sync_status", 4646358], [false, "sync_status", 4645225],
[false, "sync_status", 4644121], [false, "sync_status", 4637777], [false, "sync_status", 4635722],
[false, "sync_status", 4633050], [false, "sync_status", 4630373], [false, "sync_status", 4627007],
[false, "sync_status", 4622278], [false, "sync_status", 4617507], [false, "sync_status", 4611633],
[false, "sync_status", 4605091], [true, "update_status_au1", 4603045], [true, "save_requisition", 4603044],
[true, "update_status_s05", 4603016], [true, "update_status_s03", 4602915], [true, "update_status_s02", 4602870],
[true, "update_status_s01", 4602804], [true, "create_app", 4602700], [true, "register_user", 4602699]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment