sidebar_controller.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:3k
源码类别:

Ajax

开发平台:

Others

  1. class Admin::SidebarController < Admin::BaseController
  2.   def index
  3.     @available = available
  4.     # Reset the staged position based on the active position.
  5.     Sidebar.delete_all('active_position is null')
  6.     flash_sidebars
  7.     begin
  8.       @active = Sidebar.find(:all, :order => 'active_position ASC') unless @active
  9.     rescue
  10.       # Avoiding the view to crash
  11.       @active = []
  12.       flash[:error] = _("It seems something went wrong. Maybe some of your sidebars are actually missing and you should either reinstall them or remove them manually")
  13.     end
  14.   end
  15.   def set_active
  16.     # Get all available plugins
  17.     klass_for = available.inject({}) do |hash, klass|
  18.       hash.merge({ klass.short_name => klass })
  19.     end
  20.     # Get all already active plugins
  21.     activemap = flash_sidebars.inject({}) do |h, sb_id|
  22.       sb = Sidebar.find(sb_id.to_i)
  23.       sb ? h.merge(sb.html_id => sb_id) : h
  24.     end
  25.     # Figure out which plugins are referenced by the params[:active] array and
  26.     # lay them out in a easy accessible sequential array
  27.     flash[:sidebars] = params[:active].inject([]) do |array, name|
  28.       if klass_for.has_key?(name)
  29.         new_sidebar_id = klass_for[name].create.id
  30.         @new_item = Sidebar.find(new_sidebar_id)
  31.         array << new_sidebar_id
  32.       elsif activemap.has_key?(name)
  33.         array << activemap[name]
  34.       else
  35.         array
  36.       end
  37.     end
  38.   end
  39.   def remove
  40.     flash[:sidebars] = flash_sidebars.reject do |sb_id|
  41.       sb_id == params[:id].to_i
  42.     end
  43.     @element_to_remove = params[:element]
  44.   end
  45.   def publish
  46.     Sidebar.transaction do
  47.       position = 0
  48.       params[:configure] ||= { }
  49.       # Crappy workaround to rails update_all bug with PgSQL / SQLite
  50.       ActiveRecord::Base.connection.execute("update sidebars set active_position=null")
  51.       flash_sidebars.each do |id|
  52.         sidebar = Sidebar.find(id)
  53.         sb_attribs = params[:configure][id.to_s] || {}
  54.         # If it's a checkbox and unchecked, convert the 0 to false
  55.         # This is ugly.  Anyone have an improvement?
  56.         sidebar.fields.each do |field|
  57.           sb_attribs[field.key] = field.canonicalize(sb_attribs[field.key])
  58.         end
  59.         sidebar.update_attributes(:config => sb_attribs,
  60.                                   :active_position => position)
  61.         position += 1
  62.       end
  63.       Sidebar.delete_all('active_position is null')
  64.     end
  65.     sweep_cache
  66.     index
  67.   end
  68.   protected
  69.   
  70.   def show_available
  71.     render :partial => 'availables', :object => available
  72.   end
  73.   def available
  74.     ::Sidebar.available_sidebars
  75.   end
  76.   def flash_sidebars
  77.     unless flash[:sidebars]
  78.       begin
  79.         active = Sidebar.find(:all, :order => 'active_position ASC')
  80.         flash[:sidebars] = active.map {|sb| sb.id }
  81.       rescue
  82.         # Avoiding the view to crash
  83.         @active = []
  84.         flash[:error] = _("It seems something went wrong. Maybe some of your sidebars are actually missing and you should either reinstall them or remove them manually")
  85.       end
  86.     end
  87.     flash[:sidebars]
  88.   end
  89.   helper_method :available
  90. end