Skip to content

Commit

Permalink
Merge pull request #1208 from hisayohorie/person_contributes_councill…
Browse files Browse the repository at this point in the history
…or_info

Add form for a person to contribute suggested councillor info

Yay! Well done @hisayohorie 🎆
  • Loading branch information
equivalentideas authored Aug 2, 2017
2 parents 8d15435 + 74466ea commit 2f9e80c
Show file tree
Hide file tree
Showing 28 changed files with 507 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ You will also need to [configure the app to accept replies from councillors](#ac

#### Global feature flag

##### Writing to councillors feature
You can toggle the availability of the writing to councillors feature on or off for the entire site with the environment variable `COUNCILLORS_ENABLED`.
The feature is globally enabled when the value of `ENV["COUNCILLORS_ENABLED"]` is `"true"`.
This flag is useful if you need to turn the feature _off_ globally.
Expand All @@ -109,6 +110,17 @@ We set this in the [`.env`](https://github.com/openaustralia/planningalerts/blob
```
COUNCILLORS_ENABLED=true
```
##### Contributing suggested councillors feature
Similarly, you can toggle the availability of the contributing suggested councillors feature on or off for the entire site with the environment variable `CONTRIBUTE_COUNCILLORS_ENABLED`.
The feature is globally enabled when the value of `ENV["CONTRIBUTE_COUNCILLORS_ENABLED"]` is `"true"`.
This flag is useful if you need to turn the feature _off_ globally.

We set this in the [`.env`](https://github.com/openaustralia/planningalerts/blob/master/.env) file in production. You can control setting in development by creating your own `.env.development` file which includes:

```
CONTRIBUTE_COUNCILLORS_ENABLED=true
```


#### Set the reply address for accepting responses

Expand Down
8 changes: 8 additions & 0 deletions app/admin/suggested_councillors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ActiveAdmin.register SuggestedCouncillor do
actions :index
index do
column :authority
column :name
column :email
end
end
1 change: 1 addition & 0 deletions app/assets/stylesheets/partials/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,4 @@ form.donate {
@import "special_forms/autocomplete";
@import "special_forms/comment_form";
@import "special_forms/payment_form";
@import "special_forms/councillor_contribution_form";
45 changes: 45 additions & 0 deletions app/assets/stylesheets/partials/_grid.scss
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,48 @@
}
}
}

.councillor-contributions {
@include at-breakpoint(60em) {
@include span-columns(8, 12);
float: none;
margin-left: auto;
margin-right: auto;
}
}

.councillor-contribution-form {
.suggested-councillor-input-wrapper {
@include at-breakpoint(30em) {
display: flex;
justify-content: space-between;
}
}

.suggested-councillor-input-group {
@include at-breakpoint(30em) {
width: 50%;
box-sizing: border-box;
}
}

input {
box-sizing: border-box;
}
}

.suggested-councillor-input-group {
@include at-breakpoint(30em) {
padding-right: 1em;
}

input {
width: 100%;
}

& + & {
@include at-breakpoint(30em) {
padding-right: 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// TODO: These forms styles are very repetitive with #new_add_comment and .formtastic child styles
// They just don't have the .formatastic overrides.
// There is probably a refactor to make these the base/raw form styles
// and to move them into _form.scss
.councillor-contribution-form {
.councillor-contribution-label {
display: block;
margin: .75em 0 .25em;
padding: 0;
font-size: 1em;
font-weight: 500;
}

input {
display: block;
padding: .25em;
font-family: $blueprint-font-family;
font-size: 100%;
}

fieldset {
margin-bottom: 1em;
}

ul {
list-style: none;
padding: 0;
margin: 0;
}
}

.councillor-contribution-councillors {
padding-bottom: 2em;

input,
dd {
font-size: 1.2em;
}

dt {
color: $cool-highlight;
}
}

.councillor-contribution-actions {
text-align: right;
}
34 changes: 34 additions & 0 deletions app/controllers/contributors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ContributorsController < ApplicationController
before_action :check_if_feature_flag_is_on

def new
@contributor = Contributor.new
end

def create
@contributor = Contributor.new(contributor_params)
@councillor_contribution = CouncillorContribution.find(params[:contributor][:councillor_contribution_id])
if @contributor.save
@councillor_contribution.update(contributor: @contributor)
flash[:notice] = "Thank you"
redirect_to root_url
end
end

def no_contributor_info
flash[:notice] = "Thank you"
redirect_to root_url
end

private

def contributor_params
params.require(:contributor).permit(:name, :email)
end

def check_if_feature_flag_is_on
unless ENV["CONTRIBUTE_COUNCILLORS_ENABLED"] == "true"
render "static/error_404", status: 404
end
end
end
38 changes: 38 additions & 0 deletions app/controllers/councillor_contributions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class CouncillorContributionsController < ApplicationController
before_action :check_if_feature_flag_is_on

def new
@authority = Authority.find_by_short_name_encoded!(params[:authority_id])

if params["councillor_contribution"]
@councillor_contribution = @authority.councillor_contributions.build(councillor_contribution_params)
else
@councillor_contribution = CouncillorContribution.new
end

@councillor_contribution.suggested_councillors.build({email: nil, name: nil})
end

def create
@authority = Authority.find_by_short_name_encoded!(params[:authority_id])
@councillor_contribution = @authority.councillor_contributions.build(councillor_contribution_params)

if @councillor_contribution.save
redirect_to new_contributor_url(councillor_contribution_id: @councillor_contribution.id)
else
render :new
end
end

private

def councillor_contribution_params
params.require(:councillor_contribution).permit(suggested_councillors_attributes: [:name, :email])
end

def check_if_feature_flag_is_on
unless ENV["CONTRIBUTE_COUNCILLORS_ENABLED"].present?
render "static/error_404", status: 404
end
end
end
1 change: 1 addition & 0 deletions app/models/authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Authority < ActiveRecord::Base
has_many :applications
has_many :councillors
has_many :comments, through: :applications
has_many :councillor_contributions

validates :short_name, presence: true, uniqueness: { case_sensitive: false }

Expand Down
3 changes: 3 additions & 0 deletions app/models/contributor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Contributor < ActiveRecord::Base
has_many :councillor_contributions
end
6 changes: 6 additions & 0 deletions app/models/councillor_contribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CouncillorContribution < ActiveRecord::Base
belongs_to :contributor
belongs_to :authority
has_many :suggested_councillors, inverse_of: :councillor_contribution
accepts_nested_attributes_for :suggested_councillors, reject_if: :all_blank
end
5 changes: 5 additions & 0 deletions app/models/suggested_councillor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SuggestedCouncillor < ActiveRecord::Base
has_one :authority, through: :councillor_contribution
belongs_to :councillor_contribution
validates :councillor_contribution, :name, :email, presence: true
end
20 changes: 20 additions & 0 deletions app/views/contributors/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%h1 Thank you!
%p
This data you contributed will be reviewed
by an administrator, and go live shortly.
= form_for @contributor do |f|
= f.hidden_field :councillor_contribution_id, value: params[:councillor_contribution_id]
%fieldset
%legend
Please tell us about yourself, so we can send you a little note of appreciation
and updates about your contribution when it goes live.
%p This field is optional.
%p
= f.label :name
= f.text_field :name
%p
= f.label :email
= f.text_field :email
%p.button
= f.submit "Submit"
= link_to "I prefer not to", contributors_no_info_path
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%fieldset
.suggested-councillor-input-wrapper
= f.fields_for :suggested_councillors, @councillor_contribution.suggested_councillors.last do |suggested_councillor_field|
.suggested-councillor-input-group
= suggested_councillor_field.label :name, "Full name", class: "councillor-contribution-label"
= suggested_councillor_field.text_field :name
.suggested-councillor-input-group
= suggested_councillor_field.label :email, class: "councillor-contribution-label"
= suggested_councillor_field.text_field :email
23 changes: 23 additions & 0 deletions app/views/councillor_contributions/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.councillor-contributions
%h1.page-title Add a new councillor for #{@authority.full_name}
= form_for [@authority, @councillor_contribution], url: authority_councillor_contributions_path, html: { class: "councillor-contribution-form" } do |f|
- @councillor_contribution.suggested_councillors[0...-1].each do |s|
= f.fields_for :suggested_councillors, @councillor_contribution.suggested_councillors.last do |suggested_councillor_field|
= suggested_councillor_field.hidden_field :name, value: s.name
= suggested_councillor_field.hidden_field :email, value: s.email
.councillor-contribution-councillors
- if @councillor_contribution.suggested_councillors.many?
%ul
- @councillor_contribution.suggested_councillors[0...-1].each do |s|
%li
%dl.suggested-councillor-input-wrapper
.suggested-councillor-input-group
%dt.councillor-contribution-label Full name
%dd= s.name
.suggested-councillor-input-group
%dt.councillor-contribution-label Email
%dd= s.email
= render 'contribution_form_input', f: f
%button{formaction: new_authority_councillor_contribution_path, class: "button"} Add another councillor
.councillor-contribution-actions
= f.submit "Submit #{pluralize(@councillor_contribution.suggested_councillors.length, "new councillor")}", class: "button-action"
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,17 @@ def matches?(request)
get :per_week
end
end
resources :councillor_contributions, only:[:new, :create]
collection do
get :test_feed
end
end

post "/authorities/:authority_id/councillor_contributions/new", to: "councillor_contributions#new"

resources :contributors, only:[:new, :create]
get "/contributors/no_info", to: "contributors#no_contributor_info"

namespace :atdis do
get :test
post :test, action: 'test_redirect'
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20170616171451_create_suggested_councillors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSuggestedCouncillors < ActiveRecord::Migration
def change
create_table :suggested_councillors do |t|
t.string :name
t.string :email
t.integer :authority_id

t.timestamps null: false
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20170623020945_create_contributors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateContributors < ActiveRecord::Migration
def change
create_table :contributors do |t|
t.string :name
t.string :email

t.timestamps null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContributorIdToSuggestedCouncillors < ActiveRecord::Migration
def change
add_column :suggested_councillors, :contributor_id, :integer
end
end
10 changes: 10 additions & 0 deletions db/migrate/20170704043739_create_councillor_contributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCouncillorContributions < ActiveRecord::Migration
def change
create_table :councillor_contributions do |t|
t.integer :contributor_id
t.integer :authority_id

t.timestamps null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCouncillorContributionIdInSuggestedCouncillors < ActiveRecord::Migration
def change
add_column :suggested_councillors, :councillor_contribution_id, :integer
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveAuthorityIdFromSuggestedCouncillors < ActiveRecord::Migration
def change
remove_column :suggested_councillors, :authority_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveContributorIdFromSuggestedCouncillors < ActiveRecord::Migration
def change
remove_column :suggested_councillors, :contributor_id
end
end
Loading

0 comments on commit 2f9e80c

Please sign in to comment.