mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Add default settings migration and concern
This commit is contained in:
parent
5115890d07
commit
c62002381b
|
@ -87,6 +87,6 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def set_time_zone(&block)
|
||||
Time.use_zone(current_user.time_zone, &block)
|
||||
Time.use_zone(current_user.settings[:time_zone], &block)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -115,7 +115,7 @@ module Users
|
|||
|
||||
def update_params
|
||||
params.require(:user).permit(
|
||||
:time_zone
|
||||
settings: :time_zone
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
22
app/models/concerns/settings_model.rb
Normal file
22
app/models/concerns/settings_model.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
module SettingsModel
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
@@default_settings = HashWithIndifferentAccess.new
|
||||
|
||||
included do
|
||||
serialize :settings, JsonbHashSerializer
|
||||
after_initialize :init_default_settings, if: :new_record?
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def default_settings(dfs)
|
||||
@@default_settings.merge!(dfs)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def init_default_settings
|
||||
self.settings = @@default_settings
|
||||
end
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
class User < ApplicationRecord
|
||||
include SearchableModel
|
||||
include SettingsModel
|
||||
|
||||
acts_as_token_authenticatable
|
||||
devise :invitable, :confirmable, :database_authenticatable, :registerable,
|
||||
|
@ -33,9 +34,19 @@ class User < ApplicationRecord
|
|||
validates_attachment :avatar,
|
||||
:content_type => { :content_type => ["image/jpeg", "image/png"] },
|
||||
size: { less_than: Constants::AVATAR_MAX_SIZE_MB.megabytes }
|
||||
validates :time_zone, presence: true
|
||||
validate :time_zone_check
|
||||
|
||||
default_settings(
|
||||
time_zone: 'UTC',
|
||||
notifications: {
|
||||
assignments: true,
|
||||
assignments_email: false,
|
||||
recent: true,
|
||||
recent_email: false,
|
||||
system_message_email: false
|
||||
}
|
||||
)
|
||||
|
||||
# Relations
|
||||
has_many :user_teams, inverse_of: :user
|
||||
has_many :teams, through: :user_teams
|
||||
|
@ -384,7 +395,7 @@ class User < ApplicationRecord
|
|||
end
|
||||
|
||||
def time_zone_check
|
||||
if time_zone.nil? or ActiveSupport::TimeZone.new(time_zone).nil?
|
||||
if settings[:time_zone].nil? or ActiveSupport::TimeZone.new(settings[:time_zone]).nil?
|
||||
errors.add(:time_zone)
|
||||
end
|
||||
end
|
||||
|
|
11
app/serializers/jsonb_hash_serializer.rb
Normal file
11
app/serializers/jsonb_hash_serializer.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class JsonbHashSerializer
|
||||
def self.dump(hash)
|
||||
hash.to_json
|
||||
end
|
||||
|
||||
def self.load(hash)
|
||||
hash ||= {}
|
||||
hash = JSON.parse(hash) if hash.instance_of? String
|
||||
hash.with_indifferent_access
|
||||
end
|
||||
end
|
|
@ -12,7 +12,7 @@
|
|||
<%= form_for(@user,
|
||||
url: update_preferences_path(format: :json),
|
||||
remote: true,
|
||||
html: { method: :put, 'data-for' => 'time_zone' }) do |f| %>
|
||||
html: { method: :put, 'data-for' => 'settings[time_zone]' }) do |f| %>
|
||||
<div data-part="view">
|
||||
<div class="form-group">
|
||||
<%= f.label t("users.settings.account.preferences.edit.time_zone_label") %>
|
||||
|
@ -21,9 +21,9 @@
|
|||
disabled="disabled"
|
||||
autocomplete="off"
|
||||
type="text"
|
||||
value="<%= @user.time_zone %>"
|
||||
name="fake_user[time_zone]"
|
||||
id="fake_user_time_zone">
|
||||
value="<%= @user.settings[:time_zone] %>"
|
||||
name="fake_user[settings][time_zone]"
|
||||
id="fake_user_settings_time_zone">
|
||||
<span class="input-group-btn">
|
||||
<a href="#" class="btn btn-default" data-action="edit"><%=t "general.edit" %></a>
|
||||
</span>
|
||||
|
|
53
db/migrate/20170809131000_refactor_user_settings.rb
Normal file
53
db/migrate/20170809131000_refactor_user_settings.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
class RefactorUserSettings < ActiveRecord::Migration[5.1]
|
||||
def up
|
||||
add_column :users, :settings, :jsonb, default: {}, null: false
|
||||
|
||||
User.find_each do |user|
|
||||
settings = {
|
||||
time_zone: user['time_zone'],
|
||||
notifications: {
|
||||
assignments: user['assignments_notification'],
|
||||
assignments_email: user['assignments_notification_email'],
|
||||
recent: user['recent_notification'],
|
||||
recent_email: user['recent_notification_email'],
|
||||
system_message_email: user['system_message_notification_email']
|
||||
}
|
||||
}
|
||||
user.update(settings: settings)
|
||||
end
|
||||
|
||||
remove_column :users, :time_zone, :string
|
||||
remove_column :users, :assignments_notification, :boolean
|
||||
remove_column :users, :assignments_notification_email, :boolean
|
||||
remove_column :users, :recent_notification, :boolean
|
||||
remove_column :users, :recent_notification_email, :boolean
|
||||
remove_column :users, :system_message_notification_email, :boolean
|
||||
end
|
||||
|
||||
def down
|
||||
add_column :users, :time_zone, :string, default: false
|
||||
add_column :users, :assignments_notification, :boolean, default: false
|
||||
add_column :users, :assignments_notification_email, :boolean, default: false
|
||||
add_column :users, :recent_notification, :boolean, default: false
|
||||
add_column :users, :recent_notification_email, :boolean, default: false
|
||||
add_column :users,
|
||||
:system_message_notification_email, :boolean, default: false
|
||||
|
||||
User.find_each do |user|
|
||||
user.time_zone = user.settings[:time_zone]
|
||||
user.assignments_notification =
|
||||
user.settings[:notifications][:assignments]
|
||||
user.assignments_notification_email =
|
||||
user.settings[:notifications][:assignments_email]
|
||||
user.recent_notification =
|
||||
user.settings[:notifications][:recent]
|
||||
user.recent_notification_email =
|
||||
user.settings[:notifications][:recent_email]
|
||||
user.system_message_notification_email =
|
||||
user.settings[:notifications][:system_message_email]
|
||||
user.save
|
||||
end
|
||||
|
||||
remove_column :users, :settings, :jsonb
|
||||
end
|
||||
end
|
BIN
scinote-web-v1.0.0.tgz
Normal file
BIN
scinote-web-v1.0.0.tgz
Normal file
Binary file not shown.
|
@ -28,7 +28,6 @@ describe User, type: :model do
|
|||
it { should have_db_column :confirmed_at }
|
||||
it { should have_db_column :confirmation_sent_at }
|
||||
it { should have_db_column :unconfirmed_email }
|
||||
it { should have_db_column :time_zone }
|
||||
it { should have_db_column :invitation_token }
|
||||
it { should have_db_column :invitation_created_at }
|
||||
it { should have_db_column :invitation_sent_at }
|
||||
|
@ -38,12 +37,8 @@ describe User, type: :model do
|
|||
it { should have_db_column :invited_by_type }
|
||||
it { should have_db_column :invitations_count }
|
||||
it { should have_db_column :tutorial_status }
|
||||
it { should have_db_column :assignments_notification }
|
||||
it { should have_db_column :recent_notification }
|
||||
it { should have_db_column :assignments_notification_email }
|
||||
it { should have_db_column :recent_notification_email }
|
||||
it { should have_db_column :settings }
|
||||
it { should have_db_column :current_team_id }
|
||||
it { should have_db_column :system_message_notification_email }
|
||||
it { should have_db_column :authentication_token }
|
||||
end
|
||||
|
||||
|
@ -115,7 +110,7 @@ describe User, type: :model do
|
|||
it { should validate_presence_of :full_name }
|
||||
it { should validate_presence_of :initials }
|
||||
it { should validate_presence_of :email }
|
||||
it { should validate_presence_of :time_zone }
|
||||
it { should validate_presence_of :settings }
|
||||
|
||||
it do
|
||||
should validate_length_of(:full_name).is_at_most(
|
||||
|
|
Loading…
Reference in a new issue