class Admin::UniversitiesController < Admin::BaseController

  include Admin::InheritedResource

  resources_configuration[:self][:finder] = :find_by_subdomain

  # Method to activate the University.
  def activate
    respond_to do |format|
      if resource.update_attribute(:active, true)
        format.js {render 'referesh_datatable'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to activate.'}
      end
    end
  end

  # Method to deactivate the University.
  def deactivate
    respond_to do |format|
      if resource.update_attribute(:active, false)
        format.js {render 'referesh_datatable'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to deactivate.'}
      end
    end
  end

  # Method to find the University and update it's background image
  def upload_background_image
    @university = ActsAsTenant.current_tenant = University.find_by_subdomain(params[:id])
    if @university.update_attributes(permitted_params[:university])
      redirect_to admin_pages_path, notice: 'Image Updated successfully.'
    else
      redirect_to admin_pages_path, alert: 'Unable to update background image.'
    end
  end

  private
  def permitted_params
    params.permit(university: [:name, :subdomain, background_image_attributes: [:image, :id, :_destroy]])
  end

end
