From 2e3f9c9731ff508d8ef84d22017e1267d7eed734 Mon Sep 17 00:00:00 2001 From: scarvill91 Date: Tue, 28 Apr 2015 22:59:34 -0500 Subject: [PATCH] created relationships controller for following/unfollowing users --- app/assets/javascripts/relationships.coffee | 3 +++ app/assets/stylesheets/relationships.scss | 3 +++ app/controllers/relationships_controller.rb | 15 +++++++++++++++ app/helpers/relationships_helper.rb | 2 ++ .../relationships_controller_test.rb | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 app/assets/javascripts/relationships.coffee create mode 100644 app/assets/stylesheets/relationships.scss create mode 100644 app/controllers/relationships_controller.rb create mode 100644 app/helpers/relationships_helper.rb create mode 100644 test/controllers/relationships_controller_test.rb diff --git a/app/assets/javascripts/relationships.coffee b/app/assets/javascripts/relationships.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/relationships.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/relationships.scss b/app/assets/stylesheets/relationships.scss new file mode 100644 index 0000000..ca5c640 --- /dev/null +++ b/app/assets/stylesheets/relationships.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Relationships controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb new file mode 100644 index 0000000..6fc33a9 --- /dev/null +++ b/app/controllers/relationships_controller.rb @@ -0,0 +1,15 @@ +class RelationshipsController < ApplicationController + before_action :logged_in_user + + def create + user = User.find(params[:followed_id]) + current_user.follow(user) + redirect_to user + end + + def destroy + user = Relationship.find(params[:id]).followed + current_user.unfollow(user) + redirect_to user + end +end diff --git a/app/helpers/relationships_helper.rb b/app/helpers/relationships_helper.rb new file mode 100644 index 0000000..3b96a9c --- /dev/null +++ b/app/helpers/relationships_helper.rb @@ -0,0 +1,2 @@ +module RelationshipsHelper +end diff --git a/test/controllers/relationships_controller_test.rb b/test/controllers/relationships_controller_test.rb new file mode 100644 index 0000000..4f2e99a --- /dev/null +++ b/test/controllers/relationships_controller_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class RelationshipsControllerTest < ActionController::TestCase + + test "create should require logged-in user" do + assert_no_difference 'Relationship.count' do + post :create + end + assert_redirected_to login_url + end + + test "destroy should require logged-in user" do + assert_no_difference 'Relationship.count' do + delete :destroy, id: relationships(:one) + end + assert_redirected_to login_url + end +end