Skip to content
Snippets Groups Projects
learning_objects_controller.rb 2.74 KiB
Newer Older
class LearningObjectsController < ApplicationController
  include Reportable

  before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like]
  after_action :increment_learning_object_views, only: [:show]
  before_action :authenticate_user!, except: [:index, :show, :like]

  # GET /learning_objects
  # GET /learning_objects.json
    @learning_objects = learning_object_repository.all
  # GET /learning_objects/1
  # GET /learning_objects/1.json
    @complaint = Complaint.new
    @messages = [
        Complaint.copyrights,
        Complaint.ofensive_content,
        Complaint.ofensive_user,
        Complaint.fake_user
  # GET /learning_objects/new
    @learning_object = LearningObject.new
  # GET /learning_objects/1/edit
  def edit
  # POST /learning_objects
  # POST /learning_objects.json
  def create
    @learning_object = LearningObject.new(learning_object_params)
      if learning_object_repository.save @learning_object
        format.html { redirect_to @learning_object, notice: 'Learning object was successfully created.' }
      else
        format.html { render :new }
      end
    end
  # 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
  def report_object
    learning_object_repository.report current_user, @learning_object, message, description
  end

  # 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]
  end

  def increment_learning_object_views
    if user_signed_in?
      learning_object_repository.increment_views current_user, @learning_object
    end