Add preferences to user model

This commit is contained in:
Râu Cao 2023-04-04 12:27:49 +02:00
parent a33410eeb4
commit 23821f9e65
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
4 changed files with 61 additions and 1 deletions

View File

@ -1,6 +1,8 @@
class User < ApplicationRecord class User < ApplicationRecord
include EmailValidatable include EmailValidatable
serialize :preferences, Hash, default: {}
# Relations # Relations
has_many :invitations, dependent: :destroy has_many :invitations, dependent: :destroy
has_one :invitation, inverse_of: :invitee, foreign_key: 'invited_user_id' has_one :invitation, inverse_of: :invitee, foreign_key: 'invited_user_id'
@ -133,6 +135,11 @@ class User < ApplicationRecord
ldap.delete_attribute(dn,:service) ldap.delete_attribute(dn,:service)
end end
def pref_enabled?(key)
value = preferences.dig(*key.split(":"))
[true, "true", 1, "enabled"].include?(value)
end
def exchange_xmpp_contact_with_inviter def exchange_xmpp_contact_with_inviter
return unless inviter.services_enabled.include?("ejabberd") && return unless inviter.services_enabled.include?("ejabberd") &&
services_enabled.include?("ejabberd") services_enabled.include?("ejabberd")

View File

@ -0,0 +1,5 @@
class AddPreferencesToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :preferences, :text
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_03_19_101128) do ActiveRecord::Schema[7.0].define(version: 2023_04_03_135149) do
create_table "donations", force: :cascade do |t| create_table "donations", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "amount_sats" t.integer "amount_sats"
@ -57,8 +57,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_19_101128) do
t.text "ln_login_ciphertext" t.text "ln_login_ciphertext"
t.text "ln_password_ciphertext" t.text "ln_password_ciphertext"
t.string "ln_account" t.string "ln_account"
t.string "nostr_pubkey"
t.datetime "remember_created_at" t.datetime "remember_created_at"
t.string "remember_token" t.string "remember_token"
t.text "preferences"
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end

View File

@ -149,4 +149,50 @@ RSpec.describe User, type: :model do
end end
end end
end end
describe "#pref_enabled?" do
describe "preference not set" do
# TODO return default value
it "returns false" do
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
end
describe "preference is set" do
it "returns true for boolean true" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => true}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for string 'true'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "true"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for string 'enabled'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "enabled"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for integer 1" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => 1}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns false for boolean false" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => false}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
it "returns false for string 'false'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "false"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
it "returns false for integer 0" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => 0}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
end
end
end end