Refactor courses
This commit is contained in:
@@ -23,64 +23,67 @@ require 'yaml'
|
||||
require 'authentication.rb'
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
helper :all
|
||||
protect_from_forgery
|
||||
helper :all
|
||||
protect_from_forgery
|
||||
|
||||
include AuthenticationSystem
|
||||
include AuthenticationSystem
|
||||
|
||||
before_filter :startup
|
||||
#before_filter :set_timezone
|
||||
before_filter :require_login, :only => [ :edit, :new, :create, :update, :delete, :destroy ]
|
||||
before_filter :startup
|
||||
# before_filter :set_timezone
|
||||
before_filter :require_login, only: [:edit, :new, :create, :update, :delete,
|
||||
:destroy]
|
||||
|
||||
protected
|
||||
def rescue_action(exception)
|
||||
# Acesso negado
|
||||
if exception.is_a?(AccessDenied)
|
||||
respond_to do |format|
|
||||
format.html {
|
||||
if logged_in?
|
||||
render :file => "#{Rails.root}/public/401.html", :status => 401
|
||||
else
|
||||
login_by_html
|
||||
end
|
||||
}
|
||||
format.xml { head 401 }
|
||||
end
|
||||
rescue_from AccessDenied, with: :deny_access
|
||||
rescue_from ActiveRecord::RecordInvalid, with: :reshow_form
|
||||
rescue_from ActiveRecord::RecordNotFound, with: :show_not_found
|
||||
|
||||
# Erro de validacao
|
||||
elsif exception.is_a?(ActiveRecord::RecordInvalid)
|
||||
respond_to do |format|
|
||||
format.html { render :action => (params[:from].nil? ? (exception.record.new_record? ? 'new' : 'edit') : params[:from]) }
|
||||
format.xml { render :xml => exception.record.errors, :status => :unprocessable_entity }
|
||||
end
|
||||
protected
|
||||
|
||||
# Registro nao encontrado
|
||||
elsif (RAILS_ENV == 'production') and exception.is_a?(ActiveRecord::RecordNotFound)
|
||||
respond_to do |format|
|
||||
format.html { render :file => "#{Rails.root}/public/404.html", :status => 404 }
|
||||
format.xml { head 404 }
|
||||
end
|
||||
def deny_access
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
if logged_in?
|
||||
render file: "#{Rails.root}/public/401.html", status: 401
|
||||
else
|
||||
login_by_html
|
||||
end
|
||||
end
|
||||
format.xml { head 401 }
|
||||
end
|
||||
end
|
||||
|
||||
# Outras excecoes
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
def reshow_form(exception)
|
||||
respond_to do |format|
|
||||
format.html { render action: (params[:from].nil? ? (exception.record.new_record? ? 'new' : 'edit') : params[:from]) }
|
||||
format.xml { render xml: exception.record.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
|
||||
#def set_timezone
|
||||
# #Time.zone = session[:user].tz
|
||||
# Time.zone = "America/Fortaleza"
|
||||
#end
|
||||
def show_not_found
|
||||
if (RAILS_ENV == 'production')
|
||||
respond_to do |format|
|
||||
format.html { render file: "#{Rails.root}/public/404.html", status: 404 }
|
||||
format.xml { head 404 }
|
||||
end
|
||||
else
|
||||
fail ActiveRecord::RecordNotFound
|
||||
end
|
||||
end
|
||||
|
||||
def startup
|
||||
if session[:user_id]
|
||||
@current_user = User.find(session[:user_id])
|
||||
else
|
||||
login_by_token
|
||||
end
|
||||
# def set_timezone
|
||||
# #Time.zone = session[:user].tz
|
||||
# Time.zone = "America/Fortaleza"
|
||||
# end
|
||||
|
||||
@color = App.default_color
|
||||
@color = @current_user.pref_color if @current_user
|
||||
@color = params[:color].to_i if params[:color]
|
||||
end
|
||||
def startup
|
||||
if session[:user_id]
|
||||
@current_user = User.find(session[:user_id])
|
||||
else
|
||||
login_by_token
|
||||
end
|
||||
|
||||
@color = App.default_color
|
||||
@color = @current_user.pref_color if @current_user
|
||||
@color = params[:color].to_i if params[:color]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,35 +20,26 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class CoursesController < ApplicationController
|
||||
|
||||
before_filter :require_admin, :only => [:new, :create, :edit, :update, :destroy]
|
||||
before_filter :require_login, :only => [:enroll, :unenroll]
|
||||
before_filter :find_course, :except => [:index]
|
||||
#after_filter :cache_sweep, :only => [ :create, :update, :destroy ]
|
||||
before_filter :require_admin, only: [:new, :create, :edit, :update,
|
||||
:destroy]
|
||||
before_filter :require_login, only: [:enroll, :unenroll]
|
||||
before_filter :find_course, except: [:index]
|
||||
# after_filter :cache_sweep, only: [ :create, :update, :destroy ]
|
||||
|
||||
def index
|
||||
params[:period] = nil if params[:period] == App.current_period
|
||||
@period = params[:period] || App.current_period
|
||||
|
||||
if logged_in? and !@current_user.courses.empty?
|
||||
@courses = Course.all(:order => 'grade asc, full_name asc',
|
||||
:conditions => ['period = ? and hidden = ? and id not in (?)',
|
||||
@period, false, @current_user.courses])
|
||||
else
|
||||
@courses = Course.all(:order => 'grade asc, full_name asc',
|
||||
:conditions => ['period = ? and hidden = ?', @period, false])
|
||||
end
|
||||
@courses = Course.visible.where(period: @period)
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.xml { render :xml => @courses }
|
||||
format.xml { render xml: @courses }
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.xml { render :xml => @course }
|
||||
format.xml { render xml: @course }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -61,8 +52,9 @@ class CoursesController < ApplicationController
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to course_path(@course) }
|
||||
format.xml { head :created, :location => course_url(@course,
|
||||
:format => :xml) }
|
||||
format.xml do
|
||||
head :created, location: course_url(@course, format: :xml)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -111,19 +103,21 @@ class CoursesController < ApplicationController
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def find_course
|
||||
if params[:id]
|
||||
params[:id] = Course.first(:conditions => ['short_name = ?', params[:id]], :order => 'period desc').id if !params[:id].is_numeric? and !Course.find_by_short_name(params[:id]).nil?
|
||||
@course = Course.from_param(params[:id])
|
||||
else
|
||||
@course = Course.new(params[:course])
|
||||
end
|
||||
@course = params[:id] ? Course.find(params[:id]) : Course.new(params[:course])
|
||||
end
|
||||
|
||||
def require_admin
|
||||
raise AccessDenied.new unless admin?
|
||||
fail AccessDenied, 'only admins can modify courses' unless admin?
|
||||
end
|
||||
|
||||
def cache_sweep
|
||||
expire_fragment(course_path(@course.id, :part => 'right'))
|
||||
expire_fragment(course_path(@course.id, part: 'right'))
|
||||
expire_fragment(course_path(@course.id))
|
||||
expire_fragment(courses_path)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user