Skip to content

Commit 24d3ddd

Browse files
committed
ensure there is a default eportfolio page
fixes CNVS-14580 test plan: 1. create an eportfolio 2. click organize/manage pages 3. create a second page 4. open the eportfolio in a new tab 5. in one tab, delete the welcome page 6. in the second tab, delete the new page 7. refresh the eportfolio 8. it should still load the welcome page (having recreated it) Change-Id: I400b2d4689773a8f48d8b3bfa577982492a4bfb4 Reviewed-on: https://gerrit.instructure.com/38847 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Cameron Matheson <cameron@instructure.com> QA-Review: Amber Taniuchi <amber@instructure.com> Product-Review: Simon Williams <simon@instructure.com>
1 parent 4d84e1c commit 24d3ddd

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

app/controllers/eportfolios_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def create
4040
@portfolio = @current_user.eportfolios.build(params[:eportfolio])
4141
respond_to do |format|
4242
if @portfolio.save
43-
@portfolio.setup_defaults
43+
@portfolio.ensure_defaults
4444
flash[:notice] = t('notices.created', "Porfolio successfully created")
4545
format.html { redirect_to eportfolio_url(@portfolio) }
4646
format.json { render :json => @portfolio.as_json(:permissions => {:user => @current_user, :session => session}) }
@@ -60,8 +60,8 @@ def show
6060
session[:permissions_key] = CanvasUUID.generate
6161
end
6262
if authorized_action(@portfolio, @current_user, :read)
63-
@category = @portfolio.eportfolio_categories.first rescue nil
64-
@category ||= @portfolio.setup_defaults
63+
@portfolio.ensure_defaults
64+
@category = @portfolio.eportfolio_categories.first
6565
@page = @category.eportfolio_entries.first
6666
@owner_view = @portfolio.user == @current_user && params[:view] != 'preview'
6767
if @owner_view
@@ -89,7 +89,7 @@ def update
8989
if authorized_action(@portfolio, @current_user, :update)
9090
respond_to do |format|
9191
if @portfolio.update_attributes(params[:eportfolio])
92-
@portfolio.setup_defaults
92+
@portfolio.ensure_defaults
9393
flash[:notice] = t('notices.updated', "Porfolio successfully updated")
9494
format.html { redirect_to eportfolio_url(@portfolio) }
9595
format.json { render :json => @portfolio.as_json(:permissions => {:user => @current_user, :session => session}) }

app/models/eportfolio.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ def assign_uuid
6565
can :read
6666
end
6767

68-
def setup_defaults
69-
cat = self.eportfolio_categories.create(:name => t(:first_category, "Home")) if self.eportfolio_categories.empty?
68+
def ensure_defaults
69+
cat = self.eportfolio_categories.first
70+
cat ||= self.eportfolio_categories.create!(:name => t(:first_category, "Home"))
7071
if cat && cat.eportfolio_entries.empty?
7172
entry = cat.eportfolio_entries.build(:eportfolio => self, :name => t('first_entry.title', "Welcome"))
7273
entry.content = t('first_entry.content', "Nothing entered yet")
73-
entry.save
74+
entry.save!
7475
end
7576
cat
7677
end

lib/eportfolio_page.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
module EportfolioPage
2020
def eportfolio_page_attributes
21-
@portfolio.setup_defaults
2221
@categories = @portfolio.eportfolio_categories
2322
if @portfolio.grants_right?(@current_user, session, :manage)
2423
@recent_submissions = @current_user.submissions.order("created_at DESC").all if @current_user && @current_user == @portfolio.user
@@ -49,4 +48,4 @@ def eportfolio_page_attributes
4948
end
5049
end
5150

52-
end
51+
end

spec/controllers/eportfolios_controller_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ def category_entry
9494
response.should be_success
9595
assigns[:portfolio].should_not be_nil
9696
end
97+
98+
it "should create a category if one doesn't exist" do
99+
user_session(@user)
100+
get 'show', :id => @portfolio.id
101+
response.should be_success
102+
assigns[:category].should_not be_nil
103+
end
104+
105+
it "should create an entry in the first category if one doesn't exist" do
106+
@portfolio.eportfolio_categories.create!(:name => "Home")
107+
user_session(@user)
108+
get 'show', :id => @portfolio.id
109+
response.should be_success
110+
assigns[:page].should_not be_nil
111+
end
97112
end
98113

99114
describe "PUT 'update'" do

spec/models/eportfolio_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,22 @@
1919
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2020

2121
describe Eportfolio do
22+
describe "#ensure_defaults" do
23+
before(:once) do
24+
eportfolio
25+
end
26+
27+
it "should create a category if one doesn't exist" do
28+
@portfolio.eportfolio_categories.should be_empty
29+
@portfolio.ensure_defaults
30+
@portfolio.reload.eportfolio_categories.should_not be_empty
31+
end
32+
33+
it "should create an entry in the first category if one doesn't exist" do
34+
@category = @portfolio.eportfolio_categories.create!(:name => "Hi")
35+
@category.eportfolio_entries.should be_empty
36+
@portfolio.ensure_defaults
37+
@category.reload.eportfolio_entries.should_not be_empty
38+
end
39+
end
2240
end

0 commit comments

Comments
 (0)