このコミットが含まれているのは:
@@ -0,0 +1,56 @@
|
||||
class UserSettingsController < ApplicationController
|
||||
def show
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
render json: current_setting.serializable_hash
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
raw_attributes = request.request_parameters
|
||||
field_errors = validate_raw_attributes(raw_attributes)
|
||||
return render_validation_error fields: field_errors if field_errors.present?
|
||||
|
||||
setting = current_setting
|
||||
setting.assign_attributes(raw_attributes.slice(*Setting::EDITABLE_ATTRIBUTES))
|
||||
|
||||
if setting.save
|
||||
render json: setting.serializable_hash, status: :ok
|
||||
else
|
||||
render_validation_error setting
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_setting
|
||||
current_user.setting || current_user.create_setting!(Setting.defaults)
|
||||
end
|
||||
|
||||
def validate_raw_attributes raw_attributes
|
||||
raw_attributes.each_with_object({ }) do |(key, value), errors|
|
||||
unless Setting::EDITABLE_ATTRIBUTES.include?(key)
|
||||
errors[key.to_sym] = ['不明な設定です.']
|
||||
next
|
||||
end
|
||||
|
||||
next if value_matches_type?(key, value)
|
||||
|
||||
errors[key.to_sym] = ['値の型が不正です.']
|
||||
end
|
||||
end
|
||||
|
||||
def value_matches_type? key, value
|
||||
case Setting::TYPE_BY_ATTRIBUTE.fetch(key)
|
||||
when :string
|
||||
value.is_a?(String)
|
||||
when :integer
|
||||
value.is_a?(Integer)
|
||||
when :boolean
|
||||
value == true || value == false
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,87 @@
|
||||
class Setting < ApplicationRecord
|
||||
THEMES = ['system', 'light', 'dark'].freeze
|
||||
DISPLAY_DENSITIES = ['comfortable', 'compact'].freeze
|
||||
FONT_SIZES = ['small', 'normal', 'large'].freeze
|
||||
POST_LIST_LIMITS = [20, 50, 100].freeze
|
||||
POST_LIST_ORDERS = [
|
||||
'title_asc',
|
||||
'title_desc',
|
||||
'url_asc',
|
||||
'url_desc',
|
||||
'original_created_at_asc',
|
||||
'original_created_at_desc',
|
||||
'created_at_asc',
|
||||
'created_at_desc',
|
||||
'updated_at_asc',
|
||||
'updated_at_desc',
|
||||
].freeze
|
||||
VIEWED_POST_DISPLAYS = ['show', 'dim', 'hide'].freeze
|
||||
AUTO_FETCH_MODES = ['auto', 'manual', 'off'].freeze
|
||||
WIKI_EDITOR_MODES = ['split', 'write', 'preview'].freeze
|
||||
|
||||
STRING_ATTRIBUTES = [
|
||||
'theme',
|
||||
'display_density',
|
||||
'font_size',
|
||||
'post_list_order',
|
||||
'viewed_post_display',
|
||||
'auto_fetch_title',
|
||||
'auto_fetch_thumbnail',
|
||||
'wiki_editor_mode',
|
||||
].freeze
|
||||
INTEGER_ATTRIBUTES = ['post_list_limit'].freeze
|
||||
BOOLEAN_ATTRIBUTES = ['tag_autocomplete_nico'].freeze
|
||||
EDITABLE_ATTRIBUTES =
|
||||
(STRING_ATTRIBUTES + INTEGER_ATTRIBUTES + BOOLEAN_ATTRIBUTES).freeze
|
||||
TYPE_BY_ATTRIBUTE = {
|
||||
'theme' => :string,
|
||||
'display_density' => :string,
|
||||
'font_size' => :string,
|
||||
'post_list_limit' => :integer,
|
||||
'post_list_order' => :string,
|
||||
'viewed_post_display' => :string,
|
||||
'tag_autocomplete_nico' => :boolean,
|
||||
'auto_fetch_title' => :string,
|
||||
'auto_fetch_thumbnail' => :string,
|
||||
'wiki_editor_mode' => :string,
|
||||
}.freeze
|
||||
|
||||
belongs_to :user
|
||||
|
||||
validates :user_id, presence: true
|
||||
validates :key, presence: true, length: { maximum: 255 }
|
||||
validates :value, presence: true
|
||||
validates :user_id, uniqueness: true
|
||||
|
||||
validates :theme, inclusion: { in: THEMES }
|
||||
validates :display_density, inclusion: { in: DISPLAY_DENSITIES }
|
||||
validates :font_size, inclusion: { in: FONT_SIZES }
|
||||
validates :post_list_limit, inclusion: { in: POST_LIST_LIMITS }
|
||||
validates :post_list_order, inclusion: { in: POST_LIST_ORDERS }
|
||||
validates :viewed_post_display, inclusion: { in: VIEWED_POST_DISPLAYS }
|
||||
validates :tag_autocomplete_nico, inclusion: { in: [true, false] }
|
||||
validates :auto_fetch_title, inclusion: { in: AUTO_FETCH_MODES }
|
||||
validates :auto_fetch_thumbnail, inclusion: { in: AUTO_FETCH_MODES }
|
||||
validates :wiki_editor_mode, inclusion: { in: WIKI_EDITOR_MODES }
|
||||
|
||||
def self.defaults
|
||||
{
|
||||
theme: 'system',
|
||||
display_density: 'comfortable',
|
||||
font_size: 'normal',
|
||||
post_list_limit: 50,
|
||||
post_list_order: 'created_at_desc',
|
||||
viewed_post_display: 'show',
|
||||
tag_autocomplete_nico: true,
|
||||
auto_fetch_title: 'manual',
|
||||
auto_fetch_thumbnail: 'manual',
|
||||
wiki_editor_mode: 'split',
|
||||
}
|
||||
end
|
||||
|
||||
def self.serializable_attributes
|
||||
EDITABLE_ATTRIBUTES.map(&:to_sym)
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
super({ only: self.class.serializable_attributes }.merge(options || { }))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class User < ApplicationRecord
|
||||
|
||||
has_many :created_posts,
|
||||
class_name: 'Post', foreign_key: :uploaded_user_id, dependent: :nullify
|
||||
has_many :settings
|
||||
has_one :setting, dependent: :destroy
|
||||
has_many :user_ips, dependent: :destroy
|
||||
has_many :ip_addresses, through: :user_ips
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
|
||||
@@ -89,6 +89,11 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
|
||||
scope 'users/settings', controller: :user_settings do
|
||||
get '', action: :show
|
||||
patch '', action: :update
|
||||
end
|
||||
|
||||
resources :deerjikists, only: [] do
|
||||
collection do
|
||||
scope ':platform/:code' do
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
class RebuildSettingsAsTypedUserSettings < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
remove_foreign_key :settings, :users if foreign_key_exists?(:settings, :users)
|
||||
remove_index :settings, :user_id if index_exists?(:settings, :user_id)
|
||||
|
||||
remove_column :settings, :key, :string if column_exists?(:settings, :key)
|
||||
remove_column :settings, :value, :json if column_exists?(:settings, :value)
|
||||
|
||||
change_column_null :settings, :user_id, false
|
||||
|
||||
add_column :settings, :theme, :string, null: false, default: 'system'
|
||||
add_column :settings,
|
||||
:display_density,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'comfortable'
|
||||
add_column :settings, :font_size, :string, null: false, default: 'normal'
|
||||
add_column :settings, :post_list_limit, :integer, null: false, default: 50
|
||||
add_column :settings,
|
||||
:post_list_order,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'created_at_desc'
|
||||
add_column :settings,
|
||||
:viewed_post_display,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'show'
|
||||
add_column :settings,
|
||||
:tag_autocomplete_nico,
|
||||
:boolean,
|
||||
null: false,
|
||||
default: true
|
||||
add_column :settings,
|
||||
:auto_fetch_title,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'manual'
|
||||
add_column :settings,
|
||||
:auto_fetch_thumbnail,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'manual'
|
||||
add_column :settings,
|
||||
:wiki_editor_mode,
|
||||
:string,
|
||||
null: false,
|
||||
default: 'split'
|
||||
|
||||
add_index :settings, :user_id, unique: true
|
||||
add_foreign_key :settings, :users
|
||||
end
|
||||
end
|
||||
生成ファイル
+12
-4
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_06_26_010000) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_07_04_000000) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -374,11 +374,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_26_010000) do
|
||||
|
||||
create_table "settings", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "user_id", null: false
|
||||
t.string "key", null: false
|
||||
t.json "value", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["user_id"], name: "index_settings_on_user_id"
|
||||
t.string "theme", default: "system", null: false
|
||||
t.string "display_density", default: "comfortable", null: false
|
||||
t.string "font_size", default: "normal", null: false
|
||||
t.integer "post_list_limit", default: 50, null: false
|
||||
t.string "post_list_order", default: "created_at_desc", null: false
|
||||
t.string "viewed_post_display", default: "show", null: false
|
||||
t.boolean "tag_autocomplete_nico", default: true, null: false
|
||||
t.string "auto_fetch_title", default: "manual", null: false
|
||||
t.string "auto_fetch_thumbnail", default: "manual", null: false
|
||||
t.string "wiki_editor_mode", default: "split", null: false
|
||||
t.index ["user_id"], name: "index_settings_on_user_id", unique: true
|
||||
end
|
||||
|
||||
create_table "tag_implications", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
|
||||
新しい課題から参照
ユーザをブロックする