diff --git a/app/models/user.rb b/app/models/user.rb index 8dbcc6f74..6bdbd44aa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -108,6 +108,8 @@ class User < ApplicationRecord acts_as_follower has_one :profile, dependent: :destroy + has_one :notification_setting, class_name: "Users::NotificationSetting", dependent: :destroy + has_one :setting, class_name: "Users::Setting", dependent: :destroy has_many :access_grants, class_name: "Doorkeeper::AccessGrant", foreign_key: :resource_owner_id, inverse_of: :resource_owner, dependent: :delete_all diff --git a/app/models/users/notification_setting.rb b/app/models/users/notification_setting.rb new file mode 100644 index 000000000..b2bbac177 --- /dev/null +++ b/app/models/users/notification_setting.rb @@ -0,0 +1,14 @@ +module Users + class NotificationSetting < ApplicationRecord + self.table_name_prefix = "users_" + validates :email_digest_periodic, inclusion: { in: [true, false] } + + alias_attribute :subscribed_to_welcome_notifications?, :welcome_notifications + alias_attribute :subscribed_to_mod_roundrobin_notifications?, :mod_roundrobin_notifications + alias_attribute :subscribed_to_email_follower_notifications?, :email_follower_notifications + + # NOTE: @ridhwana Need to account for + # subscribe_to_mailchimp_newsletter in app/models/user.rb + # code: return unless saved_changes.key?(:email) || saved_changes.key?(:email_newsletter) + end +end diff --git a/app/models/users/setting.rb b/app/models/users/setting.rb new file mode 100644 index 000000000..91fbe3836 --- /dev/null +++ b/app/models/users/setting.rb @@ -0,0 +1,33 @@ +module Users + class Setting < ApplicationRecord + self.table_name_prefix = "users_" + + belongs_to :user + + enum editor_version: { v2: 0, v1: 1 }, _suffix: :editor + enum config_font: { default: 0, comic_sans: 1, monospace: 2, open_dyslexic: 3, sans_serif: 4, serif: 5 } + enum inbox_type: { private: 0, open: 1 }, _suffix: :inbox + enum config_navbar: { default_navbar: 0, static_navbar: 1 } + enum config_theme: { default_theme: 0, minimal_light_theme: 1, night_theme: 2, pink_theme: 3, + ten_x_hacker_theme: 4 } + + validates :user_id, presence: true + validates :experience_level, numericality: { less_than_or_equal_to: 10 }, allow_blank: true + validates :feed_referential_link, inclusion: { in: [true, false] } + validates :feed_url, length: { maximum: 500 }, allow_nil: true + validates :inbox_guidelines, length: { maximum: 250 }, allow_nil: true + validate :validate_feed_url, if: :feed_url_changed? + + private + + def validate_feed_url + return if feed_url.blank? + + valid = Feeds::ValidateUrl.call(feed_url) + + errors.add(:feed_url, "is not a valid RSS/Atom feed") unless valid + rescue StandardError => e + errors.add(:feed_url, e.message) + end + end +end diff --git a/app/views/users/_customization.html.erb b/app/views/users/_customization.html.erb index 92dfd77fb..12cb7c59d 100644 --- a/app/views/users/_customization.html.erb +++ b/app/views/users/_customization.html.erb @@ -11,7 +11,7 @@