Add /post_replacements.json, /posts/1234/replacements.json.

This commit is contained in:
evazion
2017-05-14 18:49:57 -05:00
parent 78b08d8394
commit cb09b6661d
5 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
class PostReplacementsController < ApplicationController
respond_to :html, :xml, :json
before_filter :approver_only, except: [:index]
def index
params[:search][:post_id] = params.delete(:post_id) if params.has_key?(:post_id)
@post_replacements = PostReplacement.search(params[:search]).paginate(params[:page], limit: params[:limit])
respond_with(@post_replacements)
end
end

View File

@@ -61,4 +61,32 @@ class PostReplacement < ActiveRecord::Base
post.distribute_files
post.update_iqdb_async
end
module SearchMethods
def search(params = {})
q = all
if params[:creator_id].present?
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
end
if params[:creator_name].present?
q = q.where(creator_name: User.name_to_id(params[:creator_name]))
end
if params[:id].present?
q = q.where(id: params[:id].split(",").map(&:to_i))
end
if params[:post_id].present?
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
end
q = q.order("created_at DESC")
q
end
end
extend SearchMethods
end

View File

@@ -199,6 +199,7 @@ Rails.application.routes.draw do
end
resources :posts do
resources :events, :only => [:index], :controller => "post_events"
resources :replacements, :only => [:index], :controller => "post_replacements"
resource :artist_commentary, :only => [:index, :show] do
collection { put :create_or_update }
member { put :revert }
@@ -217,6 +218,7 @@ Rails.application.routes.draw do
end
resources :post_appeals
resources :post_flags
resources :post_replacements, :only => [:index]
resources :post_versions, :only => [:index, :search] do
member do
put :undo

View File

@@ -0,0 +1,6 @@
FactoryGirl.define do
factory(:post_replacement) do
original_url { FFaker::Internet.http_url }
replacement_url { FFaker::Internet.http_url }
end
end

View File

@@ -0,0 +1,21 @@
require 'test_helper'
class PostReplacementsControllerTest < ActionController::TestCase
context "The post replacements controller" do
setup do
@user = FactoryGirl.create(:user, can_approve_posts: true)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
@post_replacement = FactoryGirl.create(:post_replacement, post_id: @post.id)
end
context "index action" do
should "render" do
get :index, {format: :json}
assert_response :success
end
end
end
end