ecole_eft_app/app/controllers/admin/articles_controller.rb
Nicolas Bally 56a0aa9848 initial
2012-06-17 21:11:12 +02:00

104 lines
1.8 KiB
Ruby

# -*- encoding : utf-8 -*-
class Admin::ArticlesController < ApplicationController
before_filter :authenticate_admin!
layout "admin"
before_filter :find_articles
def index
end
def cible
render :layout => false
end
def new
@article = Article.new(:published_at => Time.now)
end
def edit
@article = Article.find(params[:id])
render :layout => false
end
def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = "L'article à été ajouté avec succès."
self.find_articles
else
render :action => "new"
end
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = "L'article à été modifié avec succès."
else
render :action => "edit"
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
end
protected
def find_articles
date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i
if params[:start] and params[:start] =~ date_regex
start = Date.parse(params[:start]).beginning_of_day
params[:start]= start.strftime('%d/%m/%Y')
else
params[:start] = "Début"
end
if params[:stop] and params[:stop] =~ date_regex
stop = Date.parse(params[:stop]).end_of_day
params[:stop]= stop.strftime('%d/%m/%Y')
else
params[:stop] = "Fin"
end
@articles = Article.order('published_at DESC')
@articles = @articles.after(start) if start
@articles = @articles.before(stop) if stop
per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 100
page = (params[:page] and params[:page] != "") ? params[:page] : 1
@articles = @articles.page(page).per(per_page)
#@articles = Event.order('start_at, stop_at').after(start).before(stop)
end
end