1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Add job for tagging images

This commit is contained in:
Daniel Barber 2018-08-25 12:06:30 -04:00
parent 442f9a1472
commit 19e61fbfd8
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,8 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records
# are no longer available
# discard_on ActiveJob::DeserializationError
end

10
app/jobs/tag_image_job.rb Normal file
View File

@ -0,0 +1,10 @@
class TagImageJob < ApplicationJob
queue_as :default
def perform(image_id:)
image = Image.find(image_id)
tags = Clarifai.new(image.image.file.path).tags
image.update_attributes(tags: tags)
end
end

View File

@ -0,0 +1,5 @@
class AddTagsToImages < ActiveRecord::Migration[5.2]
def change
add_column :images, :tags, :string, array: true, default: []
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2014_03_24_170840) do
ActiveRecord::Schema.define(version: 2018_08_25_150330) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -21,6 +21,7 @@ ActiveRecord::Schema.define(version: 2014_03_24_170840) do
t.string "image_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tags", default: [], array: true
end
create_table "users", force: :cascade do |t|

View File

@ -0,0 +1,17 @@
require "rails_helper"
describe TagImageJob, type: :job do
describe ".perform" do
it "tags an image" do
stub_clarifai(%w[computers technology])
image = Image.create(
image: "#{Rails.root}/spec/fixtures/spectrum.jpg",
)
TagImageJob.perform_now(image_id: image.id)
image.reload
expect(image.tags).to eq(%w[computers technology])
end
end
end

View File

@ -65,6 +65,7 @@ RSpec.configure do |config|
# config.filter_gems_from_backtrace("gem name")
config.include AuthHelpers, type: :feature
config.include ClarifaiHelpers, type: :job
end
Shoulda::Matchers.configure do |config|