aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/public_body_change_requests_controller.rb28
-rw-r--r--app/mailers/contact_mailer.rb19
-rw-r--r--app/models/public_body_change_request.rb97
-rw-r--r--app/views/contact_mailer/add_public_body.text.erb14
-rw-r--r--app/views/contact_mailer/update_public_body_email.text.erb11
-rw-r--r--app/views/public_body_change_requests/new.html.erb61
-rw-r--r--config/routes.rb2
-rw-r--r--db/migrate/20131211152641_create_public_body_change_requests.rb20
-rw-r--r--spec/controllers/public_body_change_requests_controller_spec.rb95
-rw-r--r--spec/models/public_body_change_request_spec.rb110
10 files changed, 457 insertions, 0 deletions
diff --git a/app/controllers/public_body_change_requests_controller.rb b/app/controllers/public_body_change_requests_controller.rb
new file mode 100644
index 000000000..4a6c5f5cb
--- /dev/null
+++ b/app/controllers/public_body_change_requests_controller.rb
@@ -0,0 +1,28 @@
+class PublicBodyChangeRequestsController < ApplicationController
+
+ def create
+ @change_request = PublicBodyChangeRequest.from_params(params[:public_body_change_request], @user)
+ if @change_request.save
+ @change_request.send_message
+ flash[:notice] = @change_request.thanks_notice
+ redirect_to frontpage_url
+ return
+ else
+ render :action => 'new'
+ end
+ end
+
+ def new
+ @change_request = PublicBodyChangeRequest.new
+ if params[:body]
+ @change_request.public_body = PublicBody.find_by_url_name_with_historic(params[:body])
+ end
+ if @change_request.public_body
+ @title = _('Ask us to update the email address for {{public_body_name}}',
+ :public_body_name => @change_request.public_body.name)
+ else
+ @title = _('Ask us to add an authority')
+ end
+
+ end
+end
diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb
index 4dc49bf8b..2952d6425 100644
--- a/app/mailers/contact_mailer.rb
+++ b/app/mailers/contact_mailer.rb
@@ -42,4 +42,23 @@ class ContactMailer < ApplicationMailer
:bcc => AlaveteliConfiguration::contact_email,
:subject => subject)
end
+
+ # Send a request to the administrator to add an authority
+ def add_public_body(change_request)
+ @change_request = change_request
+ mail(:from => MailHandler.address_from_name_and_email(@change_request.get_user_name, @change_request.get_user_email),
+ :to => contact_from_name_and_email,
+ :subject => _('Add authority - {{public_body_name}}',
+ :public_body_name => @change_request.get_public_body_name))
+ end
+
+ # Send a request to the administrator to update an authority email address
+ def update_public_body_email(change_request)
+ @change_request = change_request
+ mail(:from => MailHandler.address_from_name_and_email(@change_request.get_user_name, @change_request.get_user_email),
+ :to => contact_from_name_and_email,
+ :subject => _('Update email address - {{public_body_name}}',
+ :public_body_name => @change_request.get_public_body_name))
+ end
+
end
diff --git a/app/models/public_body_change_request.rb b/app/models/public_body_change_request.rb
new file mode 100644
index 000000000..6eda893fa
--- /dev/null
+++ b/app/models/public_body_change_request.rb
@@ -0,0 +1,97 @@
+# == Schema Information
+#
+# Table name: public_body_change_requests
+#
+# id :integer not null, primary key
+# user_email :string(255)
+# user_name :string(255)
+# user_id :integer
+# public_body_name :text
+# public_body_id :integer
+# public_body_email :string(255)
+# source_url :text
+# notes :text
+# is_open :boolean default(TRUE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class PublicBodyChangeRequest < ActiveRecord::Base
+
+ belongs_to :user
+ belongs_to :public_body
+ validates_presence_of :public_body_name, :message => N_("Please enter the name of the authority"),
+ :unless => proc{ |change_request| change_request.public_body }
+ validates_presence_of :user_name, :message => N_("Please enter your name"),
+ :unless => proc{ |change_request| change_request.user }
+ validates_presence_of :user_email, :message => N_("Please enter your email address"),
+ :unless => proc{ |change_request| change_request.user }
+ validate :user_email_format, :unless => proc{ |change_request| change_request.user_email.blank? }
+ validate :body_email_format, :unless => proc{ |change_request| change_request.public_body_email.blank? }
+
+ scope :new_body_requests, :conditions => ['public_body_id IS NULL'], :order => 'created_at'
+ scope :body_update_requests, :conditions => ['public_body_id IS NOT NULL'], :order => 'created_at'
+ scope :open, :conditions => ['is_open = ?', true]
+
+ def self.from_params(params, user)
+ change_request = new
+ change_request.update_from_params(params, user)
+ end
+
+ def update_from_params(params, user)
+ if user
+ self.user_id = user.id
+ else
+ self.user_name = params[:user_name]
+ self.user_email = params[:user_email]
+ end
+ self.public_body_name = params[:public_body_name]
+ self.public_body_id = params[:public_body_id]
+ self.public_body_email = params[:public_body_email]
+ self.source_url = params[:source_url]
+ self.notes = params[:notes]
+ self
+ end
+
+ def get_user_name
+ user ? user.name : user_name
+ end
+
+ def get_user_email
+ user ? user.email : user_email
+ end
+
+ def get_public_body_name
+ public_body ? public_body.name : public_body_name
+ end
+
+ def send_message
+ if public_body
+ ContactMailer.update_public_body_email(self).deliver
+ else
+ ContactMailer.add_public_body(self).deliver
+ end
+ end
+
+ def thanks_notice
+ if self.public_body
+ _("Your request to update the address for #{get_public_body_name} has been sent. Thank you for getting in touch! We'll get back to you soon.")
+ else
+ _("Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon.")
+ end
+ end
+
+ private
+
+ def body_email_format
+ unless MySociety::Validate.is_valid_email(self.public_body_email)
+ errors.add(:public_body_email, _("The authority email doesn't look like a valid address"))
+ end
+ end
+
+ def user_email_format
+ unless MySociety::Validate.is_valid_email(self.user_email)
+ errors.add(:user_email, _("Your email doesn't look like a valid address"))
+ end
+ end
+end
diff --git a/app/views/contact_mailer/add_public_body.text.erb b/app/views/contact_mailer/add_public_body.text.erb
new file mode 100644
index 000000000..7c7978e7a
--- /dev/null
+++ b/app/views/contact_mailer/add_public_body.text.erb
@@ -0,0 +1,14 @@
+<%= _("{{user_name}} would like a new authority added to {{site_name}}", :user_name => @change_request.get_user_name, :site_name => site_name) %>
+
+<%= _("Authority:") %>
+<%= @change_request.get_public_body_name %>
+
+<%= _("Email:") %>
+<%= @change_request.public_body_email %>
+
+<%= _("Source:") %>
+<%= @change_request.source_url %>
+
+<%= _("Notes:") %>
+<%= @change_request.notes %>
+
diff --git a/app/views/contact_mailer/update_public_body_email.text.erb b/app/views/contact_mailer/update_public_body_email.text.erb
new file mode 100644
index 000000000..db494b92c
--- /dev/null
+++ b/app/views/contact_mailer/update_public_body_email.text.erb
@@ -0,0 +1,11 @@
+<%= _("{{user_name}} would like the email address for {{public_body_name}} to be updated", :user_name => @change_request.get_user_name, :public_body_name => @change_request.get_public_body_name) %>
+
+<%= _("Email:") %>
+<%= @change_request.public_body_email %>
+
+<%= _("Source:") %>
+<%= @change_request.source_url %>
+
+<%= _("Notes:") %>
+<%= @change_request.notes %>
+
diff --git a/app/views/public_body_change_requests/new.html.erb b/app/views/public_body_change_requests/new.html.erb
new file mode 100644
index 000000000..7079cd868
--- /dev/null
+++ b/app/views/public_body_change_requests/new.html.erb
@@ -0,0 +1,61 @@
+<h1><%= @title %></h1>
+<%= foi_error_messages_for :change_request %>
+ <%= form_for(@change_request, :url => change_request_path) do |f| %>
+<% if not @user %>
+ <p>
+ <label class="form_label" for="user_name">
+ <%= _("Your name:") %>
+ </label>
+ <%= f.text_field :user_name %>
+ <%= _('(or <a href="{{url}}">sign in</a>)', :url => signin_path(:r => request.fullpath)) %>
+ </p>
+
+ <p>
+ <label class="form_label" for="user_email">
+ <%= ("Your email:") %>
+ </label>
+ <%= f.text_field :user_email %>
+ </p>
+<% end %>
+<% if @change_request.public_body %>
+ <%= f.hidden_field :public_body_id %>
+<% else %>
+ <p>
+ <label class="form_label" for="public_body_name">
+ <%= _("Authority:") %>
+ </label>
+ <%= f.text_field :public_body_name %>
+ </p>
+<% end %>
+ <p>
+ <label class="form_label" for="public_body_email">
+ <%= _("Authority email:") %>
+ </label>
+ <%= f.text_field :public_body_email %>
+ <div class="form_item_note">
+ <%= _("The contact email address for FOI requests to the authority.") %>
+ </div>
+ </p>
+
+ <p>
+ <label class="form_label" for="source_url">
+ <%= _("Source URL:") %>
+ </label>
+ <%= f.text_field :source_url %>
+ <div class="form_item_note">
+ <%= _("The URL where you found the email address. This field is optional, but it would help us a lot if you can provide a link to a specific page on the authority's website that gives this address, as it will make it much easier for us to check.") %>
+ </div>
+ </p>
+
+ <p>
+ <label class="form_label" for="notes">
+ <%= _("Notes:") %>
+ </label>
+ <%= f.text_area :notes, :rows => 10, :cols => 60 %>
+ </p>
+
+ <div class="form_button">
+ <%= submit_tag _("Submit request") %>
+ </div>
+
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index cadb7ec54..a70be2492 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -108,6 +108,8 @@ Alaveteli::Application.routes.draw do
match '/body_statistics' => 'public_body#statistics', :as => :public_bodies_statistics
####
+ resource :change_request, :only => [:new, :create], :controller => 'public_body_change_requests'
+
#### Comment controller
match '/annotate/request/:url_title' => 'comment#new', :as => :new_comment, :type => 'request'
####
diff --git a/db/migrate/20131211152641_create_public_body_change_requests.rb b/db/migrate/20131211152641_create_public_body_change_requests.rb
new file mode 100644
index 000000000..e3fb560a6
--- /dev/null
+++ b/db/migrate/20131211152641_create_public_body_change_requests.rb
@@ -0,0 +1,20 @@
+class CreatePublicBodyChangeRequests < ActiveRecord::Migration
+ def up
+ create_table :public_body_change_requests do |t|
+ t.column :user_email, :string
+ t.column :user_name, :string
+ t.column :user_id, :integer
+ t.column :public_body_name, :text
+ t.column :public_body_id, :integer
+ t.column :public_body_email, :string
+ t.column :source_url, :text
+ t.column :notes, :text
+ t.column :is_open, :boolean, :null => false, :default => true
+ t.timestamps
+ end
+ end
+
+ def down
+ drop_table :public_body_change_requests
+ end
+end
diff --git a/spec/controllers/public_body_change_requests_controller_spec.rb b/spec/controllers/public_body_change_requests_controller_spec.rb
new file mode 100644
index 000000000..00d9c7d40
--- /dev/null
+++ b/spec/controllers/public_body_change_requests_controller_spec.rb
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyChangeRequestsController, "making a new change request" do
+
+ it "should show the form" do
+ get :new
+ response.should render_template("new")
+ end
+
+end
+
+describe PublicBodyChangeRequestsController, "creating a change request" do
+
+ context 'when handling a request for a new authority' do
+
+ before do
+ @email = "test@example.com"
+ name = "Test User"
+ @change_request_params = {:user_email => @email,
+ :user_name => name,
+ :public_body_name => 'New Body',
+ :public_body_email => 'new_body@example.com',
+ :notes => 'Please',
+ :source => 'http://www.example.com'}
+ end
+
+ it "should send an email to the site contact address" do
+ post :create, {:public_body_change_request => @change_request_params}
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.subject.should =~ /Add authority - New Body/
+ mail.from.should include(@email)
+ mail.to.should include('postmaster@localhost')
+ mail.body.should include('new_body@example.com')
+ mail.body.should include('New Body')
+ mail.body.should include("Please")
+ end
+
+ it 'should show a notice' do
+ post :create, {:public_body_change_request => @change_request_params}
+ expected_text = "Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon."
+ flash[:notice].should == expected_text
+ end
+
+ it 'should redirect to the frontpage' do
+ post :create, {:public_body_change_request => @change_request_params}
+ response.should redirect_to frontpage_url
+ end
+
+ end
+
+ context 'when handling a request for an update to an existing authority' do
+
+ before do
+ @email = "test@example.com"
+ name = "Test User"
+ @public_body = FactoryGirl.create(:public_body)
+ @change_request_params = {:user_email => @email,
+ :user_name => name,
+ :public_body_id => @public_body.id,
+ :public_body_email => 'new_body@example.com',
+ :notes => 'Please',
+ :source => 'http://www.example.com'}
+ end
+
+ it 'should send an email to the site contact address' do
+ post :create, {:public_body_change_request => @change_request_params}
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.subject.should =~ /Update email address - #{@public_body.name}/
+ mail.from.should include(@email)
+ mail.to.should include('postmaster@localhost')
+ mail.body.should include('new_body@example.com')
+ mail.body.should include(@public_body.name)
+ mail.body.should include("Please")
+ end
+
+ it 'should show a notice' do
+ post :create, {:public_body_change_request => @change_request_params}
+ expected_text = "Your request to update the address for #{@public_body.name} has been sent. Thank you for getting in touch! We'll get back to you soon."
+ flash[:notice].should == expected_text
+ end
+
+ it 'should redirect to the frontpage' do
+ post :create, {:public_body_change_request => @change_request_params}
+ response.should redirect_to frontpage_url
+ end
+
+ end
+
+
+end
diff --git a/spec/models/public_body_change_request_spec.rb b/spec/models/public_body_change_request_spec.rb
new file mode 100644
index 000000000..653a218e3
--- /dev/null
+++ b/spec/models/public_body_change_request_spec.rb
@@ -0,0 +1,110 @@
+# == Schema Information
+#
+# Table name: public_body_change_requests
+#
+# id :integer not null, primary key
+# user_email :string(255)
+# user_name :string(255)
+# user_id :integer
+# public_body_name :text
+# public_body_id :integer
+# public_body_email :string(255)
+# source_url :text
+# notes :text
+# is_open :boolean default(TRUE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyChangeRequest, 'when validating' do
+
+ it 'should not be valid without a public body name' do
+ change_request = PublicBodyChangeRequest.new()
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_name].should == ['Please enter the name of the authority']
+ end
+
+ it 'should not be valid without a user name if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_name].should == ['Please enter your name']
+ end
+
+ it 'should not be valid without a user email address if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ['Please enter your email address']
+ end
+
+ it 'should be valid with a user and no name or email address' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user,
+ :public_body_name => 'New Body')
+ change_request.valid?.should be_true
+ end
+
+ it 'should validate the format of a user email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :user_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ["Your email doesn't look like a valid address"]
+ end
+
+ it 'should validate the format of a public body email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :public_body_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_email].should == ["The authority email doesn't look like a valid address"]
+ end
+
+end
+
+describe PublicBodyChangeRequest, 'get_user_name' do
+
+ it 'should return the user_name field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_name => 'Test User')
+ change_request.get_user_name.should == 'Test User'
+ end
+
+ it 'should return the name of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_name.should == user.name
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_user_email' do
+
+ it 'should return the user_email field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_email => 'user@example.com')
+ change_request.get_user_email.should == 'user@example.com'
+ end
+
+ it 'should return the email of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_email.should == user.email
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_public_body_name' do
+
+ it 'should return the public_body_name field if there is no public body association' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Authority')
+ change_request.get_public_body_name.should == 'Test Authority'
+ end
+
+ it 'should return the name of the associated public body if there is one' do
+ public_body = FactoryGirl.build(:public_body)
+ change_request = PublicBodyChangeRequest.new(:public_body => public_body)
+ change_request.get_public_body_name.should == public_body.name
+ end
+
+end
+