Commit 1869f19f authored by Douglas AC's avatar Douglas AC

Including image in Subjects and allowing CRUD operations related to it for editors.

parent 621da48e
......@@ -5,21 +5,22 @@
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# it under the terms of the GNU Affero General Public subject as published by
# the Free Software Foundation, either version 3 of the subject, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# GNU Affero General Public subject for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the GNU Affero General Public subject
# along with portalmec. If not, see <http://www.gnu.org/subjects/>.
class V1::SubjectsController < ApplicationController
include ::Paginator
before_action :authenticate_user!, except: [:destroy, :index]
before_action :set_subject, only: [:show, :update, :destroy]
before_action :authorize!, only: [:update ]
# GET /subjects
......@@ -29,4 +30,48 @@ class V1::SubjectsController < ApplicationController
render json: subjects
end
# GET /subjects/1
# GET /subjects/1.json
def show
render json: @subject
end
# POST /subjects
# POST /subjects.json
def create
@subject = Subject.new(subject_params)
authorize @subject
if @subject.save
render json: @subject, status: :created
else
render json: @subject.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /subjects/1
# PATCH/PUT /subjects/1.json
def update
if @subject.update(subject_params)
render json: @subject, status: :ok
else
render json: @subject.errors, status: :unprocessable_entity
end
end
private
def set_subject
@subject = Subject.find(params[:id])
end
def subject_params
params.require(:subject).permit(:name, :image)
end
def authorize!
authorize @subject
end
end
......@@ -21,4 +21,15 @@ class Subject < ApplicationRecord
has_many :subject_relations
has_and_belongs_to_many :searches
validates_presence_of :name
has_attached_file :image, :default_url => "/images/image.svg",
s3_headers: lambda { |attachment|
{
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=#{attachment.original_filename}",
}
},
source_file_options: {all: '-background none'}
validates_attachment_content_type :image, content_type: ['image/svg+xml']
validates_attachment_presence :image
end
# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class SubjectPolicy < ApplicationPolicy
def create?
record if user_can_edit?
end
def update?
record if user_can_edit?
end
end
\ No newline at end of file
# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class SubjectSerializer < ActiveModel::Serializer
cache key: 'subject', expires_in: 24.hours
attributes :id, :name, :image
end
......@@ -195,6 +195,7 @@ Rails.application.routes.draw do
resources :contacts
resources :suggestions
resources :statistics, only: [:index]
resources :subjects
post '/package', to: 'packages#link'
get '/subjects', to: 'subjects#index'
......
class AddImageToSubjects < ActiveRecord::Migration[5.0]
def up
add_attachment :subjects, :image
end
def down
remove_attachment :subjects, :image
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment