Skip to content
Browse files

As an instructor I should be able to clone a scenario even if I am its

owner. Removing validations, updating specs and explaining to the user
why at times he can't clone some scenarios.
  • Loading branch information...
1 parent 756ea2b commit 2cf2982c13058352f990392d26f6f47113a8bcdd Carlos León committed
View
13 app/controllers/instructors/shared_scenarios_controller.rb
@@ -2,9 +2,8 @@ class Instructors::SharedScenariosController < ApplicationController
layout 'bootstrap'
- before_filter :load_scenarios, :only => :index
+ before_filter :load_scenarios
before_filter :load_scenario, :only => :clone
- #before_filter :validate_clonning, :only => :clone
def index
end
@@ -13,21 +12,15 @@ def clone
@new_scenario = @scenario.clone_for current_user
if @new_scenario.persisted?
flash[:notice] = 'Scenario successfully cloned.'
+ redirect_to instructors_scenario_path(@new_scenario)
else
flash[:error] = 'Scenario could not be clonned.'
+ render :index
end
- redirect_to instructors_shared_scenarios_path
end
private
- def validate_clonning
- if @scenario.user == current_user or @scenario.original_author == current_user
- flash[:error] = "You can't clone your own scenario."
- redirect_to instructors_shared_scenarios_path
- end
- end
-
def load_scenario
@scenario = Scenario.find(params[:id] || params[:shared_scenario_id])
end
View
11 app/models/scenario.rb
@@ -19,7 +19,6 @@ class Scenario < ActiveRecord::Base
validates_presence_of :master_scenario, :on => :create
validates :name, :presence => true, :length => {:within => 1..180}
validate :longterm_validator
- validate :user_cannot_be_same_as_original_author, :on => :create
# - Callbacks -
before_create :set_client_version
@@ -36,10 +35,6 @@ class Scenario < ActiveRecord::Base
scope :shared, where(:shared => true)
# - Instance Methods -
- def user_cannot_be_same_as_original_author
- errors.add(:user, "can't be the same as the original author") if user == original_author
- end
-
def is_a_clone?
original_author_id.present?
end
@@ -48,9 +43,9 @@ def clone_for(new_user)
unless new_user.has_role? :instructor
raise ArgumentError, "Instructor expected. Got #{new_user.role.to_s.titleize}"
end
- clon_atts = attributes.except("id", "created_at", "updated_at", "user_id", "sample", "shared")
- clon_atts.merge!(:user_id => new_user.id, :original_author_id => user.id)
- new_scenario = self.class.new clon_atts
+ clone_atts = attributes.except("id", "created_at", "updated_at", "user_id", "sample", "shared")
+ clone_atts.merge!(:user_id => new_user.id, :original_author_id => user.id)
+ new_scenario = self.class.new clone_atts
if new_scenario.save
clone_variables_for new_scenario
end
View
11 app/views/instructors/shared_scenarios/index.html.haml
@@ -1,6 +1,15 @@
= content_for :main_header do
%h1 Shared Scenarios
+- if @new_scenario && @new_scenario.errors.any?
+ .alert-message.block-message.error
+ %ul
+ - @new_scenario.errors.full_messages.each do |message|
+ - if message.match(/Master scenario can't be blank/)
+ %li This scenario can't be cloned because it doesn't have a master scenario.
+ - else
+ %li= message
+
%table.zebra-striped
%thead
%tr
@@ -25,4 +34,4 @@
= link_to 'Download', scenario.client_version.url, :class => 'btn primary'
- else
Not available for download
- %td= link_to 'Clone', instructors_shared_scenario_clone_path(scenario), :class => 'btn success'
+ %td= link_to 'Clone', instructors_shared_scenario_clone_path(scenario), :confirm => 'Are you sure?', :class => 'btn success'
View
7 spec/models/scenario_spec.rb
@@ -67,13 +67,6 @@
new_scenario.should_not be_valid
end
- it "should not perform clonation when the given user is the owner" do
- scenario.user = instructor
- new_scenario = scenario.clone_for instructor
- new_scenario.should_not be_valid
- new_scenario.errors.full_messages.should include("User can't be the same as the original author")
- end
-
end
context "Validations" do

0 comments on commit 2cf2982

Please sign in to comment.
Something went wrong with that request. Please try again.