Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
#55 move program entries in a program
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera Peeters committed Aug 12, 2013
1 parent 1a96e30 commit 1450736
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 5 deletions.
32 changes: 32 additions & 0 deletions app/controllers/program_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions app/models/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions app/views/program_entries/_form_move.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= form_for(@program_entry, :url => url_for(:action => 'update_location'), remote: true) do |f| %>
<div class="popup">
<%= errors_for @program_entry %>

<%= f.hidden_field :program_id %>
<h3>Program version <%= @program_entry.program.version %> </h3>

<div class="in_a_form"> Slot <%= f.text_field :slot, size: 2 %>
Track <%= f.text_field :track, size:2 %> </div>

<div class="actions">
<%= f.submit %>
<a onclick="$(this).closest('form').remove()">cancel</a>
</div>

</div>
<% end %>

10 changes: 7 additions & 3 deletions app/views/program_entries/_show_in_program_matrix.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" %>
<div class= "ProgramMatrix_edit_link" >
<%= link_to "edit", {controller: 'program_entries', action: 'edit', id: program_entry.to_param},
id: "program_entry_#{program_entry.id}",
:remote => true %>
<%= link_to "move", {controller: 'program_entries', action: 'edit_location', id: program_entry.to_param},
:remote => true %>
</div>
<% end %>
</td>
<% end %>
2 changes: 2 additions & 0 deletions app/views/program_entries/edit_location.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('<%="#program_entry_#{@program_entry.id}"%>').after('<%= j render("form_move") %>');

7 changes: 7 additions & 0 deletions app/views/program_entries/update_location.js.erb
Original file line number Diff line number Diff line change
@@ -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 %>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion spec/models/program_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions spec/models/program_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1450736

Please sign in to comment.