Create mute rules AJAX controller
This commit is contained in:
parent
c31ab3578d
commit
6c0e6b1d30
|
@ -0,0 +1,67 @@
|
|||
class Ajax::MuteRuleController < AjaxController
|
||||
def create
|
||||
params.require :muted_phrase
|
||||
|
||||
unless user_signed_in?
|
||||
@response[:status] = :noauth
|
||||
@response[:message] = I18n.t('messages.noauth')
|
||||
return
|
||||
end
|
||||
|
||||
rule = MuteRule.create!(user: current_user, muted_phrase: params[:muted_phrase])
|
||||
@response[:status] = :okay
|
||||
@response[:success] = true
|
||||
@response[:message] = "Rule added successfully."
|
||||
@response[:id] = rule.id
|
||||
end
|
||||
|
||||
def update
|
||||
params.require :id
|
||||
params.require :muted_phrase
|
||||
|
||||
unless user_signed_in?
|
||||
@response[:status] = :noauth
|
||||
@response[:message] = I18n.t('messages.noauth')
|
||||
return
|
||||
end
|
||||
|
||||
rule = MuteRule.find(params[:id])
|
||||
|
||||
if rule.user_id != current_user.id
|
||||
@response[:status] = :nopriv
|
||||
@response[:message] = "Can't edit other people's rules"
|
||||
return
|
||||
end
|
||||
|
||||
rule.muted_phrase = params[:muted_phrase]
|
||||
rule.save!
|
||||
|
||||
@response[:status] = :okay
|
||||
@response[:message] = "Rule updated successfully."
|
||||
@response[:success] = true
|
||||
end
|
||||
|
||||
def destroy
|
||||
params.require :id
|
||||
|
||||
unless user_signed_in?
|
||||
@response[:status] = :noauth
|
||||
@response[:message] = I18n.t('messages.noauth')
|
||||
return
|
||||
end
|
||||
|
||||
rule = MuteRule.find(params[:id])
|
||||
|
||||
if rule.user_id != current_user.id
|
||||
@response[:status] = :nopriv
|
||||
@response[:message] = "Can't edit other people's rules"
|
||||
return
|
||||
end
|
||||
|
||||
rule.destroy!
|
||||
|
||||
@response[:status] = :okay
|
||||
@response[:message] = "Rule deleted successfully."
|
||||
@response[:success] = true
|
||||
end
|
||||
end
|
|
@ -114,6 +114,9 @@ Rails.application.routes.draw do
|
|||
match '/list_membership', to: 'list#membership', via: :post, as: :list_membership
|
||||
match '/subscribe', to: 'subscription#subscribe', via: :post, as: :subscribe_answer
|
||||
match '/unsubscribe', to: 'subscription#unsubscribe', via: :post, as: :unsubscribe_answer
|
||||
match '/mute', to: 'mute_rule#create', via: :post, as: :create_mute_rule
|
||||
match '/mute/:id', to: 'mute_rule#update', via: :post, as: :update_mute_rule
|
||||
match '/mute/:id', to: 'mute_rule#destroy', via: :delete, as: :delete_mute_rule
|
||||
end
|
||||
|
||||
match '/discover', to: 'discover#index', via: :get, as: :discover
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe Ajax::MuteRuleController, :ajax_controller, type: :controller do
|
||||
|
||||
describe "#create" do
|
||||
subject { post(:create, params: params) }
|
||||
|
||||
context "when user is signed in" do
|
||||
before(:each) { sign_in(user) }
|
||||
|
||||
let(:params) { { muted_phrase: 'test' } }
|
||||
let(:expected_response) do
|
||||
{
|
||||
"success" => true,
|
||||
"status" => "okay",
|
||||
"id" => MuteRule.last.id,
|
||||
"message" => "Rule added successfully.",
|
||||
}
|
||||
end
|
||||
|
||||
it "creates a mute rule" do
|
||||
expect { subject }.to change { MuteRule.count }.by(1)
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
rule = MuteRule.first
|
||||
expect(rule.user_id).to eq(user.id)
|
||||
expect(rule.muted_phrase).to eq('test')
|
||||
end
|
||||
|
||||
include_examples "returns the expected response"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#update" do
|
||||
subject { post(:update, params: params) }
|
||||
|
||||
context "when user is signed in" do
|
||||
before(:each) { sign_in(user) }
|
||||
|
||||
let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') }
|
||||
let(:params) { { id: rule.id, muted_phrase: 'dogs' } }
|
||||
let(:expected_response) do
|
||||
{
|
||||
"success" => true,
|
||||
"status" => "okay",
|
||||
"message" => "Rule updated successfully."
|
||||
}
|
||||
end
|
||||
|
||||
it "updates a mute rule" do
|
||||
subject
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
expect(rule.reload.muted_phrase).to eq('dogs')
|
||||
end
|
||||
|
||||
include_examples "returns the expected response"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
subject { delete(:destroy, params: params) }
|
||||
|
||||
context "when user is signed in" do
|
||||
before(:each) { sign_in(user) }
|
||||
|
||||
let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') }
|
||||
let(:params) { { id: rule.id } }
|
||||
let(:expected_response) do
|
||||
{
|
||||
"success" => true,
|
||||
"status" => "okay",
|
||||
"message" => "Rule deleted successfully."
|
||||
}
|
||||
end
|
||||
|
||||
it "deletes a mute rule" do
|
||||
subject
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
expect(MuteRule.exists?(rule.id)).to eq(false)
|
||||
|
||||
end
|
||||
|
||||
include_examples "returns the expected response"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue