添加helper辅助方法
This commit is contained in:
parent
a922998ae5
commit
8e1dd0bab8
|
@ -2,4 +2,65 @@ class ApplicationController < ActionController::Base
|
||||||
# Prevent CSRF attacks by raising an exception.
|
# Prevent CSRF attacks by raising an exception.
|
||||||
# For APIs, you may want to use :null_session instead.
|
# For APIs, you may want to use :null_session instead.
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
helper_method :current_user, :is_logined?, :current_user_is_admin?, :get_categories_options, :current_user_can_star?,
|
||||||
|
:current_user_can_edit_comment?, :markdown_parser
|
||||||
|
|
||||||
|
rescue_from Exception, with: :error_500 unless Rails.env.development?
|
||||||
|
rescue_from ActiveRecord::RecordNotFound, with: :error_404 unless Rails.env.development?
|
||||||
|
|
||||||
|
before_filter :current_user
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
@current_user = nil
|
||||||
|
if session[:user_id].present?
|
||||||
|
@current_user = User.find(session[:user_id])
|
||||||
|
redirect_to logout_path(referer) unless @current_user
|
||||||
|
end
|
||||||
|
@current_user
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_user_is_admin?
|
||||||
|
@current_user && @current_user.admin
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_user_can_star?(article)
|
||||||
|
return false unless @current_user
|
||||||
|
!article.article_stars.find_by(user_id: @current_user.id).present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_user_can_edit_comment?(comment)
|
||||||
|
return false unless @current_user
|
||||||
|
comment.user_id == @current_user.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_categories_options
|
||||||
|
options = []
|
||||||
|
Category.order('articles_count desc').each {|c| options << [c.name, c.id]}
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
def markdown_parser(content)
|
||||||
|
markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML, autolink: true, tables: true
|
||||||
|
markdown.render content
|
||||||
|
end
|
||||||
|
|
||||||
|
def referer
|
||||||
|
request.env['HTTP_REFERER'] || root_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_logined?
|
||||||
|
session[:user_id].present? && session[:user_id]
|
||||||
|
end
|
||||||
|
|
||||||
|
def require_login
|
||||||
|
redirect_to root_path unless is_logined?
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_500
|
||||||
|
render file: File.join(Rails.root, 'public/500.html'), status: 500, layout: false
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_404
|
||||||
|
render file: File.join(Rails.root, 'public/404.html'), status: 404, layout: false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,2 +1,27 @@
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
|
def avatar(user)
|
||||||
|
user.avatar.present? ? image_tag(user.avatar) : (gravatar_image_tag user.email, alt: user.username)
|
||||||
|
end
|
||||||
|
|
||||||
|
def avatar_url(user)
|
||||||
|
user.avatar.present? ? user.avatar : (gravatar_image_url user.email, alt: user.username)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_categories
|
||||||
|
Category.order('articles_count desc')
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_tags(article)
|
||||||
|
tags = article.tags || ''
|
||||||
|
tags.split(',')
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_articles_count
|
||||||
|
Article.count
|
||||||
|
end
|
||||||
|
|
||||||
|
def blog_title
|
||||||
|
blog = BlogInfo.first
|
||||||
|
blog.present? ? blog.blog_title : 'Blog'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue