-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from codidact/art/articles
- Loading branch information
Showing
34 changed files
with
470 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the Articles controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
class ArticlesController < ApplicationController | ||
before_action :set_article | ||
before_action :check_article | ||
|
||
def show | ||
if @article.deleted? | ||
check_your_privilege('ViewDeleted', @article) # || return | ||
end | ||
end | ||
|
||
def share | ||
redirect_to article_path(params[:id]) | ||
end | ||
|
||
def edit | ||
check_your_privilege('Edit', @article) | ||
end | ||
|
||
def update | ||
return unless check_your_privilege('Edit', @article) | ||
|
||
PostHistory.post_edited(@article, current_user, before: @article.body_markdown, | ||
after: params[:article][:body_markdown], comment: params[:edit_comment]) | ||
body_rendered = helpers.render_markdown(params[:article][:body_markdown]) | ||
if @article.update(article_params.merge(tags_cache: params[:article][:tags_cache]&.reject { |e| e.to_s.empty? }, | ||
body: body_rendered, last_activity: DateTime.now, | ||
last_activity_by: current_user)) | ||
redirect_to article_path(@article) | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
unless check_your_privilege('Delete', @article, false) | ||
flash[:danger] = 'You must have the Delete privilege to delete posts.' | ||
redirect_to article_path(@article) && return | ||
end | ||
|
||
if @article.deleted | ||
flash[:danger] = "Can't delete a deleted post." | ||
redirect_to article_path(@article) && return | ||
end | ||
|
||
if @article.update(deleted: true, deleted_at: DateTime.now, deleted_by: current_user, | ||
last_activity: DateTime.now, last_activity_by: current_user) | ||
PostHistory.post_deleted(@article, current_user) | ||
else | ||
flash[:danger] = "Can't delete this post right now. Try again later." | ||
end | ||
redirect_to article_path(@article) | ||
end | ||
|
||
def undelete | ||
unless check_your_privilege('Delete', @article, false) | ||
flash[:danger] = 'You must have the Delete privilege to undelete posts.' | ||
redirect_to article_path(@article) && return | ||
end | ||
|
||
unless @article.deleted | ||
flash[:danger] = "Can't undelete an undeleted post." | ||
redirect_to article_path(@article) && return | ||
end | ||
|
||
if @article.update(deleted: false, deleted_at: nil, deleted_by: nil, | ||
last_activity: DateTime.now, last_activity_by: current_user) | ||
PostHistory.post_undeleted(@article, current_user) | ||
else | ||
flash[:danger] = "Can't undelete this article right now. Try again later." | ||
end | ||
redirect_to article_path(@article) | ||
end | ||
|
||
private | ||
|
||
def set_article | ||
@article = Article.find params[:id] | ||
if @article.deleted && !current_user&.has_post_privilege?('ViewDeleted', @article) | ||
not_found | ||
end | ||
end | ||
|
||
def check_article | ||
unless @article.post_type_id == Article.post_type_id | ||
not_found | ||
end | ||
end | ||
|
||
def article_params | ||
params.require(:article).permit(:body_markdown, :title, :tags_cache) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ArticlesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
module CommentsHelper | ||
end | ||
|
||
class CommentScrubber < Rails::Html::PermitScrubber | ||
def initialize | ||
super | ||
self.tags = %w[a b i em strong strike del code] | ||
self.attributes = %w[href title] | ||
end | ||
|
||
def skip_node?(node) | ||
node.text? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module PostTypesHelper | ||
def post_type_badge(type) | ||
icon_class = { | ||
'Question' => 'fas fa-question', | ||
'Article' => 'fas fa-newspaper' | ||
}[type] | ||
tag.span class: 'badge is-tag is-filled is-muted' do | ||
tag.i(class: icon_class) + ' ' + | ||
tag.span(type) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Article < Post | ||
default_scope { where(post_type_id: Article.post_type_id) } | ||
|
||
def self.post_type_id | ||
PostType.mapping['Article'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<%= render 'posts/markdown_script' %> | ||
|
||
<% if @article.errors.any? %> | ||
<div class="notice is-danger is-filled"> | ||
The following errors prevented this post from being saved: | ||
<ul> | ||
<% @article.errors.full_messages.each do |msg| %> | ||
<li><%= msg %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<%= render 'posts/image_upload' %> | ||
|
||
<%= form_for @article, url: edit_article_path(@article) do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :title, "Title your post:", class: "form-element" %> | ||
<%= f.text_field :title, class: "form-element" %> | ||
</div> | ||
|
||
<%= render 'shared/body_field', f: f, field_name: :body_markdown, field_label: 'Body' %> | ||
|
||
<div class="post-preview"></div> | ||
|
||
<div class="form-group"> | ||
<%= f.label :tags_cache, "Tags", class: "form-element" %> | ||
<div class="form-caption"> | ||
Tags help to categorize posts. Separate them by space. Use hyphens for multiple-word tags. | ||
</div> | ||
<%= f.select :tags_cache, options_for_select(@article.tags_cache.map { |t| [t, t] }, selected: @article.tags_cache), | ||
{ include_blank: true }, multiple: true, class: "form-element js-tag-select", | ||
data: { tag_set: @article.category.tag_set.id } %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= label_tag :edit_comment, 'Edit comment', class: "form-element" %> | ||
<div class="form-caption"> | ||
Describe—if necessary—what you are changing and why you are making this edit. | ||
</div> | ||
<%= text_field_tag :edit_comment, params[:edit_comment], class: 'form-element' %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= f.submit 'Save', class: "button is-filled" %><br/> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render 'form', is_edit: true %> |
Oops, something went wrong.