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

Ajax

开发平台:

Others

  1. class Admin::SettingsController < Admin::BaseController
  2.   cache_sweeper :blog_sweeper
  3.   def index
  4.     if this_blog.base_url.blank?
  5.       this_blog.base_url = blog_base_url
  6.     end
  7.     load_settings
  8.   end
  9.   
  10.   def read; load_settings end
  11.   def write; load_settings end
  12.   def feedback; load_settings end
  13.   
  14.   def seo
  15.     load_settings
  16.     if File.exists? "#{RAILS_ROOT}/public/robots.txt"
  17.       @setting.robots = ""
  18.       file = File.readlines("#{RAILS_ROOT}/public/robots.txt")
  19.       file.each do |line|
  20.         @setting.robots << line
  21.       end
  22.     end
  23.   end
  24.   
  25.   def redirect
  26.     flash[:notice] = _("Please review and save the settings before continuing")
  27.     redirect_to :action => "index"
  28.   end
  29.   def update
  30.     if request.post?
  31.       Blog.transaction do
  32.         params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
  33.         this_blog.save
  34.         flash[:notice] = _('config updated.')
  35.       end
  36.       
  37.       save_robots unless params[:setting][:robots].blank?
  38.       
  39.       redirect_to :action => params[:from]
  40.     end
  41.   rescue ActiveRecord::RecordInvalid
  42.     render :action => params[:from]
  43.   end
  44.   
  45.   def update_database
  46.     @current_version = Migrator.current_schema_version
  47.     @needed_version = Migrator.max_schema_version
  48.     @support = Migrator.db_supports_migrations?
  49.     @needed_migrations = Migrator.available_migrations[@current_version..@needed_version].collect do |mig|
  50.       mig.scan(/d+_([w_]+).rb$/).flatten.first.humanize
  51.     end
  52.   end
  53.   def migrate
  54.     if request.post?
  55.       Migrator.migrate
  56.       redirect_to :action => 'update_database'
  57.     end
  58.   end
  59.   
  60.   private
  61.   def load_settings
  62.     @setting = this_blog
  63.   end
  64.   
  65.   def save_robots
  66.     if File.writable? "#{RAILS_ROOT}/public/robots.txt"
  67.       robots = File.new("#{RAILS_ROOT}/public/robots.txt", "r+")
  68.       robots.write(params[:setting][:robots])
  69.       robots.close
  70.     end
  71.   end
  72. end