diff --git a/Gemfile b/Gemfile index e41d703ba..669483af1 100755 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem "database_cleaner" gem "rollbar" gem "rack-attack" gem "paperclip", "~> 5.0.0" +gem 'delayed_job_active_record' group :development, :test do gem "sqlite3" diff --git a/Gemfile.lock b/Gemfile.lock index 9b71ceb80..005c7a0bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,11 @@ GEM concurrent-ruby (1.0.2) daemons (1.2.4) database_cleaner (1.5.3) + delayed_job (4.1.2) + activesupport (>= 3.0, < 5.1) + delayed_job_active_record (4.1.1) + activerecord (>= 3.0, < 5.1) + delayed_job (>= 3.0, < 5) devise (4.2.0) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -211,6 +216,7 @@ PLATFORMS DEPENDENCIES active_model_serializers (~> 0.8.3) database_cleaner + delayed_job_active_record devise factory_girl_rails faker diff --git a/app/models/device.rb b/app/models/device.rb index 4058e7087..2eadadd78 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -10,6 +10,7 @@ class Device < ActiveRecord::Base has_many :peripherals, dependent: :destroy has_many :tool_bays, dependent: :destroy has_many :tools, dependent: :destroy + has_many :images, dependent: :destroy has_one :planting_area, dependent: :destroy validates :name, uniqueness: true diff --git a/app/models/image.rb b/app/models/image.rb index 32525da5c..07a75b5dc 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -1,4 +1,9 @@ +require "open-uri" + class Image < ApplicationRecord + belongs_to :device + validates :device, presence: true + has_attached_file :attachment, # default_url: "/images/:style/missing.png", styles: { x1280: "1280x1280>", @@ -7,8 +12,16 @@ class Image < ApplicationRecord x160: "160x160>", x80: "80x80>" }, size: { in: 0..5.megabytes } # Worst case scenario for 1280x1280 BMP. - validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/ - validates_attachment_file_name :attachment, matches: [/png\z/, - /jpe?g\z/, - /bmp\z/] + validates_attachment_content_type :attachment, + content_type: ["image/jpg", + "image/jpeg", + "image/png", + "image/gif"] + def set_attachment_by_url(url) + # Image.new.from_url("http://i.imgur.com/OhLresv.png").save! + self.attachment = open(url) + self.image_processed_at = Time.now + self + end + handle_asynchronously :set_attachment_by_url end diff --git a/bin/delayed_job b/bin/delayed_job new file mode 100755 index 000000000..edf195985 --- /dev/null +++ b/bin/delayed_job @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize diff --git a/config/application.rb b/config/application.rb index 24e4a08fd..a321e1d2d 100755 --- a/config/application.rb +++ b/config/application.rb @@ -7,18 +7,7 @@ Bundler.require(:default, Rails.env) module FarmBot class Application < Rails::Application - # Settings in config/environments/* take precedence over those specified - # here. Application configuration should go into files in - # config/initializers - # -- all .rb files in that directory are automatically loaded. - - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. - # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. - # config.time_zone = 'Central Time (US & Canada)' - - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. - # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] - # config.i18n.default_locale = :de + config.active_job.queue_adapter = :delayed_job config.action_dispatch.perform_deep_munge = false I18n.enforce_available_locales = false config.generators do |g| diff --git a/db/migrate/20170111035209_create_images.rb b/db/migrate/20170111035209_create_images.rb index 6e3cbb719..45d62970f 100644 --- a/db/migrate/20170111035209_create_images.rb +++ b/db/migrate/20170111035209_create_images.rb @@ -1,8 +1,12 @@ class CreateImages < ActiveRecord::Migration[5.0] def change create_table :images do |t| + t.integer :device_id + t.datetime :image_processed_at t.timestamps end + + add_index :images, :device_id add_attachment :images, :attachment end end diff --git a/db/schema.rb b/db/schema.rb index f985eff48..5b218d121 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -35,12 +35,15 @@ ActiveRecord::Schema.define(version: 20170111035209) do end create_table "images", force: :cascade do |t| + t.integer "device_id" + t.datetime "image_processed_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "attachment_file_name" t.string "attachment_content_type" t.integer "attachment_file_size" t.datetime "attachment_updated_at" + t.index ["device_id"], name: "index_images_on_device_id" end create_table "logs", force: :cascade do |t| diff --git a/spec/models/image_spec.rb b/spec/models/image_spec.rb index 92cd7ee27..ad0213267 100644 --- a/spec/models/image_spec.rb +++ b/spec/models/image_spec.rb @@ -1,5 +1,10 @@ require 'spec_helper' -RSpec.describe Image, type: :model do - pending "add some examples to (or delete) #{__FILE__}" +describe Image do + let(:device) { FactoryGirl.create(:device) } + + it 'adds URL attachments' do + image = Image.new + binding.pry + end end