Sha256: 4995b39f792361c5fce2d51da81c2e5c1bd734d5064ec60b44fa7e2d3c843318

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

module MyForum
  class ApplicationController < ActionController::Base

    before_filter :user_activity

    helper_method :attachment_img_path

    def authenticate_user!
      redirect_to admin_signin_path unless current_user
    end

    def current_user
      return session[:user_id].blank? ? nil : User.where(id: session[:user_id]).includes(:user_groups).first
    end
    helper_method :current_user

    def current_user_id
      session[:user_id]
    end
    helper_method :current_user_id

    def current_user_groups
      return [].push UserGroup::GUEST_GROUP.name unless current_user
      current_user.user_groups.map &:name
    end

    def new_pm_count
      return unless current_user
      PrivateMessage.unread_count_for(current_user)
    end
    helper_method :new_pm_count

    private

    def user_activity
      current_user.touch if current_user
    end

    def verify_admin
      redirect_to root_path unless current_user && current_user.is_admin
    end

    def check_access_permissions(obj)
      return true if current_user && current_user.is_admin

      category_user_groups = case obj.class.to_s
                               when 'MyForum::Forum'
                                 obj.category.user_groups
                               when 'MyForum::Topic'
                                 obj.forum.category.user_groups
                               else
                                 []
                             end

      redirect_to root_path if (category_user_groups.map(&:name) & current_user_groups).blank?
    end

    def attachment_img_path(attachment_id)
      attachment = Attachment.find_by_id(attachment_id.to_i)
      File.join(Attachment::URL, attachment.user_id.to_s, attachment.file_name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta8 app/controllers/my_forum/application_controller.rb
my_forum-0.0.1.beta7 app/controllers/my_forum/application_controller.rb
my_forum-0.0.1.beta5 app/controllers/my_forum/application_controller.rb