aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-11-28 17:27:28 +0000
committerLouise Crow <louise.crow@gmail.com>2014-12-15 18:04:22 +0000
commitc0873303ec0779f59ad1f920d531f89444f15878 (patch)
tree0d2f01350ff03d05743bc10b1eaaa3c4c5ec2544
parente977f2a1bf0fbe898ee13a6874b56a7f6178e622 (diff)
Add some inline editing
-rw-r--r--app/assets/javascripts/admin.js1
-rw-r--r--app/assets/javascripts/admin/holidays.js27
-rw-r--r--app/controllers/admin_holidays_controller.rb10
-rw-r--r--app/views/admin_holidays/_new_form.html.erb4
-rw-r--r--app/views/admin_holidays/new.html.erb2
-rw-r--r--spec/controllers/admin_holidays_controller_spec.rb38
6 files changed, 72 insertions, 10 deletions
diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js
index 4925a65a4..c4eb7d114 100644
--- a/app/assets/javascripts/admin.js
+++ b/app/assets/javascripts/admin.js
@@ -7,4 +7,5 @@
//= require admin/bootstrap-tab
//= require admin/admin
//= require admin/category-order
+//= require admin/holidays
//= require jquery_ujs
diff --git a/app/assets/javascripts/admin/holidays.js b/app/assets/javascripts/admin/holidays.js
new file mode 100644
index 000000000..81f269c80
--- /dev/null
+++ b/app/assets/javascripts/admin/holidays.js
@@ -0,0 +1,27 @@
+$(function() {
+
+ // New button loads the 'new' form via AJAX
+ $('#new-holiday-button').click(function(){
+ var new_call = $.ajax({ type: 'GET', url: $(this).attr('href')});
+ new_call.done(function(response) {
+ $('#existing-holidays').before(response);
+ });
+ return false;
+
+ });
+
+ // Each edit button loads the 'edit' form for that holiday via AJAX
+ $('.holiday').each(function(index){
+ var holiday_row = $(this);
+ var edit_button = holiday_row.find('.edit-button');
+
+ edit_button.click(function(){
+ var edit_call = $.ajax({ type: 'GET', url: holiday_row.data('target') });
+
+ edit_call.done(function(response) {
+ holiday_row.html(response);
+ });
+ return false;
+ });
+ });
+});
diff --git a/app/controllers/admin_holidays_controller.rb b/app/controllers/admin_holidays_controller.rb
index b65302620..3c4b37530 100644
--- a/app/controllers/admin_holidays_controller.rb
+++ b/app/controllers/admin_holidays_controller.rb
@@ -6,6 +6,11 @@ class AdminHolidaysController < AdminController
def new
@holiday = Holiday.new
+ if request.xhr?
+ render :partial => 'new_form'
+ else
+ render :action => 'new'
+ end
end
def create
@@ -20,6 +25,11 @@ class AdminHolidaysController < AdminController
def edit
@holiday = Holiday.find(params[:id])
+ if request.xhr?
+ render :partial => 'edit_form'
+ else
+ render :action => 'edit'
+ end
end
def update
diff --git a/app/views/admin_holidays/_new_form.html.erb b/app/views/admin_holidays/_new_form.html.erb
index 733d6d862..aee73f426 100644
--- a/app/views/admin_holidays/_new_form.html.erb
+++ b/app/views/admin_holidays/_new_form.html.erb
@@ -1,8 +1,8 @@
<table class="table table-striped table-condensed">
<tbody>
<tr>
- <td><%= form_for(holiday, :url => admin_holidays_path, :html => { :class => 'form-inline new-holiday-form'}) do |f| -%>
- <%= render :partial => 'form', :locals => { :f => f, :holiday => holiday, :context => :new } %>
+ <td><%= form_for(@holiday, :url => admin_holidays_path, :html => { :class => 'form-inline new-holiday-form'}) do |f| -%>
+ <%= render :partial => 'form', :locals => { :f => f, :holiday => @holiday, :context => :new } %>
<% end %>
</td>
</tr>
diff --git a/app/views/admin_holidays/new.html.erb b/app/views/admin_holidays/new.html.erb
index 3cc67429a..792c32f52 100644
--- a/app/views/admin_holidays/new.html.erb
+++ b/app/views/admin_holidays/new.html.erb
@@ -1,4 +1,4 @@
<% @title = 'New public holiday' %>
<h1><%= @title %></h1>
-<%= render :partial => 'new_form', :locals => { :holiday => @holiday } %>
+<%= render :partial => 'new_form' %>
diff --git a/spec/controllers/admin_holidays_controller_spec.rb b/spec/controllers/admin_holidays_controller_spec.rb
index 56d6041ca..1f76a7fcb 100644
--- a/spec/controllers/admin_holidays_controller_spec.rb
+++ b/spec/controllers/admin_holidays_controller_spec.rb
@@ -32,12 +32,22 @@ describe AdminHolidaysController do
describe :new do
- before do
- get :new
+
+ describe 'when not using ajax' do
+
+ it 'renders the new template' do
+ get :new
+ expect(response).to render_template('new')
+ end
+
end
- it 'renders the new template' do
- expect(response).to render_template('new')
+ describe 'when using ajax' do
+
+ it 'renders the new form partial' do
+ xhr :get, :new
+ expect(response).to render_template('new_form')
+ end
end
it 'creates a new holiday' do
@@ -88,14 +98,28 @@ describe AdminHolidaysController do
before do
@holiday = FactoryGirl.create(:holiday)
- get :edit, :id => @holiday.id
end
- it 'renders the edit template' do
- expect(response).to render_template('edit')
+ describe 'when not using ajax' do
+
+ it 'renders the edit template' do
+ get :edit, :id => @holiday.id
+ expect(response).to render_template('edit')
+ end
+
+ end
+
+ describe 'when using ajax' do
+
+ it 'renders the edit form partial' do
+ xhr :get, :edit, :id => @holiday.id
+ expect(response).to render_template('edit_form')
+ end
+
end
it 'gets the holiday in the id param' do
+ get :edit, :id => @holiday.id
assigns[:holiday].should == @holiday
end