Last active
December 11, 2015 21:48
-
-
Save rlivsey/4664751 to your computer and use it in GitHub Desktop.
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
class TodosController < ApplicationController | |
class TodoResponse < SimpleDelegator | |
include CommonResponses # handles failure, not_found, permission_denied etc... | |
def success(todo) | |
render json: todo, serializer: TodoSerializer | |
end | |
end | |
def create | |
uc = CreateTodo.new(params[:todo], current_user_session) | |
uc.subscribe TodoResponse.new(self) | |
uc.subscribe PusherActions.new(current_user_session) | |
uc.subscribe ... # you get the idea | |
uc.call | |
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
class Todo | |
include Virtus | |
attribute :title, String | |
attribute :done, Boolean, default: false | |
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
class Todos | |
include MongoRepository # uses Plucky for query building etc... | |
include ScopedByAccount | |
end | |
module ScopedByAccount | |
def initialize(account) | |
@account = account | |
super | |
end | |
private | |
def query(options={}) | |
super.amend(account_id: @account.id) | |
end | |
def to_mongo(obj) | |
super.merge(account_id: @account.id) | |
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
class CreateTodo | |
include PubSub | |
include ModelPresenter | |
attribute :title, :for => :todo | |
validates_presence_of :title | |
# user_session is an object containing the current user, current account etc... | |
def initialize(attrs, user_session) | |
@user_session = user_session | |
@todo = Todo.new | |
self.attributes = attrs | |
end | |
def call | |
if !permission.can_create? | |
publish :denied | |
elsif valid? | |
repo.store(@todo) | |
publish :success, @todo | |
else | |
publish :failure, errors | |
end | |
end | |
private | |
def permission | |
TodoPermissions.new(@user_session) | |
end | |
def repo | |
Todos.new(@user_session.account) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment