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:
parent
442f9a1472
commit
19e61fbfd8
8
app/jobs/application_job.rb
Normal file
8
app/jobs/application_job.rb
Normal 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
10
app/jobs/tag_image_job.rb
Normal 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
|
||||
5
db/migrate/20180825150330_add_tags_to_images.rb
Normal file
5
db/migrate/20180825150330_add_tags_to_images.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class AddTagsToImages < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :images, :tags, :string, array: true, default: []
|
||||
end
|
||||
end
|
||||
@ -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|
|
||||
|
||||
17
spec/jobs/tag_image_job_spec.rb
Normal file
17
spec/jobs/tag_image_job_spec.rb
Normal 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
|
||||
@ -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|
|
||||
|
||||
Loading…
Reference in New Issue
Block a user