Skip to content

Commit

Permalink
Merge pull request #343 from izwick-schachter/autoflag-rss
Browse files Browse the repository at this point in the history
Added RSS feed for autoflagged posts
  • Loading branch information
ArtOfCode- committed Mar 7, 2018
2 parents 9e8a567 + 537ee6a commit 6c02632
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/controllers/api_controller.rb
Expand Up @@ -121,6 +121,9 @@ def search_posts
if params[:to_date].present?
@posts = @posts.where('`posts`.`created_at` < ?', DateTime.strptime(params[:to_date], '%s'))
end
if params[:autoflagged].present?
@posts = @posts.where(autoflagged: params[:autoflagged])
end
results = @posts.select(select_fields(filter)).order(id: :desc).paginate(page: params[:page], per_page: @pagesize)
render json: { items: results, has_more: has_more?(params[:page], results.count) }
end
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/rss_controller.rb
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class RSSController < ApplicationController
def autoflagged
@posts = Post.order('created_at DESC').paginate(page: params[:page], per_page: 50)
@posts = @posts.where(site: params[:site]) if params[:site].present?
if params[:deleted].present?
@posts = if params[:deleted].casecmp('true') == 0 || params[:deleted] == '1'
@posts.where.not(deleted_at: nil)
else
@posts.where(deleted_at: nil)
end
end
if params[:from_date].present?
@posts = @posts.where('`posts`.`created_at` > ?', DateTime.strptime(params[:from_date], '%s'))
end
if params[:to_date].present?
@posts = @posts.where('`posts`.`created_at` < ?', DateTime.strptime(params[:to_date], '%s'))
end
if params[:feedback_type].present?
@posts = @posts.includes(:feedbacks).where(feedbacks: { feedback_type: params[:feedback_type] })
end
respond_to do |format|
format.html
format.rss { render layout: false }
end
end
end
1 change: 1 addition & 0 deletions app/controllers/search_controller.rb
Expand Up @@ -111,6 +111,7 @@ def index
format.json do
render json: @results
end
format.rss { render :search, layout: false }
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/rss_helper.rb
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module RSSHelper
end
2 changes: 1 addition & 1 deletion app/views/posts/_post.html.erb
Expand Up @@ -46,7 +46,7 @@
by <%= link_to post.username, url_for(:controller => :stack_exchange_users, :action => :show, :id => post.stack_exchange_user.id) %>&#x202D;
<% end %>
<% if post.site.present? %>
<% if post.site.present? && @sites.find { |x| x.id == post.site_id }.present? %>
<%= link_to (image_tag @sites.find { |x| x.id == post.site_id }.site_logo, size: "20"), post.link %>
<% end %>
</span>
Expand Down
7 changes: 7 additions & 0 deletions app/views/posts/_table.html.erb
@@ -0,0 +1,7 @@
<table class="table" id="posts-index-table">
<tbody>
<% posts.each do |post| %>
<%= render "posts/post", :post => post %>
<% end %>
</tbody>
</table>
8 changes: 1 addition & 7 deletions app/views/posts/index.html.erb
Expand Up @@ -11,13 +11,7 @@
</li>
</ul>

<table class="table" id="posts-index-table">
<tbody>
<% @posts.each do |post| %>
<%= render "post", :post => post %>
<% end %>
</tbody>
</table>
<%= render 'table', posts: @posts %>

<div class="text-center">
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
Expand Down
7 changes: 7 additions & 0 deletions app/views/rss/autoflagged.html.erb
@@ -0,0 +1,7 @@
<h1>Autoflagged Posts</h1>

<%= render 'posts/table', posts: @posts %>

<div class="text-center">
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
</div>
20 changes: 20 additions & 0 deletions app/views/rss/autoflagged.rss.builder
@@ -0,0 +1,20 @@
# frozen_string_literal: true

xml.instruct! :xml, version: '1.0'
xml.rss version: '2.0' do
xml.channel do
xml.title 'Autoflagged Posts'
xml.description 'Posts which have been autoflagged by Charcoal HQ'
xml.link root_url

@posts.each do |post|
xml.item do
xml.title post.title
xml.description post.body
xml.pubDate post.created_at.iso8601
xml.link url_for(controller: 'posts', action: 'show', id: post.id)
xml.link url_for(controller: 'posts', action: 'show', id: post.id)
end
end
end
end
19 changes: 19 additions & 0 deletions app/views/search/search.rss.builder
@@ -0,0 +1,19 @@
# frozen_string_literal: true

xml.instruct! :xml, version: '1.0'
xml.rss version: '2.0' do
xml.channel do
xml.title 'Search Results'
xml.link root_url

@results.each do |post|
xml.item do
xml.title post.title
xml.description post.body
xml.pubDate post.created_at.iso8601
xml.link url_for(controller: 'posts', action: 'show', id: post.id)
xml.link url_for(controller: 'posts', action: 'show', id: post.id)
end
end
end
end
5 changes: 5 additions & 0 deletions config/initializers/inflections.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format. Inflections
Expand All @@ -15,3 +16,7 @@
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end

ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'RSS'
end
4 changes: 4 additions & 0 deletions config/routes.rb
Expand Up @@ -294,6 +294,10 @@

get 'sites/dash', to: 'dashboard#site_dash', as: :site_dash

scope 'rss' do
get 'autoflagged', to: 'rss#autoflagged'
end

devise_for :users, controllers: { sessions: 'custom_sessions' }
devise_scope :user do
get 'users/2fa/login', to: 'custom_sessions#verify_2fa'
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/rss_controller_test.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'test_helper'

class RSSControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 6c02632

Please sign in to comment.