diff --git a/app/assets/stylesheets/application.scss.erb b/app/assets/stylesheets/application.scss.erb index 82f318fe..e68390ef 100644 --- a/app/assets/stylesheets/application.scss.erb +++ b/app/assets/stylesheets/application.scss.erb @@ -3095,3 +3095,7 @@ script.data-gratipay-username { display: inline; float: left; } + +.inline { + display: inline-block; +} diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 9d8c8eb2..56a518c1 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a9fff9de..6b771d6f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 92890a92..8427582a 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -33,23 +33,35 @@
<%= @user.name %>
- <%= form.label :name, "Name:", :class => "firstFieldText" %> - <%= form.text_field :name %> + <%= form.label :name, "Name:", class: 'firstFieldText' %> + <%= form.text_field :name %> +
+
+ <%= form.label :email, "Email:", class: 'firstFieldText' %> + <%= form.email_field :email %> +
+
+ <%= form.label :emails_allowed, class: 'firstFieldText' do %> + <%= form.check_box :emails_allowed, class: 'inline' %> + Send Metamaps notifications to my email. + <% end %>
-
<%= form.label :email, "Email:", :class => "firstFieldText" %> - <%= form.email_field :email %>
Change Password
-
- <%= form.label :current_password, "Current Password:", :class => "firstFieldText" %> - <%= password_field_tag :current_password, params[:current_password] %> +
+ <%= form.label :current_password, "Current Password:", :class => "firstFieldText" %> + <%= password_field_tag :current_password, params[:current_password] %> +
+
+ <%= form.label :password, "New Password:", :class => "firstFieldText" %> + <%= form.password_field :password, :autocomplete => :off%> +
+
+ <%= form.label :password_confirmation, "Confirm New Password:", :class => "firstFieldText" %> + <%= form.password_field :password_confirmation, :autocomplete => :off%> +
+
Oops, don't change password
-
<%= form.label :password, "New Password:", :class => "firstFieldText" %> - <%= form.password_field :password, :autocomplete => :off%>
-
<%= form.label :password_confirmation, "Confirm New Password:", :class => "firstFieldText" %> - <%= form.password_field :password_confirmation, :autocomplete => :off%>
-
Oops, don't change password
-
<%= form.submit "Update", class: "update", onclick: "Metamaps.Account.showLoading()" %>
diff --git a/config/routes.rb b/config/routes.rb index dc7e3e8a..000784f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20161125175229_add_emails_allowed_to_users.rb b/db/migrate/20161125175229_add_emails_allowed_to_users.rb new file mode 100644 index 00000000..609e4309 --- /dev/null +++ b/db/migrate/20161125175229_add_emails_allowed_to_users.rb @@ -0,0 +1,5 @@ +class AddEmailsAllowedToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :emails_allowed, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index bc7a08ea..5839929c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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