diff --git a/app/controllers/program_entries_controller.rb b/app/controllers/program_entries_controller.rb index 6985726..866e4f8 100644 --- a/app/controllers/program_entries_controller.rb +++ b/app/controllers/program_entries_controller.rb @@ -40,6 +40,16 @@ def edit end end + def edit_location + @program_entry = ProgramEntry.find(params[:id]) + @program=@program_entry.program + respond_to do |format| + format.html # edit.html.erb + format.json { render json: @program_entry } + format.js + end + end + def create @program_entry = ProgramEntry.new(params[:program_entry]) @program=@program_entry.program @@ -74,6 +84,28 @@ def update end end + def update_location + @program_entry = ProgramEntry.find(params[:id]) + old_slot = @program_entry.slot + old_track = @program_entry.track + new_slot = params[:program_entry][:slot].to_i + new_track = params[:program_entry][:track].to_i + + @program=Program.find(@program_entry.program.id) + respond_to do |format| + if @program.switch_entries(old_slot, old_track, new_slot, new_track) + @program_entry.reload + format.html { redirect_to :controller => 'programs', :action => 'edit', :id => @program_entry.program.id } + format.json { render json: @program_entry, status: :updated, location: @program_entry } + format.js + else + format.html { render action: "edit" } + format.json { render json: @program_entry.errors, status: :unprocessable_entity } + format.js + end + end + end + def destroy @program_entry = ProgramEntry.find(params[:id]) @program = @program_entry.program diff --git a/app/models/program.rb b/app/models/program.rb index 057d35b..870bf72 100644 --- a/app/models/program.rb +++ b/app/models/program.rb @@ -38,6 +38,20 @@ def programEntryMatrix # rows=slots, cols=tracks @matrix end + def switch_entries(old_slot, old_track, new_slot, new_track) + entry_on_old_location = entry(old_slot, old_track) + entry_on_new_location = entry(new_slot, new_track) + + if entry_on_new_location + entry_on_new_location.slot, entry_on_new_location.track = old_slot,old_track + end + if entry_on_old_location + entry_on_old_location.slot, entry_on_old_location.track = new_slot, new_track + end + @matrix=nil + save + end + def maxSlot program_entries.collect{ |pe| pe.slot }.max || 0 end diff --git a/app/views/program_entries/_form_move.html.erb b/app/views/program_entries/_form_move.html.erb new file mode 100644 index 0000000..4ee2040 --- /dev/null +++ b/app/views/program_entries/_form_move.html.erb @@ -0,0 +1,18 @@ +<%= form_for(@program_entry, :url => url_for(:action => 'update_location'), remote: true) do |f| %> + +<% end %> + diff --git a/app/views/program_entries/_show_in_program_matrix.html.erb b/app/views/program_entries/_show_in_program_matrix.html.erb index 548700f..28a93eb 100644 --- a/app/views/program_entries/_show_in_program_matrix.html.erb +++ b/app/views/program_entries/_show_in_program_matrix.html.erb @@ -15,9 +15,13 @@ <% end %> <%= wikinize(program_entry.comment) %> <% if editable %> - <%= link_to "edit", {controller: 'program_entries', action: 'edit', id: program_entry.to_param}, - id: "program_entry_#{program_entry.id}", - :remote => true, :class => "ProgramMatrix_edit_link" %> + <% end %> <% end %> diff --git a/app/views/program_entries/edit_location.js.erb b/app/views/program_entries/edit_location.js.erb new file mode 100644 index 0000000..5c159cb --- /dev/null +++ b/app/views/program_entries/edit_location.js.erb @@ -0,0 +1,2 @@ +$('<%="#program_entry_#{@program_entry.id}"%>').after('<%= j render("form_move") %>'); + diff --git a/app/views/program_entries/update_location.js.erb b/app/views/program_entries/update_location.js.erb new file mode 100644 index 0000000..1c24706 --- /dev/null +++ b/app/views/program_entries/update_location.js.erb @@ -0,0 +1,7 @@ +<% if @program_entry.errors.any? -%> + $('<%="#edit_program_entry_#{@program_entry.id}"%>').html('<%= j render("form_popup") %>'); +<% else %> + $('<%="#edit_program_entry_#{@program_entry.id}"%>').remove(); + $('#ProgramMatrix').after("<%= j render 'programs/program_matrix', :program=> @program, :editable=> true %>"); + $('#ProgramMatrix').remove(); +<% end %> diff --git a/config/routes.rb b/config/routes.rb index a339d0f..eebe178 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,10 @@ resources :archived_presenters, except: [:destroy] - resources :program_entries, except: [:destroy] + resources :program_entries, except: [:destroy] do + get 'edit_location', :on => :member + put 'update_location', :on => :member + end resources :programs, except: [:destroy] do resources :program_entries, :on => :member, except: [:destroy] diff --git a/spec/models/program_entry_spec.rb b/spec/models/program_entry_spec.rb index b382f57..ec56e0f 100644 --- a/spec/models/program_entry_spec.rb +++ b/spec/models/program_entry_spec.rb @@ -3,7 +3,7 @@ describe ProgramEntry do describe "saving" do it "is possible" do - program_entry = FactoryGirl.create :program_entry + program_entry = FactoryGirl.create :program_entry ProgramEntry.first.should == program_entry end end diff --git a/spec/models/program_spec.rb b/spec/models/program_spec.rb index ad9c926..7e36eff 100644 --- a/spec/models/program_spec.rb +++ b/spec/models/program_spec.rb @@ -420,6 +420,50 @@ def a_program_entry_for(program, session) end end + describe "switch_entries" do + it "empty program does not crash" do + program.switch_entries(1,1,2,2) + end + context "switch 2 existing entries" do + let(:entry_12) { FactoryGirl.create(:program_entry, :program => program, :slot => 1, :track => 2) } + let(:entry_34) { FactoryGirl.create(:program_entry, :program => program, :slot => 3, :track => 4) } + before do + entry_12 + entry_34 + program.switch_entries(1,2,3,4) + end + it "entry-method returns switched entries" do + program.entry(1,2).should == entry_34 + program.entry(3,4).should == entry_12 + end + it "entry-method returns switched entries after reload" do + program.reload + program.entry(1,2).should == entry_34 + program.entry(3,4).should == entry_12 + end + it "entry_12 knows its new location after reload" do + entry_12.reload + [entry_12.slot, entry_12.track].should == [3, 4] + end + it "entry_12 does not know its new location without reload" do + [entry_12.slot, entry_12.track].should == [1, 2] + end + it "entry_34 knows its new location after reload" do + entry_34.reload + [entry_34.slot, entry_34.track].should == [1, 2] + end + it "entry_34 does not know its new location without reload" do + [entry_34.slot, entry_34.track].should == [3, 4] + end + end + it "move entry to empty location" do + entry_12 = a_program_entry_in_slot_and_track(program, 1, 2) + program.switch_entries(1,2,3,4) + program.entry(1,2).should be_nil + program.entry(3,4).should == entry_12 + end + end + describe "sessionsInProgram" do def a_program_entry_for(program) FactoryGirl.create(:program_entry, :program => program)