diff options
-rw-r--r-- | app/controllers/admin_spam_addresses_controller.rb | 27 | ||||
-rw-r--r-- | app/views/admin_spam_addresses/index.html.erb | 39 | ||||
-rw-r--r-- | config/routes.rb | 8 | ||||
-rw-r--r-- | spec/controllers/admin_spam_addresses_controller_spec.rb | 91 |
4 files changed, 165 insertions, 0 deletions
diff --git a/app/controllers/admin_spam_addresses_controller.rb b/app/controllers/admin_spam_addresses_controller.rb new file mode 100644 index 000000000..f5c7e93da --- /dev/null +++ b/app/controllers/admin_spam_addresses_controller.rb @@ -0,0 +1,27 @@ +class AdminSpamAddressesController < AdminController + + def index + @spam_addresses = SpamAddress.all + @spam_address = SpamAddress.new + end + + def create + @spam_address = SpamAddress.new(params[:spam_address]) + + if @spam_address.save + notice = "#{ @spam_address.email } has been added to the spam addresses list" + redirect_to spam_addresses_path, :notice => notice + else + @spam_addresses = SpamAddress.all + render :index + end + end + + def destroy + @spam_address = SpamAddress.find(params[:id]) + @spam_address.destroy + notice = "#{ @spam_address.email } has been removed from the spam addresses list" + redirect_to spam_addresses_path, :notice => notice + end + +end diff --git a/app/views/admin_spam_addresses/index.html.erb b/app/views/admin_spam_addresses/index.html.erb new file mode 100644 index 000000000..ec557b142 --- /dev/null +++ b/app/views/admin_spam_addresses/index.html.erb @@ -0,0 +1,39 @@ +<% @title = 'Spam Addresses' %> + +<h1><%= @title %></h1> + +<div class="row"> + <div class="span12"> + <%= form_for(@spam_address, :html => { :class => 'form-inline' }) do |f| -%> + <%= error_messages_for @spam_address %> + <%= f.text_field :email, :class => 'input-xxlarge', :placeholder => 'Enter email' %> + <%= f.submit 'Add Spam Address', :class => 'btn btn-warning' %> + <% end -%> + </div> +</div> + +<hr /> + +<% if @spam_addresses.any? %> + <div class="row"> + <table class="table table-hover span12"> + <thead> + <tr> + <th>Email</th> + <th></th> + </tr> + </thead> + <tbody> + <% @spam_addresses.each do |spam| %> + <tr> + <td><%= spam.email %></td> + <td><%= link_to 'Remove', spam, + :method => :delete, + :confirm => 'This is permanent! Are you sure?', + :class => 'btn btn-mini btn-danger' %></td> + </tr> + <% end %> + </tbody> + </table> + </div> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 1079fbe14..d9d21f0bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -248,6 +248,14 @@ Alaveteli::Application.routes.draw do match '/admin/censor/destroy/:censor_rule_id' => 'admin_censor_rule#destroy', :as => :admin_rule_destroy #### + #### AdminSpamAddresses controller + scope '/admin' do + resources :spam_addresses, + :controller => 'admin_spam_addresses', + :only => [:index, :create, :destroy] + end + #### + #### Api controller match '/api/v2/request.json' => 'api#create_request', :as => :api_create_request, :via => :post diff --git a/spec/controllers/admin_spam_addresses_controller_spec.rb b/spec/controllers/admin_spam_addresses_controller_spec.rb new file mode 100644 index 000000000..da1e9bb5a --- /dev/null +++ b/spec/controllers/admin_spam_addresses_controller_spec.rb @@ -0,0 +1,91 @@ +require 'spec_helper' + +describe AdminSpamAddressesController do + render_views + before { basic_auth_login @request } + + describe :index do + + it 'lists the spam addresses' do + 3.times { FactoryGirl.create(:spam_address) } + get :index + assigns(:spam_addresses).should == SpamAddress.all + end + + it 'creates a new spam address for the form' do + get :index + expect(assigns(:spam_address)).to be_a_new(SpamAddress) + end + + it 'renders the index template' do + get :index + expect(response).to render_template('index') + end + + end + + describe :create do + + let(:spam_params) { FactoryGirl.attributes_for(:spam_address) } + + it 'creates a new spam address with the given parameters' do + post :create, :spam_address => spam_params + assigns(:spam_address).email.should == spam_params[:email] + assigns(:spam_address).should be_persisted + end + + it 'redirects to the index action if successful' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + expect(response).to redirect_to(spam_addresses_path) + end + + it 'notifies the admin the spam address has been created' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + msg = "#{ spam_params[:email] } has been added to the spam addresses list" + flash[:notice].should == msg + end + + it 'renders the index action if the address could not be saved' do + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + expect(response).to render_template('index') + end + + it 'collects the spam addresses if the address could not be saved' do + 3.times { FactoryGirl.create(:spam_address) } + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + assigns(:spam_addresses).should == SpamAddress.all + end + + end + + describe :delete do + + before(:each) do + @spam = FactoryGirl.create(:spam_address) + delete :destroy, :id => @spam.id + end + + it 'finds the spam address to delete' do + assigns(:spam_address).should == @spam + end + + it 'destroys the spam address' do + assigns(:spam_address).should be_destroyed + end + + it 'tells the admin the spam address has been deleted' do + msg = "#{ @spam.email } has been removed from the spam addresses list" + flash[:notice].should == msg + end + + it 'redirects to the index action' do + expect(response).to redirect_to(spam_addresses_path) + end + + end + +end |