Files
danbooru/app/controllers/notes_controller.rb
evazion b4ce2d83a6 models: remove belongs_to_creator macro.
The belongs_to_creator macro was used to initialize the creator_id field
to the CurrentUser. This made tests complicated because it meant you had
to create and set the current user every time you wanted to create an
object, when lead to the current user being set over and over again. It
also meant you had to constantly be aware of what the CurrentUser was in
many different contexts, which was often confusing. Setting creators
explicitly simplifies everything greatly.
2020-01-21 00:09:38 -06:00

70 lines
1.7 KiB
Ruby

class NotesController < ApplicationController
respond_to :html, :xml, :json, :js
before_action :member_only, :except => [:index, :show, :search]
def search
end
def index
@notes = Note.includes(:creator).paginated_search(params)
@notes = @notes.includes(:creator) if request.format.html?
respond_with(@notes)
end
def show
@note = Note.find(params[:id])
respond_with(@note) do |format|
format.html { redirect_to(post_path(@note.post, anchor: "note-#{@note.id}")) }
end
end
def create
@note = Note.create(note_params(:create).merge(creator: CurrentUser.user))
respond_with(@note) do |fmt|
fmt.json do
if @note.errors.any?
render :json => {:success => false, :reasons => @note.errors.full_messages}.to_json, :status => 422
else
render :json => @note.to_json(:methods => [:html_id])
end
end
end
end
def update
@note = Note.find(params[:id])
@note.update(note_params(:update))
respond_with(@note) do |format|
format.json do
if @note.errors.any?
render :json => {:success => false, :reasons => @note.errors.full_messages}.to_json, :status => 422
else
render :json => @note.to_json
end
end
end
end
def destroy
@note = Note.find(params[:id])
@note.update(is_active: false)
respond_with(@note)
end
def revert
@note = Note.find(params[:id])
@version = @note.versions.find(params[:version_id])
@note.revert_to!(@version)
respond_with(@note)
end
private
def note_params(context)
permitted_params = %i[x y width height body]
permitted_params += %i[post_id html_id] if context == :create
params.require(:note).permit(permitted_params)
end
end