Created
April 1, 2013 14:43
-
-
Save valachi/5285313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding: UTF-8 | |
| class ApplicationController < ActionController::Base | |
| protect_from_forgery | |
| before_filter :fetch_bottom_carousel, :fetch_sidebar | |
| before_filter :pass_user_to_js, :pass_article_to_js | |
| helper_method :paginating?, :current_article, :current_article?, :current_paginating | |
| def authenticate_active_admin_user! | |
| authenticate_user! | |
| unless current_user.role?('admin') || current_user.role?('editor') | |
| redirect_to root_path, alert: 'Unauthorized Access!' | |
| end | |
| end | |
| # редиректим на страницу, где был напечатан комментарий | |
| def after_sign_in_path_for(resource) | |
| if cookies[:path] | |
| cookies[:path] | |
| cookies.delete :path | |
| else | |
| super | |
| end | |
| end | |
| def fetch_sidebar | |
| @articles = Article.excluding_categories(['спецпроекты']) | |
| @sidebar_articles = case | |
| when paginating? | |
| exclude_ids = articles_to_exlude_when_paginating | @bottom_carousel_articles | |
| @articles.excluding_ids(exclude_ids).sample(3) | |
| when current_article? | |
| related_articles = current_article.related_articles | |
| exclude_ids = related_articles | [current_article] | @bottom_carousel_articles | |
| related_articles + @articles.excluding_ids(exclude_ids).sample(3 - related_articles.count) | |
| else | |
| @articles.excluding_ids(@bottom_carousel_articles).sample(3) | |
| end | |
| end | |
| # как тут исключить статьи из сайдбара? | |
| def fetch_bottom_carousel | |
| @articles = Article.excluding_categories(['спецпроекты']) | |
| @bottom_carousel_articles = case | |
| when paginating? | |
| @articles.excluding_ids(articles_to_exlude_when_paginating).sample(9) | |
| when current_article? | |
| @articles.excluding_ids([current_article]).sample(9) | |
| else @articles.sample(9) | |
| end | |
| end | |
| def current_ability | |
| @current_ability ||= Ability.new(current_user) | |
| end | |
| rescue_from CanCan::AccessDenied do |exception| | |
| redirect_to admin_dashboard_path, :alert => exception.message | |
| end | |
| def pass_user_to_js | |
| gon.user = { | |
| id: current_user.try(:id), | |
| email: current_user.try(:email), | |
| guest: !current_user | |
| } | |
| end | |
| def pass_article_to_js | |
| if controller_name == 'articles' && action_name == 'show' | |
| gon.article = {id: params[:id]} | |
| end | |
| end | |
| def load_modal | |
| render partial: "shared/modals/#{params[:modal]}" | |
| end | |
| private | |
| def paginating? | |
| params[:page] && controller_name == 'articles' | |
| end | |
| def current_paginating | |
| return unless paginating? | |
| params[:page].to_i | |
| end | |
| def articles_to_exlude_when_paginating | |
| return unless paginating? && current_paginating | |
| Article.limit(8).offset((current_paginating - 1)* 8) | |
| end | |
| def current_article? | |
| controller_name == 'articles' && action_name == 'show' | |
| end | |
| def current_article | |
| return false unless current_article? | |
| Article.find_by_id(params[:id]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment