-
-
Save kurko/2407768 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# A controller | |
class PostsController < ActionController::Base | |
def create | |
@context = PostManagementContext.new(params[:posts]) | |
if @context.save | |
# redirect | |
end | |
end | |
def search | |
# ... | |
end | |
# ... | |
end | |
# Context class | |
# | |
# Could be Context::PostManagement. Whatever now. | |
class PostManagementContext | |
def initialize(params) | |
@params = params | |
end | |
def save | |
@post = Post.new(@params) | |
if @post.save | |
# @post is probably ActiveRecord, so there's nothing to extend | |
@twitter = login_twitter | |
@twitter.extend TwitterPoster | |
@twitter.post(@post.body) | |
end | |
end | |
def save_comment | |
@comment = Comment.new(@params) | |
# ... instantiates Model | |
if @comment.save | |
@twitter = login_twitter | |
@twitter.extend TwitterFollower | |
@twitter.follow(@comment.twitter_nickname) | |
end | |
end | |
private | |
def login_twitter | |
Twitter.new(login: "login", password: "password") | |
end | |
end | |
# Data object | |
class Twitter | |
def initialize(user) | |
@twitter = Twitter::API.login(user[:login], user[:password]) # fake API class | |
end | |
end | |
# Roles | |
module TwitterPoster | |
def post(body) | |
# calls Twitter API | |
end | |
def parse_tweet_body | |
# ... | |
end | |
# other methods related to posting on Twitter | |
end | |
module TwitterFollower | |
def follow(body) | |
# calls Twitter API | |
end | |
# other methods related to following on Twitter | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment