module Refinery::Admin::BaseController

Public Class Methods

included(base) click to toggle source
# File lib/refinery/admin/base_controller.rb, line 9
def self.included(base)
  base.layout :layout?

  base.before_action :force_ssl!,
                     :authenticate_refinery_user!,
                     :restrict_controller

  base.after_action :store_location?, :only => [:index] # for redirect_back_or_default

  base.helper_method :searching?, :group_by_date, :refinery_admin_root_path
end

Public Instance Methods

admin?() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 21
def admin?
  true # we're in the admin base controller, so always true.
end
refinery_admin_root_path() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 29
def refinery_admin_root_path
  current_refinery_user.landing_url
end
searching?() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 25
def searching?
  params[:search].present?
end

Protected Instance Methods

authenticate_refinery_user!() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 39
def authenticate_refinery_user!
  authorisation_manager.authenticate!
end
force_ssl!() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 35
def force_ssl!
  redirect_to :protocol => 'https' if Refinery::Core.force_ssl && !request.ssl?
end
group_by_date(records) click to toggle source
# File lib/refinery/admin/base_controller.rb, line 43
def group_by_date(records)
  new_records = []

  records.each do |record|
    key = record.created_at.strftime("%Y-%m-%d")
    record_group = new_records.map{ |r| r.last if r.first == key }.flatten.compact << record
    (new_records.delete_if { |i| i.first == key}) << [key, record_group]
  end

  new_records
end
restrict_controller() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 55
def restrict_controller
  unless allow_controller?(params[:controller].gsub('admin/', ''))
    logger.warn "'#{current_refinery_user}' tried to access '#{params[:controller]}' but was rejected."
    error_404
  end
end

Private Instance Methods

allow_controller?(controller_name) click to toggle source
# File lib/refinery/admin/base_controller.rb, line 64
def allow_controller?(controller_name)
  authorisation_manager.allow?(:controller, controller_name)
end
authorisation_manager() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 68
def authorisation_manager
  @authorisation_manager ||= ::Refinery::Core::AuthorisationManager.new
end
layout?() click to toggle source
# File lib/refinery/admin/base_controller.rb, line 72
def layout?
  "refinery/admin#{'_dialog' if from_dialog?}"
end
pop_stored_location() click to toggle source

Clear and return the stored location

# File lib/refinery/admin/base_controller.rb, line 92
def pop_stored_location
  session.delete(:return_to)
end
redirect_back_or_default(default) click to toggle source

Redirect to the URI stored by the most recent store_location call or to the passed default.

# File lib/refinery/admin/base_controller.rb, line 98
def redirect_back_or_default(default)
  redirect_to(pop_stored_location || default)
end
store_location() click to toggle source

Store the URI of the current request in the session.

We can return to this location by calling redirect_back_or_default.

# File lib/refinery/admin/base_controller.rb, line 87
def store_location
  session[:return_to] = request.fullpath
end
store_location?() click to toggle source

TODO: all store_location stuff should be in its own object.. Check whether it makes sense to return the user to the last page they were at instead of the default e.g. refinery_admin_pages_path right now we just want to snap back to index actions and definitely not to dialogues.

# File lib/refinery/admin/base_controller.rb, line 80
def store_location?
  store_location unless request.xhr? || from_dialog?
end