Last active
December 11, 2017 15:16
-
-
Save sushinoya/1d7b9bf3587b439ca5e445f1878443b5 to your computer and use it in GitHub Desktop.
Twitter Clone Code
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
require 'rails_helper' | |
RSpec.describe My::FollowRequestsController, type: :controller do | |
let(:sender_user) { create(:user) } | |
let(:recipient_user) { create(:user) } | |
let(:follow_request) { create(:follow_request, sender: sender_user, recipient: recipient_user) } | |
let(:status) { 'pending' } | |
describe 'GET #new' do | |
before { sign_in sender_user } | |
before { get :new, controller: 'my/follow_requests', action: 'new' } | |
context 'when user is successfully able to get the follow request page' do | |
it { expect(response).to render_template(:new) } | |
end | |
end | |
describe 'POST #create' do | |
context 'when user is successfully able to create a follow request' do | |
before { sign_in sender_user } | |
before { post :create, recipient_id: create(:user).id, sender_id: sender_user.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user is not able to create a follow request' do | |
before { sign_in recipient_user } | |
before { post :create, recipient_id: recipient_user.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:error] } | |
end | |
end | |
describe 'PUT #accept' do | |
context 'when user is successfully able to accept a follow request' do | |
before { sign_in follow_request.recipient } | |
before { put :accept, id: follow_request.id } | |
it { is_expected.to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user is not able to accept a follow request' do | |
before { sign_in follow_request.sender } | |
before { put :accept, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:error] } | |
end | |
end | |
describe 'PUT #reject' do | |
context 'when user is successfully able to reject a follow request' do | |
before { sign_in follow_request.recipient } | |
before { put :reject, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user is not able to reject a follow request' do | |
before { sign_in follow_request.sender } | |
before { put :reject, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:error] } | |
end | |
end | |
describe 'DELETE #destroy' do | |
context 'when user is successfully able to disallow a follow request' do | |
before { sign_in follow_request.recipient } | |
before { delete :destroy, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user is not able to destroy a follow request' do | |
before { sign_in follow_request.sender } | |
before { delete :destroy, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:error] } | |
end | |
end | |
describe 'DELETE #unfollow' do | |
context 'when user is successfully able to unfollow another user' do | |
before { sign_in follow_request.sender } | |
before { delete :unfollow, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user is not able to destroy a follow request' do | |
before { sign_in follow_request.recipient } | |
before { delete :unfollow, id: follow_request.id } | |
it { expect(response).to redirect_to(new_my_follow_request_path) } | |
it { is_expected.to set_flash[:error] } | |
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
class FollowRequest < ActiveRecord::Base | |
belongs_to :sender, class_name: 'User' | |
belongs_to :recipient, class_name: 'User' | |
enum status: [:pending, :accepted, :rejected] | |
scope :sent_unaccepted, -> { where(status: [statuses[:pending], statuses[:rejected]]) } | |
validates :sender, presence: true, uniqueness: { scope: [:recipient_id] } | |
validates :recipient, presence: true | |
validates :status, presence: true | |
validates :status, inclusion: { in: statuses.keys } | |
validate :compare_sender_and_recipient | |
private | |
def compare_sender_and_recipient | |
errors.add(:sender, 'You cannot send a follow request to yourself! Gosh! Get a life man.') if sender == recipient | |
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 RegistrationsController < Devise::RegistrationsController | |
private | |
def sign_up_params | |
params.require(:user).permit(:username, :name, :email, :password, :password_confirmation) | |
end | |
def account_update_params | |
params.require(:user).permit(:username, :name, :status, :email, :password, :password_confirmation, :current_password) | |
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
require 'rails_helper' | |
RSpec.describe My::StatusUpdateController, type: :controller do | |
let(:user) { create(:user) } | |
before { sign_in user } | |
describe 'PUT/PATCH #update' do | |
before { put :update, status_update_form: params } | |
context 'when user status updates successfully' do | |
let(:params) do | |
{ user: { status: 'status' } } | |
end | |
it { expect(response).to redirect_to(my_root_path) } | |
it { is_expected.to set_flash[:success] } | |
end | |
context 'when user status updates unsuccessfully' do | |
let(:params) do | |
{ user: { status: 'this string is > 50 characters long and it will not work so yeah this should fail' } } | |
end | |
it { expect(response).to redirect_to(my_root_path) } | |
it { is_expected.to set_flash[:error] } | |
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
class Tweet < ActiveRecord::Base | |
belongs_to :user, inverse_of: :tweets | |
validates :text, presence: true | |
validates :text, length: { minimum: 10, maximum: 140 } | |
validates :user, presence: true | |
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 TweetsController < ApplicationController | |
before_action :prepare_tweet, only: [:destroy] | |
def create | |
@form = TweetsForm.new(current_user, tweet_post_params[:tweet]) | |
if @form.save | |
redirect_to root_path, flash: { success: 'Successfully posted tweet' } | |
else | |
redirect_to root_path, flash: { error: @form.tweet.errors.full_messages.to_sentence } | |
end | |
end | |
def destroy | |
if @tweet.destroy | |
redirect_to root_path, flash: { success: 'Successfully deleted tweet' } | |
else | |
redirect_to root_path, flash: { error: @tweet.errors.full_messages.to_sentence } | |
end | |
end | |
private | |
def tweet_post_params | |
params.require(:tweets_form).permit(tweet: [:text]) | |
end | |
def prepare_tweet | |
@tweet = Tweet.find_by(id: params[: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 User < ActiveRecord::Base | |
devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable | |
has_many :follow_requests, foreign_key: 'sender_id' | |
has_many :requested, through: :follow_requests, source: :recipient | |
has_many :tweets, inverse_of: :user | |
validates :email, uniqueness: true | |
validates :username, uniqueness: true, presence: true | |
validates :name, presence: true | |
validates :status, length: { maximum: 40 } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment