Skip to content
Snippets Groups Projects
learning_objects_controller.rb 3.59 KiB
Newer Older
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
require 'uri'

class LearningObjectsController < ApplicationController
  before_action :set_learning_object, only: [:show, :edit, :update,
                                             :destroy, :like, :bookmarks,
                                             :collections, :upload, :download]
  after_action :increment_learning_object_views, only: [:show]
  before_action :authenticate_user!, except: [:index, :show]
  # GET /learning_objects/1
  # GET /learning_objects/1.json
    @liked = learning_object_repository.liked?(current_user, @learning_object) if user_signed_in?
  # GET /learning_objects/new
    @learning_object = LearningObject.new
    @school_levels = ['Educação Infantil', 'Ensino Fundamental', 'Ensino Médio']
    @subjects = Subject.default_list
    @types = learning_object_repository.types
  # GET /learning_objects/1/edit
  def edit
  # POST /learning_objects
  # POST /learning_objects.json
  def create
    @draft = LearningObject::Draft.new(learning_object_params)
    @draft.subjects = build_subjects learning_object_params[:subjects]
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed

      if learning_object_repository.create @draft
        # go to file submission page
        format.html { redirect_to upload_learning_object_path(id: @draft.id), notice: 'Learning object was successfully created.' }
        format.html { redirect_to new_learning_object_path, notice: 'Was not possible to create a learning object.' }
  # PATCH/PUT /learning_objects/1
  # PATCH/PUT /learning_objects/1.json
      if learning_object_repository.update(learning_object_params)
        format.html { redirect_to @learning_object, notice: 'Learning object was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  # DELETE /learning_objects/1
  # DELETE /learning_objects/1.json
    learning_object_repository.destroy @learning_object

    respond_to do |format|
      format.html { redirect_to learning_objects_url, notice: 'Learning object was successfully destroyed.' }
    end
    if @learning_object.liked? current_user
      @learning_object.dislike current_user
    else
      @learning_object.like current_user
    end

    if request.xhr?
      render json: {count: @learning_object.likes, id: params[:id]}
    end
    @learning_object.download current_user
    redirect_to @learning_object.get_retrievelink
  # POST /learning_objects/1/bookmarks
  def bookmarks
    bookmarks = current_user.bookmarks
    bookmarks.add @learning_object
    collection_repository.save_learning_objects bookmarks

    if request.xhr?
      render json: {id: params[:id]}
    end
  # GET /learning_objects/1/collections.json
  def collections
    @collections = @learning_object.collections
  end

  def build_subjects(subjects)
    subjects.map do |subject|
      subject_repository.find subject
  # Use callbacks to share common setup or constraints between actions.
  def set_learning_object
    @learning_object = learning_object_repository.find params[:id]
  # Never trust parameters from the scary internet, only allow the white list through.
  def learning_object_params
    params[:learning_object].permit(:author, :name, :type, :description, :school_level, :language, subjects: [])
    if user_signed_in?
      learning_object_repository.increment_views current_user, @learning_object
    end