Skip to content

Commit

Permalink
Whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Parrish committed Feb 24, 2016
1 parent 64bb9f1 commit 7bf6aa2
Show file tree
Hide file tree
Showing 332 changed files with 2,439 additions and 2,439 deletions.
10 changes: 5 additions & 5 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box_url = 'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'
config.vm.network :forwarded_port, guest: 80, host: 3000 # puma
config.vm.network :forwarded_port, guest: 5432, host: 5433 # postgres

config.vm.provision :shell, inline: 'mkdir -p /postgres_data'

config.vm.provision 'docker' do |d|
d.pull_images 'zooniverse/postgresql'

d.run 'zooniverse/postgresql',
args: '--name pg --publish 5432:5432 --env PG_USER="talk" --env DB="talk_staging" --env PASS="talk" -v /postgres_data:/data'

d.build_image '/vagrant', args: '-t zooniverse/talk'
d.run 'talk', image: 'zooniverse/talk',
args: '--publish 80:80 --link pg:pg -v /vagrant:/rails_app --env TALK_DB_PASSWORD="talk" --env SECRET_KEY_BASE="79c13497d5c6bf783c544ad058c28469b101c9612ea607b335863573f37fe4201233b7f044ba5b5923d2e1aa709e4a0bb61f4dbb6a9b9c9dbd03ffeb21c9382d" --env RACK_ENV=staging --env RAILS_ENV=staging --env VAGRANT_APP=1'
end

config.vm.provider :virtualbox do |vb|
vb.customize ['modifyvm', :id, '--memory', '2048']
vb.customize ['modifyvm', :id, '--cpus', '4']
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ class ApplicationController < ActionController::Base
include Pundit
include ActionRendering
include ActionRescuing

before_action :set_format
before_action :enforce_ban, if: ->{ current_user }
before_action :enforce_ip_ban

def root
authorize :application, :index?
respond_to do |format|
format.html{ redirect_to FrontEnd.zooniverse_talk }
format.json{ render json: resources }
end
end

def resources
{
announcements: { href: '/announcements', type: 'announcements' },
Expand All @@ -36,21 +36,21 @@ def resources
users: { href: '/users', type: 'users' }
}
end

def set_format
request.format = :json if request.format == :json_api
end

def sinkhole
raise ActionController::RoutingError.new 'Not found'
end

def log_event!(label, payload)
EventLog.create label: label, user_id: current_user.try(:id), payload: payload
rescue
nil
end

concerning :Authentication do
def bearer_token
return @bearer_token if @bearer_token
Expand All @@ -59,21 +59,21 @@ def bearer_token
@bearer_token
end
end

concerning :CurrentUser do
def current_user
return unless bearer_token
@current_user ||= User.from_panoptes bearer_token
end

def current_user_ip
request.remote_ip
end

def enforce_ban
raise Talk::BannedUserError if current_user.banned?
end

def enforce_ip_ban
raise Talk::BannedUserError if UserIpBan.banned?(request)
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
class CommentsController < ApplicationController
include TalkResource

before_action :filter_subject_default, on: :index, if: ->{
params.has_key?(:subject_default) && params[:section]
}

def upvote
service.upvote
render json: serializer_class.resource(service.resource, nil, current_user: current_user)
end

def remove_upvote
service.remove_upvote
render json: serializer_class.resource(service.resource, nil, current_user: current_user)
end

def destroy
comment = Comment.find params[:id]
authorize comment
comment.soft_destroy
render json: { }, status: :no_content
end

protected

def filter_subject_default
board_ids = Board.where(section: params[:section], subject_default: params[:subject_default]).pluck :id
params[:board_id] = board_ids.join ','
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/concerns/action_rendering.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module ActionRendering
extend ActiveSupport::Concern

def index
authorize model_class
scoped = policy_scope model_class
params[:sort] ||= serializer_class.default_sort if serializer_class.default_sort
render json: serializer_class.page(params, scoped, current_user: current_user)
end

def show
authorize model_class.where(id: resource_ids)
render json: serializer_class.resource(params, nil, current_user: current_user)
end

def create
service.create
render json: serializer_class.resource(service.resource, nil, current_user: current_user)
end

def update
service.update
render json: serializer_class.resource(service.resource, nil, current_user: current_user)
end

def destroy
instance = model_class.find params[:id]
authorize instance
Expand Down
32 changes: 16 additions & 16 deletions app/controllers/concerns/action_rescuing.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
module ActionRescuing
extend ActiveSupport::Concern

