#19 トップバーと新規作成ちょっとだけ
This commit is contained in:
@@ -36,9 +36,6 @@ class PostsController < ApplicationController
|
||||
|
||||
# POST /posts
|
||||
def create
|
||||
logger.info ">>> thumbnail: #{params[:thumbnail]&.content_type}"
|
||||
logger.info ">>> filename: #{params[:thumbnail]&.original_filename}"
|
||||
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
class WikiPagesController < ApplicationController
|
||||
def show
|
||||
p params
|
||||
wiki_page = WikiPage.find_by(title: params[:title])
|
||||
if wiki_page
|
||||
render plain: wiki_page.markdown
|
||||
@@ -9,15 +8,26 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
wiki_page = WikiPage.new(title: params[:title], tag_id: params[:tag_id], created_user: current_user, updated_user: current_user)
|
||||
wiki_page.markdown = params[:markdown], user: current_user
|
||||
if wiki_page.save
|
||||
render json: wiki_page, status: :created
|
||||
else
|
||||
render json: { errors: wiki_page.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
title = params[:title]
|
||||
wiki_page = WikiPage.find_by(title: title)
|
||||
unless wiki_page
|
||||
wiki_page = WikiPage.new(title: title, created_user: current_user, updated_user: current_user)
|
||||
end
|
||||
wiki_page.markdown = params[:markdown]
|
||||
wiki_page = WikiPage.find(params[:id])
|
||||
return head :not_found unless wiki_pages
|
||||
|
||||
wiki_page.updated_user = current_user
|
||||
wiki_page.markdown = params[:markdown], user: current_user
|
||||
wiki_page.save!
|
||||
head :ok
|
||||
end
|
||||
|
||||
@@ -2,33 +2,36 @@ require 'gollum-lib'
|
||||
|
||||
|
||||
class WikiPage < ApplicationRecord
|
||||
WIKI_PATH = Rails.root.join('wiki').to_s
|
||||
|
||||
belongs_to :tag, optional: true
|
||||
belongs_to :created_user, class_name: 'User', foreign_key: 'created_user_id'
|
||||
belongs_to :updated_user, class_name: 'User', foreign_key: 'updated_user_id'
|
||||
|
||||
validates :title, presence: true, length: { maximum: 255 }, uniqueness: true
|
||||
|
||||
def markdown
|
||||
wiki = Gollum::Wiki.new(WIKI_PATH)
|
||||
page = wiki.page(title)
|
||||
def body
|
||||
page = wiki.page("#{ id }.md")
|
||||
page&.raw_data
|
||||
end
|
||||
|
||||
def markdown= content
|
||||
wiki = Gollum::Wiki.new(WIKI_PATH)
|
||||
|
||||
page = wiki.page(title)
|
||||
def body= content, user:
|
||||
page = wiki.page("#{ id }.md")
|
||||
|
||||
commit_info = { message: "Update #{ title }",
|
||||
name: current_user.id,
|
||||
name: user.id,
|
||||
email: 'dummy@example.com' }
|
||||
|
||||
if page
|
||||
page.update(content, commit: commit_info)
|
||||
else
|
||||
wiki.write_page(title, :markdown, content, commit_info)
|
||||
wiki.write_page("#{ id }.md", :markdown, content, commit_info)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
WIKI_PATH = Rails.root.join('wiki').to_s
|
||||
|
||||
def wiki
|
||||
@wiki ||= Gollum::Wiki.new(WIKI_PATH)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,8 +20,9 @@ Rails.application.routes.draw do
|
||||
delete 'posts/:id/viewed', to: 'posts#unviewed'
|
||||
get 'preview/title', to: 'preview#title'
|
||||
get 'preview/thumbnail', to: 'preview#thumbnail'
|
||||
get 'wiki/*title', to: 'wiki_pages#show', format: false
|
||||
post 'wiki/*title', to: 'wiki_pages#save', format: false
|
||||
get 'wiki/:title', to: 'wiki_pages#show'
|
||||
post 'wiki', to: 'wiki_pages#create'
|
||||
put 'wiki/:id', to: 'wiki_pages#update'
|
||||
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
|
||||
Reference in New Issue
Block a user