configurable field to unsubscribe from emails + link to do it yourself

This commit is contained in:
Devin Howard 2016-11-25 13:22:23 -05:00
parent 79634f8faf
commit 4629ebf8f9
7 changed files with 66 additions and 20 deletions

View file

@ -3095,3 +3095,7 @@ script.data-gratipay-username {
display: inline;
float: left;
}
.inline {
display: inline-block;
}

View file

@ -54,6 +54,28 @@ class NotificationsController < ApplicationController
end
end
def unsubscribe
# TODO will a logged out user be unsubscribed after logging in?
# need to use devise stored_url or whatever
if current_user.nil?
return redirect_to sign_in_path, notice: 'Continue to unsubscribe from emails by logging in.'
end
if current_user.emails_allowed == false
return redirect_to edit_user_path(current_user), notice: 'You were already unsubscribed from emails.'
end
current_user.emails_allowed = false
success = current_user.save
if success
redirect_to edit_user_path(current_user), notice: 'You will no longer receive emails from Metamaps.'
else
flash[:alert] = 'Sorry, something went wrong. You have not been unsubscribed from emails.'
redirect_to edit_user_path(current_user)
end
end
private
def set_receipts

View file

@ -13,13 +13,12 @@ class UsersController < ApplicationController
# GET /users/:id/edit
def edit
@user = current_user
respond_with(@user)
@user = User.find(current_user.id)
end
# PUT /users/:id
def update
@user = current_user
@user = User.find(current_user.id)
if user_params[:password] == '' && user_params[:password_confirmation] == ''
# not trying to change the password
@ -96,6 +95,6 @@ class UsersController < ApplicationController
private
def user_params
params.require(:user).permit(:name, :email, :image, :password, :password_confirmation)
params.require(:user).permit(:name, :email, :image, :password, :password_confirmation, :emails_allowed)
end
end

View file

@ -33,23 +33,35 @@
<div class="nameEdit"><%= @user.name %></div>
</div>
<div class="changeName">
<%= form.label :name, "Name:", :class => "firstFieldText" %>
<%= form.text_field :name %>
<%= form.label :name, "Name:", class: 'firstFieldText' %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :email, "Email:", class: 'firstFieldText' %>
<%= form.email_field :email %>
</div>
<div>
<%= form.label :emails_allowed, class: 'firstFieldText' do %>
<%= form.check_box :emails_allowed, class: 'inline' %>
Send Metamaps notifications to my email.
<% end %>
</div>
<div><%= form.label :email, "Email:", :class => "firstFieldText" %>
<%= form.email_field :email %></div>
<div class="changePass" onclick="Metamaps.Account.showPass()">Change Password</div>
<div class="toHide">
<div>
<%= form.label :current_password, "Current Password:", :class => "firstFieldText" %>
<%= password_field_tag :current_password, params[:current_password] %>
<div>
<%= form.label :current_password, "Current Password:", :class => "firstFieldText" %>
<%= password_field_tag :current_password, params[:current_password] %>
</div>
<div>
<%= form.label :password, "New Password:", :class => "firstFieldText" %>
<%= form.password_field :password, :autocomplete => :off%>
</div>
<div>
<%= form.label :password_confirmation, "Confirm New Password:", :class => "firstFieldText" %>
<%= form.password_field :password_confirmation, :autocomplete => :off%>
</div>
<div class="noChangePass" onclick="Metamaps.Account.hidePass()">Oops, don't change password</div>
</div>
<div><%= form.label :password, "New Password:", :class => "firstFieldText" %>
<%= form.password_field :password, :autocomplete => :off%></div>
<div><%= form.label :password_confirmation, "Confirm New Password:", :class => "firstFieldText" %>
<%= form.password_field :password_confirmation, :autocomplete => :off%></div>
<div class="noChangePass" onclick="Metamaps.Account.hidePass()">Oops, don't change password</div>
</div>
<div id="accountPageLoading"></div>
<%= form.submit "Update", class: "update", onclick: "Metamaps.Account.showLoading()" %>
<div class="clearfloat"></div>

View file

@ -50,6 +50,9 @@ Metamaps::Application.routes.draw do
resources :messages, only: [:show, :create, :update, :destroy]
resources :notifications, only: [:index, :show] do
collection do
get :unsubscribe
end
member do
put :mark_read
put :mark_unread

View file

@ -0,0 +1,5 @@
class AddEmailsAllowedToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :emails_allowed, :boolean, default: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161105160340) do
ActiveRecord::Schema.define(version: 20161125175229) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -296,8 +296,8 @@ ActiveRecord::Schema.define(version: 20161105160340) do
t.string "password_salt", limit: 255
t.string "persistence_token", limit: 255
t.string "perishable_token", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code", limit: 8
t.string "joinedwithcode", limit: 8
t.text "settings"
@ -317,6 +317,7 @@ ActiveRecord::Schema.define(version: 20161105160340) do
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "generation"
t.boolean "emails_allowed", default: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end