aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrhenium <re4k@re4k.info>2013-05-28 16:06:55 +0900
committerrhenium <re4k@re4k.info>2013-05-28 16:06:55 +0900
commit55f7cae17ec78afadb27fd31c62a55c3bc6b47cf (patch)
tree6d02d963e14861fe3d71c18ace0a8c4587b2f344
parent76f2450412b974ede2a904504cf5bffd28a01776 (diff)
downloadaclog-55f7cae17ec78afadb27fd31c62a55c3bc6b47cf.tar.gz
add account settings
-rw-r--r--app/controllers/settings_controller.rb27
-rw-r--r--app/models/account.rb22
-rw-r--r--app/models/notification.rb4
-rw-r--r--app/views/layouts/_base.html.haml1
-rw-r--r--app/views/settings/confirm_deactivation.html.haml5
-rw-r--r--app/views/settings/deactivate.html.haml3
-rw-r--r--app/views/settings/index.html.haml10
-rw-r--r--config/routes.rb7
-rw-r--r--db/migrate/20130525142430_add_account_settings_to_account.rb8
-rw-r--r--db/schema.rb13
-rw-r--r--spec/controllers/settings_controller_spec.rb5
11 files changed, 99 insertions, 6 deletions
diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb
new file mode 100644
index 0000000..0b86888
--- /dev/null
+++ b/app/controllers/settings_controller.rb
@@ -0,0 +1,27 @@
+class SettingsController < ApplicationController
+ before_filter :authenticate!
+ layout "index"
+
+ def index
+ end
+
+ def update
+ @account.update_settings!(notification: params[:notification] == "true",
+ private: params[:private] == "true")
+ redirect_to action: "index"
+ end
+
+ def confirm_deactivation
+ end
+
+ def deactivate
+ @account.deactivate!
+ reset_session
+ end
+
+ private
+ def authenticate!
+ raise Aclog::Exceptions::LoginRequired unless session[:user_id]
+ @account = session[:account]
+ end
+end
diff --git a/app/models/account.rb b/app/models/account.rb
index de4be88..80c2f80 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -1,17 +1,39 @@
require "msgpack/rpc/transport/unix"
class Account < ActiveRecord::Base
+ ACTIVE = 0; DEACTIVATED = 1
+
belongs_to :user
+ scope :active, -> { where(status: Account::ACTIVE) }
+
def self.create_or_update(hash)
account = where(user_id: hash[:user_id]).first_or_initialize
account.oauth_token = hash[:oauth_token]
account.oauth_token_secret = hash[:oauth_token_secret]
account.consumer_version = hash[:consumer_version]
+ account.status = Account::ACTIVE
account.save if account.changed?
account
end
+ def notification?; self.notification end
+ def private?; self.private end
+ def active?; self.status == Account::ACTIVE end
+
+ def update_settings!(params)
+ self.notification = params[:notification]
+ self.private = params[:private]
+ self.save! if self.changed?
+ end
+
+ def deactivate!
+ self.status = Account::DEACTIVATED
+ self.save!
+
+ update_connection
+ end
+
def update_connection
transport = MessagePack::RPC::UNIXTransport.new
client = MessagePack::RPC::Client.new(transport, File.join(Rails.root, "tmp", "sockets", "receiver.sock"))
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 4f5a68e..f01393f 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -1,7 +1,9 @@
class Notification
def self.notify_favorite(tweet)
if Settings.notification.favorites.include?(tweet.favorites.count)
- reply_favs(tweet, tweet.favorites.count) if tweet.user.registered?
+ if tweet.user.registered? && tweet.user.account.notification?
+ reply_favs(tweet, tweet.favorites.count)
+ end
end
end
diff --git a/app/views/layouts/_base.html.haml b/app/views/layouts/_base.html.haml
index 868c269..907bd45 100644
--- a/app/views/layouts/_base.html.haml
+++ b/app/views/layouts/_base.html.haml
@@ -16,6 +16,7 @@
= link_to "about", about_path
- if logged_in?
%li= link_to "logout", logout_path
+ %li= link_to "settings", settings_path
%li= link_to session[:account].user.screen_name, user_best_path(session[:account].user.screen_name)
- else
%li= link_to "login", "/i/login"
diff --git a/app/views/settings/confirm_deactivation.html.haml b/app/views/settings/confirm_deactivation.html.haml
new file mode 100644
index 0000000..bc58b74
--- /dev/null
+++ b/app/views/settings/confirm_deactivation.html.haml
@@ -0,0 +1,5 @@
+%h1 deactivate
+= form_tag "/i/settings/deactivate", method: :post do
+ = link_to "Cancel", action: "index"
+ = submit_tag
+
diff --git a/app/views/settings/deactivate.html.haml b/app/views/settings/deactivate.html.haml
new file mode 100644
index 0000000..94d3203
--- /dev/null
+++ b/app/views/settings/deactivate.html.haml
@@ -0,0 +1,3 @@
+%h1 deactivated
+ご利用ありがとうございました。記録を停止されますが、データは削除されません。もう一度ログインをすると記録が再開されます。
+
diff --git a/app/views/settings/index.html.haml b/app/views/settings/index.html.haml
new file mode 100644
index 0000000..375bba9
--- /dev/null
+++ b/app/views/settings/index.html.haml
@@ -0,0 +1,10 @@
+%h1 settings
+= form_tag "/i/settings/update", method: :post do
+ = check_box_tag :notification, true, @account.notification
+ = label_tag :notification, "通知を有効にする"
+ = check_box_tag :private, true, @account.private
+ = label_tag :private, "best を非公開にする"
+ = submit_tag
+
+= link_to "Deactivate my account", controller: "settings", action: "confirm_deactivation"
+
diff --git a/config/routes.rb b/config/routes.rb
index eb07dd4..2419eb0 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -21,6 +21,13 @@ Aclog::Application.routes.draw do
get "/i/:id" => "tweets#show", as: "tweet", constraints: {id: /\d+/}
+ scope "/i/settings", controller: "settings" do
+ get "/", action: "index", as: "settings"
+ post "/update", action: "update"
+ get "/confirm_deactivation", action: "confirm_deactivation"
+ post "/deactivate", action: "deactivate"
+ end
+
scope "about", controller: "about" do
get "/", action: "about", as: "about"
get "/api", action: "api", as: "about_api"
diff --git a/db/migrate/20130525142430_add_account_settings_to_account.rb b/db/migrate/20130525142430_add_account_settings_to_account.rb
new file mode 100644
index 0000000..2b11479
--- /dev/null
+++ b/db/migrate/20130525142430_add_account_settings_to_account.rb
@@ -0,0 +1,8 @@
+class AddAccountSettingsToAccount < ActiveRecord::Migration
+ def change
+ add_column :accounts, :notification, :boolean, null: false, default: false
+ add_column :accounts, :private, :boolean, null: false, default: false
+ add_column :accounts, :status, :integer, limit: 2, null: false, default: 0
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 6dee41a..e62d456 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,15 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20130511060935) do
+ActiveRecord::Schema.define(version: 20130525142430) do
create_table "accounts", force: true do |t|
- t.integer "user_id", limit: 8, null: false
- t.string "oauth_token", null: false
- t.string "oauth_token_secret", null: false
+ t.integer "user_id", limit: 8, null: false
+ t.string "oauth_token", null: false
+ t.string "oauth_token_secret", null: false
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "consumer_version", null: false
+ t.integer "consumer_version", null: false
+ t.boolean "notification", default: false, null: false
+ t.boolean "private", default: false, null: false
+ t.integer "status", limit: 2, default: 0, null: false
end
add_index "accounts", ["user_id"], name: "index_accounts_on_user_id", unique: true, using: :btree
diff --git a/spec/controllers/settings_controller_spec.rb b/spec/controllers/settings_controller_spec.rb
new file mode 100644
index 0000000..be88ada
--- /dev/null
+++ b/spec/controllers/settings_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe SettingsController do
+
+end