diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 0000000..963815f --- /dev/null +++ b/app/jobs/application_job.rb @@ -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 diff --git a/app/jobs/tag_image_job.rb b/app/jobs/tag_image_job.rb new file mode 100644 index 0000000..ac84935 --- /dev/null +++ b/app/jobs/tag_image_job.rb @@ -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 diff --git a/db/migrate/20180825150330_add_tags_to_images.rb b/db/migrate/20180825150330_add_tags_to_images.rb new file mode 100644 index 0000000..4f88136 --- /dev/null +++ b/db/migrate/20180825150330_add_tags_to_images.rb @@ -0,0 +1,5 @@ +class AddTagsToImages < ActiveRecord::Migration[5.2] + def change + add_column :images, :tags, :string, array: true, default: [] + end +end diff --git a/db/schema.rb b/db/schema.rb index 1262dd0..231143d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/jobs/tag_image_job_spec.rb b/spec/jobs/tag_image_job_spec.rb new file mode 100644 index 0000000..2244725 --- /dev/null +++ b/spec/jobs/tag_image_job_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 92feb71..f5522e0 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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|