refactored the controller, moved flash message into translations

This commit is contained in:
Ilyaaaaaaaaaaaaa Zhitomirskiy 2011-07-04 16:39:28 -07:00
parent edac7601f5
commit 2d8b5bd54c
3 changed files with 34 additions and 9 deletions

View file

@ -8,10 +8,12 @@ class TagFollowingsController < ApplicationController
@tag_following = current_user.tag_followings.new(:tag_id => @tag.id) @tag_following = current_user.tag_followings.new(:tag_id => @tag.id)
if @tag_following.save if @tag_following.save
redirect_to(tag_path(:name => params[:name]), :notice => "Successfully following: #{params[:name]}" ) flash[:notice] = I18n.t('tag_followings.create.success', :name => params[:name])
else else
render :nothing => true, :status => 406 flash[:error] = I18n.t('tag_followings.create.failure', :name => params[:name])
end end
redirect_to tag_path(:name => params[:name])
end end
# DELETE /tag_followings/1 # DELETE /tag_followings/1
@ -20,9 +22,11 @@ class TagFollowingsController < ApplicationController
@tag = ActsAsTaggableOn::Tag.find_by_name(params[:name]) @tag = ActsAsTaggableOn::Tag.find_by_name(params[:name])
@tag_following = current_user.tag_followings.where(:tag_id => @tag.id).first @tag_following = current_user.tag_followings.where(:tag_id => @tag.id).first
if @tag_following && @tag_following.destroy if @tag_following && @tag_following.destroy
redirect_to(tag_path(:name => params[:name]), :notice => "Successfully stopped following: #{params[:name]}" ) flash[:notice] = I18n.t('tag_followings.destroy.success', :name => params[:name])
else else
render :nothing => true, :status => 410 flash[:error] = I18n.t('tag_followings.destroy.failure', :name => params[:name])
end end
redirect_to tag_path(:name => params[:name])
end end
end end

View file

@ -701,6 +701,14 @@ en:
follow: "Follow #%{tag}" follow: "Follow #%{tag}"
stop_following: "Stop Following #%{tag}" stop_following: "Stop Following #%{tag}"
tag_followings:
create:
success: "Successfully following: #%{name}"
failure: "Failed to follow: #%{name}"
destroy:
success: "Successfully stopped following: #%{name}"
failure: "Failed to stop following: #%{name}"
tokens: tokens:
show: show:
connect_to_cubbies: "Connect to Cubbi.es" connect_to_cubbies: "Connect to Cubbi.es"

View file

@ -41,15 +41,19 @@ describe TagFollowingsController do
}.to_not change(alice.tag_followings, :count).by(1) }.to_not change(alice.tag_followings, :count).by(1)
end end
it "redirects to the tag page" do it "redirects and flashes success to the tag page" do
post :create, valid_attributes post :create, valid_attributes
response.should redirect_to(tag_path(:name => valid_attributes[:name])) response.should redirect_to(tag_path(:name => valid_attributes[:name]))
flash[:notice].should == "Successfully following: ##{valid_attributes[:name]}"
end end
it "returns a 406 if you already have a tag" do it "redirects and flashes error if you already have a tag" do
TagFollowing.any_instance.stub(:save).and_return(false) TagFollowing.any_instance.stub(:save).and_return(false)
post :create, valid_attributes post :create, valid_attributes
response.code.should == "406"
response.should redirect_to(tag_path(:name => valid_attributes[:name]))
flash[:error].should == "Failed to follow: ##{valid_attributes[:name]}"
end end
end end
end end
@ -66,10 +70,19 @@ describe TagFollowingsController do
}.to change(TagFollowing, :count).by(-1) }.to change(TagFollowing, :count).by(-1)
end end
it "returns a 410 if you already have a tag" do it "redirects and flashes error if you already don't follow the tag" do
delete :destroy, valid_attributes
response.should redirect_to(tag_path(:name => valid_attributes[:name]))
flash[:notice].should == "Successfully stopped following: ##{valid_attributes[:name]}"
end
it "redirects and flashes error if you already don't follow the tag" do
TagFollowing.any_instance.stub(:destroy).and_return(false) TagFollowing.any_instance.stub(:destroy).and_return(false)
delete :destroy, valid_attributes delete :destroy, valid_attributes
response.code.should == "410"
response.should redirect_to(tag_path(:name => valid_attributes[:name]))
flash[:error].should == "Failed to stop following: ##{valid_attributes[:name]}"
end end
end end