DRY up excess calls to `Device.current`

pull/527/head
Rick Carlino 2017-11-08 08:31:43 -06:00
parent a5957f51b7
commit 713e368d37
6 changed files with 20 additions and 19 deletions

View File

@ -1,10 +1,11 @@
# Run Rails & Webpack concurrently
rails: rails s -e development -p 3000 -b 0.0.0.0
rails: rails s -e development -p 3000 -b 0.0.0.0
webpack: ./node_modules/.bin/webpack-dev-server --config config/webpack.config.js
worker: rake jobs:work
worker: rake jobs:work
# mqtt: rails mqtt:start
# UNCOMMENT THIS LINE IF YOU ARE DOING MOBILE TESTING:
# Get started with `npm install weinre -g`
# Learn more at https://people.apache.org/~pmuellr/weinre/docs/latest/

View File

@ -2,13 +2,10 @@ class CreateAttachmentFromUrlJob < ApplicationJob
queue_as :default
def perform(image:, attachment_url:)
Device.current = image.device
image.set_attachment_by_url(attachment_url)
image.save!
Device.current = nil
rescue => e
Rollbar.error('ERROR PROCESSING IMAGE!!', e)
raise e
image.device.auto_sync_transaction do
image.set_attachment_by_url(attachment_url)
image.save!
end
end
def max_attempts

View File

@ -7,9 +7,7 @@ class ApplicationRecord < ActiveRecord::Base
"last_sign_in_at",
"last_sign_in_ip",
"sign_in_count",
# HACK: Regimens and RegimenItems won't sync if you
# activate this line:
# "updated_at",
"updated_at",
"current_sign_in_at" ]
# Determine if the changes to the model are worth broadcasting or not.

View File

@ -49,12 +49,18 @@ class Device < ApplicationRecord
RequestStore.store[:device] = dev
end
# Sets Device.current to `self` and returns it to the previous value when
# finished running block. Usually this is unecessary, but may be required in
# background jobs. If you are not receiving auto_sync data on your client,
# you probably need to use this method.
def auto_sync_transaction
prev = Device.current
Device.current = self
yield
Device.current = prev
end
def self.current_jwt
RequestStore.store[:jwt]
end
def self.mine # For development mode debugging.
raise "NO" unless Rails.env.development?
Device.current = User.find_by!(email: "admin@admin.com").device
end
end

View File

@ -43,8 +43,6 @@ class Image < ApplicationRecord
content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]
def set_attachment_by_url(url)
# Image.new.set_attachment_by_url("http://i.imgur.com/OhLresv.png").save!
Device.current = self.device
self.attachment = open(url)
self.attachment_processed_at = Time.now
self

View File

@ -2,6 +2,7 @@ unless Rails.env == "production"
ToolSlot.destroy_all
Tool.destroy_all
Point.destroy_all
LogDispatch.destroy_all
User.destroy_all
POINT_COUNT = 2