diff options
author | Louise Crow <louise.crow@gmail.com> | 2014-09-19 09:22:35 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2014-09-22 12:42:08 +0100 |
commit | ffb60e1a6cbd35c8cae4902bb28d7ba4f32ed740 (patch) | |
tree | aec571c88d7001b83745f10ece089f12ee1f8e13 /app/controllers | |
parent | 897177e0660a3d567fcb4c4d203c1f7da8b5b0a4 (diff) |
Add actions for reordering public body headings and categories.
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/admin_public_body_category_controller.rb | 23 | ||||
-rw-r--r-- | app/controllers/admin_public_body_heading_controller.rb | 21 |
2 files changed, 44 insertions, 0 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb index e22c5c572..d138eb495 100644 --- a/app/controllers/admin_public_body_category_controller.rb +++ b/app/controllers/admin_public_body_category_controller.rb @@ -55,6 +55,29 @@ class AdminPublicBodyCategoryController < AdminController end end + def reorder + error = nil + ActiveRecord::Base.transaction do + params[:categories].each_with_index do |category_id, index| + link = PublicBodyCategoryLink.find(:first, + :conditions => ['public_body_category_id = ? + AND public_body_heading_id = ?', + category_id, params[:heading_id]]) + unless link + error = "Couldn't find PublicBodyCategoryLink for category #{category_id}, heading #{params[:heading_id]}" + raise ActiveRecord::Rollback + end + link.category_display_order = index + unless link.save + error = link.errors.full_messages.join(",") + raise ActiveRecord::Rollback + end + end + render :nothing => true, :status => :ok and return + end + render :text => error, :status => :unprocessable_entity + end + def create I18n.with_locale(I18n.default_locale) do @category = PublicBodyCategory.new(params[:public_body_category]) diff --git a/app/controllers/admin_public_body_heading_controller.rb b/app/controllers/admin_public_body_heading_controller.rb index 43d8e329c..cc9d586a7 100644 --- a/app/controllers/admin_public_body_heading_controller.rb +++ b/app/controllers/admin_public_body_heading_controller.rb @@ -18,6 +18,27 @@ class AdminPublicBodyHeadingController < AdminController end end + def reorder + error = nil + ActiveRecord::Base.transaction do + params[:headings].each_with_index do |heading_id, index| + begin + heading = PublicBodyHeading.find(heading_id) + rescue ActiveRecord::RecordNotFound => e + error = e.message + raise ActiveRecord::Rollback + end + heading.display_order = index + unless heading.save + error = heading.errors.full_messages.join(",") + raise ActiveRecord::Rollback + end + end + render :nothing => true, :status => :ok and return + end + render :text => error, :status => :unprocessable_entity + end + def new @heading = PublicBodyHeading.new render :formats => [:html] |