-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
334 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ gem 'oj_mimic_json' | |
|
||
## Messaging | ||
gem 'shoryuken' | ||
gem 'announce' | ||
|
||
## Deployment | ||
# configuration | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# encoding: utf-8 | ||
|
||
require 'active_support/concern' | ||
require 'announce_actions/announce_filter' | ||
|
||
# expects underlying model to have filename, class, and id attributes | ||
module AnnounceActions | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
attr_accessor :announced_actions | ||
|
||
def announce_actions(*args) | ||
self.announced_actions ||= [] | ||
|
||
options = args.extract_options! | ||
|
||
actions = args.map(&:to_s).uniq | ||
actions = [:create, :update, :destroy] if actions.empty? | ||
|
||
actions.each do |action| | ||
next if announced_actions.include?(action) | ||
|
||
add_announce_filter(action, options) | ||
|
||
# remember this action already announcing, prevent dupes | ||
self.announced_actions << action | ||
end | ||
end | ||
|
||
def add_announce_filter(action, options) | ||
# when it is a destroy action, default action name to 'delete' | ||
announce_filter = new_announce_filter(action, options) | ||
|
||
# default callback options for only this action, and only on success | ||
default_options = { only: [action], if: ->() { response.successful? } } | ||
callback_options = options.slice(:only, :except, :if, :unless) | ||
|
||
after_action announce_filter, default_options.merge(callback_options) | ||
end | ||
|
||
def new_announce_filter(action, options) | ||
filter_options = options.slice(:action, :decorator) | ||
filter_options[:action] ||= 'delete' if action.to_s == 'destroy' | ||
AnnounceActions::AnnounceFilter.new(action, filter_options) | ||
end | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
app/controllers/concerns/announce_actions/announce_filter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# encoding: utf-8 | ||
|
||
require 'announce' | ||
|
||
# expects underlying model to have filename, class, and id attributes | ||
module AnnounceActions | ||
|
||
class AnnounceFilter | ||
include Announce::Publisher | ||
|
||
attr_accessor :action, :options | ||
|
||
def initialize(action, options = {}) | ||
@action = action | ||
@options = options | ||
end | ||
|
||
def after(controller) | ||
subject = controller.controller_name.singularize | ||
announce(subject, resource_action, decorated_resource(controller)) | ||
end | ||
|
||
def resource_action | ||
(options[:action] || action).to_s | ||
end | ||
|
||
def decorated_resource(controller) | ||
decorator = decorator_class(controller) | ||
raise "No decorator specified: #{controller.inspect}" unless decorator | ||
res = announce_resource(action, controller) | ||
decorator.new(res).to_json | ||
end | ||
|
||
def announce_resource(action, controller) | ||
pre = "#{action}_" if controller.respond_to?("#{action}_resource", true) | ||
controller.send("#{pre}resource") | ||
end | ||
|
||
def decorator_class(controller) | ||
return options[:decorator] if options[:decorator] | ||
resource_class = controller.controller_name.singularize.camelize | ||
"Api::Min::#{resource_class}Representer".safe_constantize || | ||
"Api::#{resource_class}Representer".safe_constantize | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,4 @@ def show | |
head 401 | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,5 +43,4 @@ def cache_key_class_name | |
def cache_options | ||
{compress: true, race_condition_ttl: 10, expires_in: 1.hour} | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Configuration file that defines the settings for pub/sub of events | ||
app_name: cms | ||
|
||
publish: | ||
story: | ||
- create | ||
- update | ||
- delete | ||
- publish | ||
- unpublish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Announce::configure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'shoryuken' | ||
|
||
Shoryuken::EnvironmentLoader.load(config_file: (Rails.root + 'config' + 'shoryuken.yml')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
aws: | ||
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> | ||
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> | ||
region: <%= ENV['AWS_REGION'] %> | ||
account_id: <%= ENV['AWS_ACCOUNT_ID'] %> | ||
concurrency: 25 # The number of allocated threads to process messages. Default 25 | ||
delay: 30 # The delay in seconds to pause a queue when it's empty. Default 0 |
48 changes: 48 additions & 0 deletions
48
test/controllers/concerns/announce_actions/announce_filter_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# encoding: utf-8 | ||
|
||
require 'announce_actions' | ||
|
||
describe AnnounceActions::AnnounceFilter do | ||
|
||
let(:controller_class) { Api::TestObjectsController } | ||
|
||
let(:controller) { controller_class.new } | ||
|
||
let(:filter) { AnnounceActions::AnnounceFilter.new('create', {}) } | ||
|
||
before do | ||
controller_class.class_eval do | ||
include AnnounceActions | ||
announce_actions | ||
end | ||
|
||
clear_messages | ||
end | ||
|
||
it 'is created with an action and a options' do | ||
filter.action.must_equal('create') | ||
filter.options.must_equal({}) | ||
end | ||
|
||
it 'defaults to the min decorator when possible' do | ||
dc = filter.decorator_class(controller) | ||
dc.must_equal Api::Min::TestObjectRepresenter | ||
end | ||
|
||
it 'can use an optional decorator' do | ||
filter.options[:decorator] = Api::TestObjectRepresenter | ||
dc = filter.decorator_class(controller) | ||
dc.must_equal Api::TestObjectRepresenter | ||
end | ||
|
||
it 'can use an optional action name' do | ||
filter.resource_action.must_equal('create') | ||
filter.options[:action] = 'foo' | ||
filter.resource_action.must_equal('foo') | ||
end | ||
|
||
it 'tries the action specific resource method' do | ||
def controller.foo_resource; 'foo'; end | ||
filter.announce_resource('foo', controller).must_equal 'foo' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# encoding: utf-8 | ||
|
||
require 'announce_actions' | ||
|
||
describe Api::TestObjectsController do | ||
|
||
let (:controller_class) { Api::TestObjectsController } | ||
|
||
before do | ||
controller_class.class_eval { include AnnounceActions } | ||
controller_class.announced_actions = [] | ||
clear_messages | ||
end | ||
|
||
it 'can declare announced actions' do | ||
controller_class.announce_actions(:destroy) | ||
controller_class.announced_actions.size.must_equal 1 | ||
end | ||
|
||
it 'prevents dupe announcements' do | ||
controller_class.announce_actions(:destroy) | ||
controller_class.announce_actions(:destroy, :update) | ||
controller_class.announced_actions.size.must_equal 2 | ||
end | ||
|
||
it 'defaults to create, update, and destroy' do | ||
default_actions = [:create, :destroy, :update] | ||
controller_class.announce_actions | ||
controller_class.announced_actions.sort.must_equal default_actions | ||
end | ||
|
||
describe 'test callbacks' do | ||
|
||
before { define_routes } | ||
after { Rails.application.reload_routes! } | ||
|
||
it 'will call announce' do | ||
controller_class.announce_actions(:create) | ||
|
||
post :create, { title: 'foo' }.to_json | ||
|
||
response.must_be :success? | ||
last_message['subject'].to_s.must_equal 'test_object' | ||
last_message['action'].to_s.must_equal 'create' | ||
end | ||
|
||
it 'renames destroy action to delete' do | ||
controller_class.announce_actions(:destroy) | ||
|
||
delete :destroy, id: 1 | ||
|
||
response.must_be :success? | ||
last_message['subject'].to_s.must_equal 'test_object' | ||
last_message['action'].to_s.must_equal 'delete' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# encoding: utf-8 | ||
|
||
require 'hal_actions' | ||
|
||
describe HalActions do | ||
|
Oops, something went wrong.