mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
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:
parent
cfe4d33733
commit
815c4186dd
7 changed files with 61 additions and 2 deletions
|
@ -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
|
||||
|
|
22
lib/draper/compatibility/global_id.rb
Normal file
22
lib/draper/compatibility/global_id.rb
Normal 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
|
|
@ -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
|
||||
|
|
7
spec/dummy/app/jobs/publish_post_job.rb
Normal file
7
spec/dummy/app/jobs/publish_post_job.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class PublishPostJob < ActiveJob::Base
|
||||
queue_as :default
|
||||
|
||||
def perform(post)
|
||||
post.save!
|
||||
end
|
||||
end
|
|
@ -28,4 +28,6 @@ Dummy::Application.configure do
|
|||
config.active_support.deprecation = :stderr
|
||||
|
||||
config.eager_load = false
|
||||
|
||||
config.active_job.queue_adapter = :test
|
||||
end
|
||||
|
|
9
spec/dummy/spec/jobs/publish_post_job_spec.rb
Normal file
9
spec/dummy/spec/jobs/publish_post_job_spec.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue