Feature: ActiveJob Integration and Documentation (#817)

* Add note on Active Job integration for #663

* Implement GlobalID by delegating to object and add documentation.

* Update readme and add missing require statement.

* Actually include the module.

* Create post instead of assuming it exists.

* Add test for #to_global_id.
This commit is contained in:
Cliff Braton 2017-09-11 09:35:14 -07:00 committed by GitHub
parent cfe4d33733
commit 815c4186dd
7 changed files with 61 additions and 2 deletions

View File

@ -618,6 +618,15 @@ is included in `ActiveRecord::Base` and `Mongoid::Document` by default. If
you're using another ORM, or want to decorate plain old Ruby objects,
you can include this module manually.
### Active Job Integration
[Active Job](http://edgeguides.rubyonrails.org/active_job_basics.html) allows you to pass ActiveRecord
objects to background tasks directly and performs the necessary serialization and deserialization. In
order to do this, arguments to a background job must implement [Global ID](https://github.com/rails/globalid).
Decorated objects implement Global ID by delegating to the object they are decorating. This means
you can pass decorated objects to background jobs, however, the object won't be decorated when it is
deserialized.
## Contributors
Draper was conceived by Jeff Casimir and heavily refined by Steve Klabnik and a

View File

@ -0,0 +1,22 @@
module Draper
module Compatibility
# [Active Job](http://edgeguides.rubyonrails.org/active_job_basics.html) allows you to pass
# ActiveRecord objects to background tasks directly and performs the necessary serialization
# and deserialization. In order to do this, arguments to a background job must implement
# [Global ID](https://github.com/rails/globalid).
#
# This compatibility patch implements Global ID for decorated objects by delegating to the object
# that is decorated. This means you can pass decorated objects to background jobs, but
# the object won't be decorated when it is deserialized. This patch is meant as an intermediate
# fix until we can find a way to deserialize the decorated object correctly.
module GlobalID
extend ActiveSupport::Concern
included do
include ::GlobalID::Identification
delegate :to_global_id, :to_signed_global_id, to: :object
end
end
end
end

View File

@ -1,6 +1,9 @@
require 'draper/compatibility/global_id'
module Draper
class Decorator
include Draper::ViewHelpers
include Draper::Compatibility::GlobalID if defined?(GlobalID)
extend Draper::Delegation
include ActiveModel::Serialization

View File

@ -0,0 +1,7 @@
class PublishPostJob < ActiveJob::Base
queue_as :default
def perform(post)
post.save!
end
end

View File

@ -28,4 +28,6 @@ Dummy::Application.configure do
config.active_support.deprecation = :stderr
config.eager_load = false
config.active_job.queue_adapter = :test
end

View File

@ -0,0 +1,9 @@
RSpec.describe PublishPostJob, type: :job do
let(:post) { Post.create.decorate }
subject(:job) { described_class.perform_later(post) }
it 'queues the job' do
expect { job }.to have_enqueued_job(described_class).with(post.object)
end
end

View File

@ -1,8 +1,15 @@
require 'spec_helper'
require 'shared_examples/decoratable'
describe Post do
it_behaves_like "a decoratable model"
RSpec.describe Post do
it_behaves_like 'a decoratable model'
it { should be_a ApplicationRecord }
describe '#to_global_id' do
let(:post) { Post.create }
subject { post.to_global_id }
it { is_expected.to eq post.decorate.to_global_id }
end
end