Permalink
This commit does not belong to any branch on this respository, and may belong to a fork outside of the repository.
Showing
with
260 additions
and 27 deletions.
- +29 −0 app/assets/stylesheets/static_pages.css.scss
- +21 −0 app/controllers/relationships_controller.rb
- +20 −1 app/controllers/users_controller.rb
- +10 −1 app/models/micropost.rb
- +6 −0 app/models/relationship.rb
- +23 −2 app/models/user.rb
- +2 −0 app/views/relationships/create.js.erb
- +2 −0 app/views/relationships/destroy.js.erb
- +15 −0 app/views/shared/_stats.html.erb
- +3 −0 app/views/static_pages/home.html.erb
- +5 −0 app/views/users/_follow.html.erb
- +9 −0 app/views/users/_follow_form.html.erb
- +5 −0 app/views/users/_unfollow.html.erb
- +6 −0 app/views/users/show.html.erb
- +30 −0 app/views/users/show_follow.html.erb
- +6 −0 config/routes.rb
- +13 −0 db/migrate/20131118215946_create_relationships.rb
- +12 −1 db/schema.rb
- +38 −22 lib/tasks/sample_data.rake
- +5 −0 spec/models/relationship_spec.rb
@@ -0,0 +1,21 @@ | |||
class RelationshipsController < ApplicationController | |||
before_action :signed_in_user | |||
|
|||
def create | |||
@user = User.find(params[:relationship][:followed_id]) | |||
current_user.follow!(@user) | |||
respond_to do |format| | |||
format.html { redirect_to @user } | |||
format.js | |||
end | |||
end | |||
|
|||
def destroy | |||
@user = Relationship.find(params[:id]).followed | |||
current_user.unfollow!(@user) | |||
respond_to do |format| | |||
format.html { redirect_to @user } | |||
format.js | |||
end | |||
end | |||
end |
@@ -0,0 +1,6 @@ | |||
class Relationship < ActiveRecord::Base | |||
belongs_to :follower, class_name: "User" | |||
belongs_to :followed, class_name: "User" | |||
validates :follower_id, presence: true | |||
validates :followed_id, presence: true | |||
end |
@@ -0,0 +1,2 @@ | |||
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>") | |||
$("#followers").html('<%= @user.followers.count %>') |
@@ -0,0 +1,2 @@ | |||
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>") | |||
$("#followers").html('<%= @user.followers.count %>') |
@@ -0,0 +1,15 @@ | |||
<% @user ||= current_user %> | |||
<div class="stats"> | |||
<a href="<%= following_user_path(@user) %>"> | |||
<strong id="following" class="stat"> | |||
<%= @user.followed_users.count %> | |||
</strong> | |||
following | |||
</a> | |||
<a href="<%= followers_user_path(@user) %>"> | |||
<strong id="followers" class="stat"> | |||
<%= @user.followers.count %> | |||
</strong> | |||
followers | |||
</a> | |||
</div> |
@@ -0,0 +1,5 @@ | |||
<%= form_for(current_user.relationships.build(followed_id: @user.id), | |||
remote: true) do |f| %> | |||
<div><%= f.hidden_field :followed_id %></div> | |||
<%= f.submit "Follow", class: "btn btn-large btn-primary" %> | |||
<% end %> |
@@ -0,0 +1,9 @@ | |||
<% unless current_user?(@user) %> | |||
<div id="follow_form"> | |||
<% if current_user.following?(@user) %> | |||
<%= render 'unfollow' %> | |||
<% else %> | |||
<%= render 'follow' %> | |||
<% end %> | |||
</div> | |||
<% end %> |
@@ -0,0 +1,5 @@ | |||
<%= form_for(current_user.relationships.find_by(followed_id: @user), | |||
html: { method: :delete }, | |||
remote: true) do |f| %> | |||
<%= f.submit "Unfollow", class: "btn btn-large" %> | |||
<% end %> |
@@ -0,0 +1,30 @@ | |||
<% provide(:title, @title) %> | |||
<div class="row"> | |||
<aside class="span4"> | |||
<section> | |||
<%= gravatar_for @user %> | |||
<h1><%= @user.name %></h1> | |||
<span><%= link_to "view my profile", @user %></span> | |||
<span><b>Microposts:</b> <%= @user.microposts.count %></span> | |||
</section> | |||
<section> | |||
<%= render 'shared/stats' %> | |||
<% if @users.any? %> | |||
<div class="user_avatars"> | |||
<% @users.each do |user| %> | |||
<%= link_to gravatar_for(user, size: 30), user %> | |||
<% end %> | |||
</div> | |||
<% end %> | |||
</section> | |||
</aside> | |||
<div class="span8"> | |||
<h3><%= @title %></h3> | |||
<% if @users.any? %> | |||
<ul class="users"> | |||
<%= render @users %> | |||
</ul> | |||
<%= will_paginate %> | |||
<% end %> | |||
</div> | |||
</div> |
@@ -0,0 +1,13 @@ | |||
class CreateRelationships < ActiveRecord::Migration | |||
def change | |||
create_table :relationships do |t| | |||
t.integer :follower_id | |||
t.integer :followed_id | |||
|
|||
t.timestamps | |||
end | |||
add_index :relationships, :follower_id | |||
add_index :relationships, :followed_id | |||
add_index :relationships, [:follower_id, :followed_id], unique: true | |||
end | |||
end |
@@ -1,26 +1,42 @@ | |||
namespace :db do | namespace :db do | ||
|
|||
desc "Fill database with sample data" | desc "Fill database with sample data" | ||
task populate: :environment do | task populate: :environment do | ||
|
make_users | ||
User.create!(name: "Example User", | make_microposts | ||
email: "example@railstutorial.org", | make_relationships | ||
password: "foobar", | end | ||
password_confirmation: "foobar") | end | ||
99.times do |n| |
|
||
name = Faker::Name.name | def make_users | ||
email = "example-#{n+1}@railstutorial.org" | admin = User.create!(name: "Example User", | ||
password = "password" | email: "example@railstutorial.org", | ||
User.create!(name: name, | password: "foobar", | ||
email: email, | password_confirmation: "foobar", | ||
password: password, | admin: true) | ||
password_confirmation: password) | 99.times do |n| | ||
end | name = Faker::Name.name | ||
users = User.all(limit: 6) | email = "example-#{n+1}@railstutorial.org" | ||
50.times do | password = "password" | ||
content = Faker::Lorem.sentence(5) | User.create!(name: name, | ||
users.each { |user| user.microposts.create!(content: content) } | email: email, | ||
end | password: password, | ||
end | password_confirmation: password) | ||
end | |||
end | end | ||
|
|
||
def make_microposts | |||
users = User.all(limit: 6) | |||
50.times do | |||
content = Faker::Lorem.sentence(5) | |||
users.each { |user| user.microposts.create!(content: content) } | |||
end | |||
end | |||
|
|||
def make_relationships | |||
users = User.all | |||
user = users.first | |||
followed_users = users[2..50] | |||
followers = users[3..40] | |||
followed_users.each { |followed| user.follow!(followed) } | |||
followers.each { |follower| follower.follow!(user) } | |||
end |
@@ -0,0 +1,5 @@ | |||
require 'spec_helper' | |||
|
|||
describe Relationship do | |||
pending "add some examples to (or delete) #{__FILE__}" | |||
end |