diff --git a/app/controllers/eportfolios_controller.rb b/app/controllers/eportfolios_controller.rb index 9519dc74d90..a84a3891759 100644 --- a/app/controllers/eportfolios_controller.rb +++ b/app/controllers/eportfolios_controller.rb @@ -40,7 +40,7 @@ class EportfoliosController < ApplicationController @portfolio = @current_user.eportfolios.build(params[:eportfolio]) respond_to do |format| if @portfolio.save - @portfolio.setup_defaults + @portfolio.ensure_defaults flash[:notice] = t('notices.created', "Porfolio successfully created") format.html { redirect_to eportfolio_url(@portfolio) } format.json { render :json => @portfolio.as_json(:permissions => {:user => @current_user, :session => session}) } @@ -60,8 +60,8 @@ class EportfoliosController < ApplicationController session[:permissions_key] = CanvasUUID.generate end if authorized_action(@portfolio, @current_user, :read) - @category = @portfolio.eportfolio_categories.first rescue nil - @category ||= @portfolio.setup_defaults + @portfolio.ensure_defaults + @category = @portfolio.eportfolio_categories.first @page = @category.eportfolio_entries.first @owner_view = @portfolio.user == @current_user && params[:view] != 'preview' if @owner_view @@ -89,7 +89,7 @@ class EportfoliosController < ApplicationController if authorized_action(@portfolio, @current_user, :update) respond_to do |format| if @portfolio.update_attributes(params[:eportfolio]) - @portfolio.setup_defaults + @portfolio.ensure_defaults flash[:notice] = t('notices.updated', "Porfolio successfully updated") format.html { redirect_to eportfolio_url(@portfolio) } format.json { render :json => @portfolio.as_json(:permissions => {:user => @current_user, :session => session}) } diff --git a/app/models/eportfolio.rb b/app/models/eportfolio.rb index 06394ee0541..ea1dd6ac7b5 100644 --- a/app/models/eportfolio.rb +++ b/app/models/eportfolio.rb @@ -65,12 +65,13 @@ class Eportfolio < ActiveRecord::Base can :read end - def setup_defaults - cat = self.eportfolio_categories.create(:name => t(:first_category, "Home")) if self.eportfolio_categories.empty? + def ensure_defaults + cat = self.eportfolio_categories.first + cat ||= self.eportfolio_categories.create!(:name => t(:first_category, "Home")) if cat && cat.eportfolio_entries.empty? entry = cat.eportfolio_entries.build(:eportfolio => self, :name => t('first_entry.title', "Welcome")) entry.content = t('first_entry.content', "Nothing entered yet") - entry.save + entry.save! end cat end diff --git a/lib/eportfolio_page.rb b/lib/eportfolio_page.rb index c442e83d4d9..9555605a09f 100644 --- a/lib/eportfolio_page.rb +++ b/lib/eportfolio_page.rb @@ -18,7 +18,6 @@ module EportfolioPage def eportfolio_page_attributes - @portfolio.setup_defaults @categories = @portfolio.eportfolio_categories if @portfolio.grants_right?(@current_user, session, :manage) @recent_submissions = @current_user.submissions.order("created_at DESC").all if @current_user && @current_user == @portfolio.user @@ -49,4 +48,4 @@ module EportfolioPage end end -end \ No newline at end of file +end diff --git a/spec/controllers/eportfolios_controller_spec.rb b/spec/controllers/eportfolios_controller_spec.rb index 89bc62f9af7..e771a7b9781 100644 --- a/spec/controllers/eportfolios_controller_spec.rb +++ b/spec/controllers/eportfolios_controller_spec.rb @@ -94,6 +94,21 @@ describe EportfoliosController do response.should be_success assigns[:portfolio].should_not be_nil end + + it "should create a category if one doesn't exist" do + user_session(@user) + get 'show', :id => @portfolio.id + response.should be_success + assigns[:category].should_not be_nil + end + + it "should create an entry in the first category if one doesn't exist" do + @portfolio.eportfolio_categories.create!(:name => "Home") + user_session(@user) + get 'show', :id => @portfolio.id + response.should be_success + assigns[:page].should_not be_nil + end end describe "PUT 'update'" do diff --git a/spec/models/eportfolio_spec.rb b/spec/models/eportfolio_spec.rb index 0f543841aa0..290e6e27eb7 100644 --- a/spec/models/eportfolio_spec.rb +++ b/spec/models/eportfolio_spec.rb @@ -19,4 +19,22 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') describe Eportfolio do + describe "#ensure_defaults" do + before(:once) do + eportfolio + end + + it "should create a category if one doesn't exist" do + @portfolio.eportfolio_categories.should be_empty + @portfolio.ensure_defaults + @portfolio.reload.eportfolio_categories.should_not be_empty + end + + it "should create an entry in the first category if one doesn't exist" do + @category = @portfolio.eportfolio_categories.create!(:name => "Hi") + @category.eportfolio_entries.should be_empty + @portfolio.ensure_defaults + @category.reload.eportfolio_entries.should_not be_empty + end + end end