-
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
54 changed files
with
425 additions
and
95 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
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
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,9 @@ | ||
class StoryPolicy < AccountablePolicy | ||
def publish? | ||
update? | ||
end | ||
|
||
def unpublish? | ||
update? | ||
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
Oops, something went wrong.