Skip to content

Commit 89cf462

Browse files
committedJun 26, 2014
Merge pull request coderwall#17 from drewblas/wip_45
Require a skill to be set on the user profile before allowing a Protip.
2 parents 78bf4e5 + d4c7eb5 commit 89cf462

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
 

‎app/controllers/protips_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class ProtipsController < ApplicationController
22

33
before_filter :access_required, only: [:new, :create, :edit, :update, :destroy, :me]
4+
before_filter :require_skills_first, only: [:new, :create]
45
before_filter :lookup_protip, only: [:show, :edit, :update, :destroy, :upvote, :tag, :flag, :queue, :feature, :delete_tag]
56
before_filter :reformat_tags, only: [:create, :update]
67
before_filter :verify_ownership, only: [:edit, :update, :destroy]
@@ -556,4 +557,11 @@ def get_topics_from_protips(protips)
556557
topics = protips.map(&:tags).flatten.uniq.first(8) if topics.blank? && protips.present?
557558
topics
558559
end
560+
561+
def require_skills_first
562+
if current_user.skills.empty?
563+
flash[:error] = "Please improve your profile by adding some skills before posting Pro Tips"
564+
redirect_to badge_path(username: current_user.username, anchor: 'add-skill')
565+
end
566+
end
559567
end

‎app/views/users/show.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
= @user.protips.count > 1 ? 'Pro Tip'.pluralize : 'Pro Tip'
3333
.recent-pro-tip
3434
-if viewing_self?
35-
%a.share-a-protip.track{:href => new_protip_path, 'data-action' => 'create protip', 'data-from' => 'profile card'}
35+
%a.tip.share-a-protip.track{:href => new_protip_path, 'data-action' => 'create protip', 'data-from' => 'profile card', 'title' => @user.skills.empty? ? "Fill out your profile by adding some skills first, then share some Pro Tips!" : "Share your best coding tidbits!" }
3636
Share a Pro Tip
3737
-else
3838
%h4 Most recent Protip

‎spec/controllers/protips_controller_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,23 @@ def valid_session
3434
end
3535

3636
describe "GET new" do
37+
before { User.any_instance.stub(:skills).and_return(['skill']) } # User must have a skill to create protips
38+
3739
it "assigns a new protip as @protip" do
3840
get :new, {}, valid_session
3941
assigns(:protip).should be_a_new(Protip)
4042
end
43+
44+
it "allows viewing the page when you have a skill" do
45+
get :new, {}, valid_session
46+
response.should render_template('new')
47+
end
48+
49+
it "prevents viewing the page when you don't have a skill" do
50+
User.any_instance.stub(:skills).and_return([])
51+
get :new, {}, valid_session
52+
response.should redirect_to badge_path(username: current_user.username, anchor: 'add-skill')
53+
end
4154
end
4255

4356
describe "GET edit" do
@@ -49,6 +62,8 @@ def valid_session
4962
end
5063

5164
describe "POST create" do
65+
before { User.any_instance.stub(:skills).and_return(['skill']) } # User must have a skill to create protips
66+
5267
describe "with valid params" do
5368
it "creates a new Protip" do
5469
expect {
@@ -83,6 +98,12 @@ def valid_session
8398
response.should render_template("new")
8499
end
85100
end
101+
102+
it "prevents creating when you don't have a skill" do
103+
User.any_instance.stub(:skills).and_return([])
104+
post :create, {protip: valid_attributes}, valid_session
105+
response.should redirect_to badge_path(username: current_user.username, anchor: 'add-skill')
106+
end
86107
end
87108

88109
describe "PUT update" do

0 commit comments

Comments
 (0)