From 9a149c41ad3f4e152f111336126b6381d09e2f17 Mon Sep 17 00:00:00 2001 From: ArtOfCode- Date: Mon, 8 Jun 2020 00:22:14 +0100 Subject: [PATCH 1/9] Start on articles --- app/models/article.rb | 7 +++++++ db/seeds/post_types.yml | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 app/models/article.rb diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 000000000..127ebc0aa --- /dev/null +++ b/app/models/article.rb @@ -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 \ No newline at end of file diff --git a/db/seeds/post_types.yml b/db/seeds/post_types.yml index c957d9cfd..ae17f34ee 100644 --- a/db/seeds/post_types.yml +++ b/db/seeds/post_types.yml @@ -1,4 +1,5 @@ - name: Question - name: Answer - name: PolicyDoc -- name: HelpDoc \ No newline at end of file +- name: HelpDoc +- name: Article \ No newline at end of file From da16f3c18597755937bb8e2cf49452727c3eda56 Mon Sep 17 00:00:00 2001 From: ArtOfCode- Date: Mon, 8 Jun 2020 21:03:09 +0100 Subject: [PATCH 2/9] Article controllers --- app/assets/javascripts/articles.coffee | 3 ++ app/assets/stylesheets/articles.scss | 3 ++ app/controllers/articles_controller.rb | 42 ++++++++++++++++++++ app/helpers/application_helper.rb | 26 ++++++++++++ app/helpers/articles_helper.rb | 2 + app/views/articles/show.html.erb | 8 ++++ app/views/posts/_article_list.html.erb | 26 ++++++++++++ app/views/posts/_expanded.html.erb | 17 ++++---- app/views/posts/_type_agnostic.html.erb | 3 +- config/routes.rb | 5 +++ test/controllers/articles_controller_test.rb | 7 ++++ 11 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 app/assets/javascripts/articles.coffee create mode 100644 app/assets/stylesheets/articles.scss create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 app/views/articles/show.html.erb create mode 100644 app/views/posts/_article_list.html.erb create mode 100644 test/controllers/articles_controller_test.rb diff --git a/app/assets/javascripts/articles.coffee b/app/assets/javascripts/articles.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/articles.coffee @@ -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/ diff --git a/app/assets/stylesheets/articles.scss b/app/assets/stylesheets/articles.scss new file mode 100644 index 000000000..e77f17a9e --- /dev/null +++ b/app/assets/stylesheets/articles.scss @@ -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/ diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 000000000..695b0e164 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,42 @@ +class ArticlesController < ApplicationController + before_action :set_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(&:empty?), + body: body_rendered, last_activity: DateTime.now, + last_activity_by: current_user)) + redirect_to article_path(@article) + else + render :edit + end + end + + private + + def set_article + @article = Article.find params[:id] + end + + def article_params + params.require(:article).permit(:body_markdown, :title, :tags_cache) + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 78fdcce45..7f6c1a8eb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -75,4 +75,30 @@ def strip_markdown(markdown) markdown end + + def generic_share_link(post) + case post.post_type_id + when Question.post_type_id + share_question_url(post) + when Answer.post_type_id + share_answer_url(qid: post.parent_id, id: post.id) + when Article.post_type_id + share_article_url(post) + else + '#' + end + end + + def generic_edit_link(post) + case post.post_type_id + when Question.post_type_id + edit_question_url(post) + when Answer.post_type_id + edit_answer_url(post) + when Article.post_type_id + edit_article_url(post) + else + '#' + end + end end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 000000000..296827759 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 000000000..0e822e298 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,8 @@ +<% content_for :title, @article.title.truncate(50) %> +<% content_for :description do %> + <% Rails.cache.fetch "posts/#{@article.id}/description" do %> + <%= @article.body_plain[0..74].strip %>... + <% end %> +<% end %> + +<%= render 'posts/expanded', post: @article %> \ No newline at end of file diff --git a/app/views/posts/_article_list.html.erb b/app/views/posts/_article_list.html.erb new file mode 100644 index 000000000..63d781a05 --- /dev/null +++ b/app/views/posts/_article_list.html.erb @@ -0,0 +1,26 @@ +<% active_user = post.last_activity_by || post.user %> +
+
+ <%= post.score %> + score +
+
+
+ <%= link_to post.title, share_article_path(post) %> +
+

+ last activity <%= time_ago_in_words(post.last_activity) %> ago by <%= link_to active_user.username, user_path(active_user) %> +

+
+ <% tag_set = post.tag_set %> + <% required_ids = defined?(@category) ? @category&.required_tag_ids : post.category&.required_tag_ids %> + <% topic_ids = defined?(@category) ? @category&.topic_tag_ids : post.category&.topic_tag_ids %> + <% category_sort_tags(post.tags, required_ids, topic_ids).each do |tag| %> + <% required = required_ids&.include? tag.id %> + <% topic = topic_ids&.include? tag.id %> + <%= link_to tag.name, questions_tagged_path(tag_set: tag_set.id, tag: tag.name), + class: "badge is-tag #{required ? 'is-filled' : ''} #{topic ? 'is-outlined' : ''}" %> + <% end %> +
+
+
diff --git a/app/views/posts/_expanded.html.erb b/app/views/posts/_expanded.html.erb index 7c40682cd..9c4156919 100644 --- a/app/views/posts/_expanded.html.erb +++ b/app/views/posts/_expanded.html.erb @@ -1,13 +1,12 @@ <% is_question = post.post_type_id == Question.post_type_id %> +<% is_top_level = post.parent_id.nil? %> +<% has_tags = is_top_level && !post.tag_ids.empty? %> -
- <% if is_question %> +
+ <% if is_top_level %>

- <% if post.meta? %> - meta - <% end %> <%= post.title %> - <%= post.closed ? "[closed]" : "" %> + <%= is_question && post.closed ? "[closed]" : "" %>

<% end %> @@ -95,7 +94,7 @@
- <% if is_question %> + <% if has_tags %>