Skip to content

Commit

Permalink
Adding Follow model to represent when one user follows another and a …
Browse files Browse the repository at this point in the history
…blank FollowsController#index
  • Loading branch information
dmvt committed Oct 27, 2013
1 parent 54b4166 commit c519a55
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ rerun.txt
pickle-email-*.html
.project
config/initializers/secret_token.rb
.DS_Store
3 changes: 3 additions & 0 deletions app/assets/javascripts/follows.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/follows.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Follows controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
7 changes: 7 additions & 0 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FollowsController < ApplicationController
before_filter :authenticate_user!

def index

end
end
2 changes: 2 additions & 0 deletions app/helpers/follows_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module FollowsHelper
end
7 changes: 7 additions & 0 deletions app/models/follow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Follow < ActiveRecord::Base
belongs_to :following, :class_name => "User"
belongs_to :user

validates :following, :presence => true
validates :user, :presence => true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :lockable,
:recoverable, :rememberable, :trackable, :validatable

has_many :follows

validates :name, :presence => true
validates :username, :presence => true, :uniqueness => true
end
Empty file.
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

authenticated :user do
resources :follows
root :to => 'discover#index', :as => :user_root
end

# You can have the root of your site routed with "root"
root 'home#index'
root :to => 'home#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20131027165351_create_follows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateFollows < ActiveRecord::Migration
def change
create_table :follows do |t|
t.integer :user_id
t.integer :following_id

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131026231121) do
ActiveRecord::Schema.define(version: 20131027165351) do

create_table "follows", force: true do |t|
t.integer "user_id"
t.integer "following_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "email", default: "", null: false
Expand Down
24 changes: 24 additions & 0 deletions spec/controllers/follows_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe FollowsController do
context "when no user is logged in" do
describe "GET index" do
subject { response }
before { get :index }

it { should_not be_successful }
end
end

context "when a user is logged in" do
describe "GET index" do
subject { response }
before do
sign_in FactoryGirl.create(:user)
get :index
end

it { should be_successful }
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/follows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :follow do
user { FactoryGirl.create(:user) }
following { FactoryGirl.create(:user) }
end
end
14 changes: 14 additions & 0 deletions spec/helpers/follows_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the FollowsHelper. For example:
#
# describe FollowsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
describe FollowsHelper do
end
21 changes: 21 additions & 0 deletions spec/models/follow_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Follow do
context "associations" do
it { should belong_to :user }
it { should belong_to :following }
end

context "factories" do
describe "#follow" do
subject { FactoryGirl.build(:follow) }

it { should be_valid }
end
end

context "validations" do
it { should validate_presence_of :following }
it { should validate_presence_of :user }
end
end
4 changes: 4 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'spec_helper'

describe User do
context "associations" do
it { should have_many :follows }
end

context "factories" do
describe "#user" do
subject { FactoryGirl.build(:user) }
Expand Down
4 changes: 4 additions & 0 deletions spec/support/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# see: https://github.com/plataformatec/devise#test-helpers
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end

0 comments on commit c519a55

Please sign in to comment.