Skip to content

Commit

Permalink
Merge pull request #73 from idanyzheng/master
Browse files Browse the repository at this point in the history
  • Loading branch information
caok committed Aug 7, 2013
2 parents 9a1e6ee + 050c815 commit 97b2de2
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 11 deletions.
4 changes: 2 additions & 2 deletions danny/microblog_app/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

source 'http://ruby.taobao.org'
# source 'https://rubygems.org'
# source 'http://ruby.taobao.org'
source 'https://rubygems.org'

ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
Expand Down
4 changes: 2 additions & 2 deletions danny/microblog_app/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Ruby on Rails Tutorial: Microblog application
#Ruby on Rails Tutorial

##chapter 3
http://safe-plateau-5354.herokuapp.com
14 changes: 11 additions & 3 deletions danny/microblog_app/app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,15 @@ aside {
margin-bottom: 5px;
}
}



/*follow all*/
#follow_all {
ul {
float: right;
list-style: none;
li {
float: left;
margin-left: 10px;
}
}
}

13 changes: 11 additions & 2 deletions danny/microblog_app/app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def create
# format.html { redirect_to @user }
# format.js
# end
respond_with @user
respond_with @user do |format|
format.html { redirect_to @user }
end
end

def destroy
Expand All @@ -20,6 +22,13 @@ def destroy
# format.html { redirect_to @user }
# format.js
# end
respond_with @user
respond_with @user do |format|
format.html { redirect_to @user }
end
end


private


end
32 changes: 31 additions & 1 deletion danny/microblog_app/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit,:update,:index, :following, :fans]
before_action :signed_in_user, only: [:edit,
:update,
:index,
:following,
:fans,
:follow_all,
:unfollow_all]
before_action :corrent_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
before_action :no_new_create, only: [:new, :create]


def following
@title = "Following"
@user = User.find(params[:id])
Expand All @@ -18,6 +25,29 @@ def fans
render 'show_follow'
end

def follow_all
fans = User.find(params[:user][:id]).fans
current_user.follow_all! fans
flash[:success] = "Follow all is done"
respond_to do |format|
format.html { redirect_to current_user }
format.js
end

end

def unfollow_all
fans = User.find(params[:user][:id]).fans
current_user.unfollow_all! fans
flash[:success] = "unFollow all is done"
respond_to do |format|
format.html { redirect_to current_user }
format.js
end
end



def new
@user= User.new
end
Expand Down
1 change: 1 addition & 0 deletions danny/microblog_app/app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ def gravatar_for(user, options = {size: 50})
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
image_tag(gravatar_url, alt:user.name, class: "gravatar")
end

end
12 changes: 12 additions & 0 deletions danny/microblog_app/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def follow!(other_user)
self.relationships.create!(followed_id: other_user.id)
end

def follow_all!(users)
users.each do |user|
follow! user unless following? user
end
end

def unfollow_all!(users)
users.each do |user|
unfollow! user if following? user
end
end

def unfollow!(other_user)
self.relationships.find_by(followed_id: other_user.id).destroy!

Expand Down
12 changes: 12 additions & 0 deletions danny/microblog_app/app/views/users/_show_follow_all.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul id="follow_all">
<%= form_for @user, url: follow_all_user_path, method: :post do |f| %>
<div><%= f.hidden_field :id %></div>
<%= f.submit "Follow All", class: "btn btn-large btn-primary" %>
<% end %>

<%= form_for @user, url: unfollow_all_user_path, method: :post do |f| %>
<div><%= f.hidden_field :id %></div>
<%= f.submit "Unfollow All", class: "btn btn-large" %>
<% end %>
</ul>

Empty file.
5 changes: 4 additions & 1 deletion danny/microblog_app/app/views/users/show_follow.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
</section>
</aside>
<div class="span8">
<h3><%= @title %></h3>
<div>
<h3><%= @title %></h3>
</div>
<% if @users.any? %>
<%= render 'show_follow_all' if @title =="Fans" || !current_user? @user %>
<ul class="users">
<%= render @users %>
</ul>
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions danny/microblog_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
member do
get :following
get :fans
post :follow_all
post :unfollow_all
end
end
resources :sessions, only: [:new,:create,:destroy]
Expand All @@ -22,6 +24,7 @@
resources :microposts, only: [:create,:destroy]
resources :relationships, only: [:create,:destroy]


# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
34 changes: 34 additions & 0 deletions danny/microblog_app/spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@
it {should respond_to(:follow!)}
it {should respond_to(:unfollow!)}
it {should respond_to(:following?)}
###
it {should respond_to(:follow_all!)}
it {should respond_to(:unfollow_all!)}



describe "following" do
let(:other_user){FactoryGirl.create(:user)}
Expand All @@ -220,6 +225,35 @@
end
end

describe "follow all" do
let!(:other_user1){FactoryGirl.create(:user)}
let!(:other_user2){FactoryGirl.create(:user)}
let(:users){ [other_user1,other_user2] }
before do
@user.save
@user.follow_all! users
end
it {should be_following(other_user1)}
it {should be_following(other_user2)}
its(:followed_users) {should include(other_user1)}
its(:followed_users) {should include(other_user2)}
end

describe "unfollow all" do
let!(:other_user1){FactoryGirl.create(:user)}
let!(:other_user2){FactoryGirl.create(:user)}
let(:users){ [other_user1,other_user2] }
before do
@user.save
@user.follow_all! users
@user.unfollow_all! users
end
it {should_not be_following(other_user1)}
it {should_not be_following(other_user2)}
its(:followed_users) {should_not include(other_user1)}
its(:followed_users) {should_not include(other_user2)}
end

describe "relationships associations" do
let!(:user){FactoryGirl.create(:user)}
let!(:followed){FactoryGirl.create(:user)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@
specify { expect(response).to redirect_to(signin_path)}
end
end

#follow all
describe "submitting to follow all" do
# let(:params) {{ user: { id:current_user.id} }}
before { post follow_all_user_path(user) }
specify {expect(response).to redirect_to(signin_path)}
end
end

describe "for signed in user" do
Expand Down
38 changes: 38 additions & 0 deletions danny/microblog_app/spec/requests/user_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,44 @@
end
end
end
##follow all
describe "follow all" do
subject { page }
let!(:current_user) { FactoryGirl.create(:user) }
let(:viewed_user) { FactoryGirl.create(:user) }
let(:other_user1) { FactoryGirl.create(:user) }
let(:other_user2) { FactoryGirl.create(:user) }

before do
other_user1.follow! viewed_user
other_user2.follow! viewed_user
end
describe "follow all viewed user's fans" do
before do
sign_in current_user
visit fans_user_path(viewed_user)
end
it "should Increment the current user's following count" do
expect do
click_button "Follow All"
end.to change(current_user.followed_users, :count).by(2)
end
end

describe "unfollow all viewed user's fans" do
before do
current_user.follow! other_user1
current_user.follow! other_user2
sign_in current_user
visit fans_user_path(viewed_user)
end
it "should Decrement the current user's following count" do
expect do
click_button "Unfollow All"
end.to change(current_user.followed_users, :count).by(-2)
end
end
end

#profile page fans and following counts
describe "profile page" do
Expand Down

0 comments on commit 97b2de2

Please sign in to comment.