2016-10-19 18:03:59 +08:00
|
|
|
class ArticleHomepagesController < ApplicationController
|
|
|
|
before_filter :find_article_homepage, :only => [:show, :edit, :update, :destroy]
|
|
|
|
|
|
|
|
def new
|
|
|
|
@article = ArticleHomepage.new
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {render :layout=>'static_base'}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
if User.current.logged?
|
|
|
|
@article = ArticleHomepage.new
|
|
|
|
@article.user = User.current
|
|
|
|
@article.homepage_id = User.current.homepage.id
|
|
|
|
@article.title = params[:article_homepage][:title]
|
|
|
|
@article.content = params[:article_homepage][:content]
|
|
|
|
if @article.save
|
|
|
|
if params[:set_homepage]
|
|
|
|
User.current.homepage.update_attribute(:article_id, @article.id)
|
|
|
|
end
|
|
|
|
redirect_to user_homepages_path(:user_id => User.current.id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
redirect_to signin_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {render :layout => 'clear_base'}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
if User.current.admin? || User.current.id == @article.user_id
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :layout => 'static_base' }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if User.current.logged?
|
|
|
|
@article.title = params[:article_homepage][:title]
|
|
|
|
@article.content = params[:article_homepage][:content]
|
2016-10-19 19:05:42 +08:00
|
|
|
if params[:set_homepage]
|
|
|
|
User.current.homepage.update_attribute(:article_id, @article.id)
|
|
|
|
else
|
|
|
|
if User.current.homepage.article_id == @article.id
|
|
|
|
User.current.homepage.update_attribute(:article_id, nil)
|
|
|
|
end
|
|
|
|
end
|
2016-10-19 18:03:59 +08:00
|
|
|
if @article.save
|
|
|
|
redirect_to user_homepages_path(:user_id => User.current.id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
redirect_to signin_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @article
|
|
|
|
if @article.homepage.article_id == @article.id
|
|
|
|
@article.homepage.update_attribute(:article_id, nil)
|
|
|
|
end
|
|
|
|
@article.destroy
|
|
|
|
redirect_to user_homepages_path(:user_id => User.current.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def find_article_homepage
|
|
|
|
@article = ArticleHomepage.find params[:id]
|
|
|
|
end
|
|
|
|
end
|