Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
kookster committed Jun 11, 2015
2 parents 07f3e61 + 6c7a5f2 commit c309594
Show file tree
Hide file tree
Showing 54 changed files with 425 additions and 95 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gem 'oj_mimic_json'

## Messaging
gem 'shoryuken'
gem 'announce'

## Deployment
# configuration
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ GEM
tzinfo (~> 1.1)
acts_as_list (0.7.0)
activerecord (>= 3.0)
announce (0.2.2)
ansi (1.5.0)
arel (6.0.0)
aws-sdk-core (2.0.41)
Expand Down Expand Up @@ -393,6 +394,7 @@ PLATFORMS
DEPENDENCIES
actionpack-action_caching
acts_as_list
announce
better_errors
binding_of_caller
capistrano (~> 3.2)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Api::BaseController < ApplicationController

include ApiVersioning
include HalActions
include AnnounceActions
include Roar::Rails::ControllerAdditions

# respond to hal or json, but always returns application/hal+json
Expand All @@ -17,7 +18,6 @@ class Api::BaseController < ApplicationController
allow_params :index, [:page, :per, :zoom]

cache_api_action :show

cache_api_action :index

caches_action :entrypoint, cache_path: ->(_c) { { _c: Api.version(api_version).cache_key } }
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/api/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ class Api::StoriesController < Api::BaseController

filter_resources_by :series_id, :account_id

announce_actions :create, :update, :delete, :publish, :unpublish

def publish
publish_resource.tap do |res|
authorize res
res.publish!
respond_with root_resource(res), show_options
end
end

def publish_resource
@story ||= Story.unpublished.where(id: params[:id]).first
end

def unpublish
unpublish_resource.tap do |res|
authorize res
res.unpublish!
respond_with root_resource(res), show_options
end
end

def unpublish_resource
@story ||= Story.published.where(id: params[:id]).first
end

def random
@story = Story.published.limit(1).order('RAND()').first
show
Expand Down
48 changes: 48 additions & 0 deletions app/controllers/concerns/announce_actions.rb
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 app/controllers/concerns/announce_actions/announce_filter.rb
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
2 changes: 0 additions & 2 deletions app/controllers/concerns/api_versioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ def api_versions(*versions)
self.understood_api_versions = versions.map(&:to_s)
before_filter :check_api_version
end

end

end
1 change: 0 additions & 1 deletion app/controllers/public_assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ def show
head 401
end
end

end
5 changes: 2 additions & 3 deletions app/models/concerns/public_asset.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8

require 'active_support/concern'
require 'digest/md5'
require 'openssl'

# expects underlying model to have filename, class, and id attributes
module PublicAsset
Expand All @@ -26,7 +26,7 @@ def public_url_token(options={})

str = [t,e,u,c,i,v,n,x].join("|")

Digest::MD5.hexdigest(str)
OpenSSL::Digest::MD5.hexdigest(str)
end

# assumes a route like the following
Expand Down Expand Up @@ -91,5 +91,4 @@ def filename_extension
def token_secret
ENV['PUBLIC_ASSET_SECRET']
end

end
16 changes: 7 additions & 9 deletions app/models/paged_collection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8

require 'forwardable'
require 'openssl'

class PagedCollection
extend ActiveModel::Naming
Expand Down Expand Up @@ -29,14 +30,12 @@ def initialize(items, request=nil, options=nil)
end

def cache_key
key_components = ["c"]
key_components << item_class.model_name.cache_key
key_components << items.inject([0, 0]) do |keys, i|
keys[0] = keys[0] + i.try(:id).to_i.modulo(100)
keys[1] = [keys[1], i.try(:updated_at).try(:utc).to_i].max
keys
end.flatten.join("-")

item_keys = items.inject([]) do |keys, i|
keys << i.try(:id)
keys << i.try(:updated_at).try(:utc).to_i
end
key_components = ['c', item_class.model_name.cache_key]
key_components << OpenSSL::Digest::MD5.hexdigest(item_keys.join)
ActiveSupport::Cache.expand_cache_key(key_components)
end

Expand Down Expand Up @@ -76,5 +75,4 @@ def parent
def count
items.length
end

end
22 changes: 21 additions & 1 deletion app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def self.paranoia_scope

scope :published, -> { where('`published_at` IS NOT NULL AND `network_only_at` IS NULL') }

scope :unpublished, -> { where('`published_at` IS NULL') }

scope :purchased, -> {
joins(:purchases).
select('`pieces`.*', 'COUNT(`purchases`.`id`) AS `purchase_count`').group('`pieces`.`id`')
Expand Down Expand Up @@ -118,7 +120,7 @@ def tags=(ts)
end

def self.policy_class
AccountablePolicy
StoryPolicy
end

def episode_date
Expand All @@ -131,6 +133,24 @@ def subscription_episode?
series && series.subscribable?
end

# raising exceptions here to prevent sending publish messages
# when not actually a change to be published
def publish!
if published?
raise "Story #{id} is already published."
else
update_attributes!(published_at: Time.now)
end
end

def unpublish!
if !published?
raise "Story #{id} is not published."
else
update_attributes!(published_at: nil)
end
end

def destroy
if v4?
really_destroy!
Expand Down
7 changes: 1 addition & 6 deletions app/policies/account_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class AccountPolicy < ApplicationPolicy
attr_reader :user, :account

def initialize(user, account)
@user = user
@account = account
end
alias_method :account, :record

def create?
user.present?
Expand Down
9 changes: 1 addition & 8 deletions app/policies/accountable_policy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
class AccountablePolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

class AccountablePolicy < ApplicationPolicy
def create?
update?
end
Expand Down
9 changes: 2 additions & 7 deletions app/policies/image_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class ImagePolicy
attr_reader :user, :image

def initialize(user, image)
@user = user
@image = image
end
class ImagePolicy < ApplicationPolicy
alias_method :image, :record

def create?
update?
Expand Down
7 changes: 1 addition & 6 deletions app/policies/membership_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class MembershipPolicy < ApplicationPolicy
attr_reader :user, :membership

def initialize(user, membership)
@user = user
@membership = membership
end
alias_method :membership, :record

def create?
update? || (user == membership.user && !membership.approved?)
Expand Down
7 changes: 1 addition & 6 deletions app/policies/producer_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class ProducerPolicy < ApplicationPolicy
attr_reader :user, :producer

def initialize(user, producer)
@user = user
@producer = producer
end
alias_method :producer, :record

def create?
update?
Expand Down
9 changes: 1 addition & 8 deletions app/policies/story_attribute_policy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
class StoryAttributePolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

class StoryAttributePolicy < ApplicationPolicy
def create?
update?
end
Expand Down
9 changes: 9 additions & 0 deletions app/policies/story_policy.rb
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
7 changes: 1 addition & 6 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class UserPolicy < ApplicationPolicy
attr_reader :user, :other_user

def initialize(user, other_user)
@user = user
@other_user = other_user
end
alias_method :other_user, :record

def create?
true
Expand Down
2 changes: 1 addition & 1 deletion app/representers/api/account_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api::AccountRepresenter < Api::BaseRepresenter

property :id
property :id, writeable: false
property :type
property :name
property :path
Expand Down
2 changes: 1 addition & 1 deletion app/representers/api/address_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api::AddressRepresenter < Api::BaseRepresenter

property :id
property :id, writeable: false
property :street_1
property :street_2
property :street_3
Expand Down

0 comments on commit c309594

Please sign in to comment.