included do
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActiveRecord::RecordInvalid, with: :bad_request
rescue_from ActiveRecord::RecordNotUnique, with: :conflict
rescue_from ActiveRecord::UnknownAttributeError, with: :unprocessable
rescue_from ActiveRecord::RecordNotDestroyed, with: :unauthorized

rescue_from ActionController::UnpermittedParameters, with: :unprocessable
rescue_from ActionController::RoutingError, with: :not_found
rescue_from ActionController::ParameterMissing, with: :unprocessable

rescue_from JSON::Schema::ValidationError, with: :unprocessable

rescue_from Pundit::AuthorizationNotPerformedError, with: :not_implemented
rescue_from Pundit::PolicyScopingNotPerformedError, with: :not_implemented
rescue_from Pundit::NotDefinedError, with: :not_implemented
rescue_from Pundit::NotAuthorizedError, with: :unauthorized

rescue_from RangeError, with: :bad_request
rescue_from RestPack::Serializer::InvalidInclude, with: :bad_request

rescue_from TalkService::ParameterError, with: :unprocessable
rescue_from Talk::InvalidParameterError, with: :unprocessable
rescue_from Talk::BannedUserError, with: :forbidden
Expand All @@ -30,7 +30,7 @@ module ActionRescuing
rescue_from OauthAccessToken::ExpiredError, with: :unauthorized
rescue_from OauthAccessToken::RevokedError, with: :unauthorized
end

module ClassMethods
def disallow(*methods)
methods.each do |method|
Expand All @@ -40,39 +40,39 @@ def disallow(*methods)
end
end
end

def unauthorized(exception)
render_exception :unauthorized, exception
end

def forbidden(exception)
render_exception :forbidden, exception
end

def not_found(exception)
render_exception :not_found, exception
end

def not_allowed(exception)
render_exception :method_not_allowed, exception
end

def bad_request(exception)
render_exception :bad_request, exception
end

def unprocessable(exception)
render_exception :unprocessable_entity, exception
end

def not_implemented(exception)
render_exception :not_implemented, exception
end

def conflict(exception)
render_exception :conflict, exception
end

def render_exception(status, exception)
self.response_body = nil
render status: status, json: { error: exception.message }
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/concerns/talk_resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TalkResource
extend ActiveSupport::Concern

included do
class_attribute :model_class, :serializer_class, :schema_class, :serializer_class, :service_class
self.model_class = name.sub(/Controller$/, '').singularize.constantize rescue nil
Expand All @@ -9,7 +9,7 @@ module TalkResource
self.service_class = "#{ model_class.name }Service".constantize rescue ApplicationService
attr_accessor :resource
end

def service
@service ||= service_class.new({
params: params,
Expand All @@ -20,7 +20,7 @@ def service
user_ip: current_user_ip
})
end

def resource_ids
if params[:id].is_a? String
params[:id].split(',').compact.collect &:to_i
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class ConversationsController < ApplicationController
include TalkResource
disallow :update

def index
authorize model_class
scoped = policy_scope model_class
scoped = scoped.unread if params.delete(:unread)
params[:sort] ||= serializer_class.default_sort if serializer_class.default_sort
render json: serializer_class.page(params, scoped, current_user: current_user)
end

def show
super
Conversation.mark_as_read_by resource_ids, current_user.id
end

def destroy
conversation = Conversation.find params[:id]
authorize conversation
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/moderations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class ModerationsController < ApplicationController
include TalkResource

before_action :invert_enums, only: [:index], if: ->{ params.has_key? :state }

def create
service.create
render json: serializer_class.resource(service.resource, nil, current_user: current_user, **sanitize_resource)
end

protected

def sanitize_resource
{ }.tap do |list|
%w(actioned_at actions created_at reports destroyed_target user_ip).each do |attr|
list[:"include_#{ attr }?"] = false
end
end
end

def invert_enums
inverted_state = Moderation.states[params[:state]]
raise Talk::InvalidParameterError.new(:state, "in #{ Moderation.states.keys }", params[:state]) unless inverted_state
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class NotificationsController < ApplicationController
include TalkResource
disallow :create, :destroy

def read
raise Pundit::NotAuthorizedError.new('not logged in') unless current_user
scoped = policy_scope model_class
Expand Down
Loading

0 comments on commit 7bf6aa2

Please sign in to comment.