From 19e8b861a2c97240e0ca9c47d13bb3d7c5cb7520 Mon Sep 17 00:00:00 2001 From: ysksn Date: Thu, 16 Nov 2017 00:05:20 +0900 Subject: [PATCH] Delegate some methods of User to @settings (#5706) * Move some tests of User into Settings::ScopedSettings * Add a test for User@settings --- app/models/user.rb | 40 +++-------------------- spec/lib/settings/scoped_settings_spec.rb | 35 ++++++++++++++++++++ spec/models/user_spec.rb | 39 ++-------------------- 3 files changed, 42 insertions(+), 72 deletions(-) create mode 100644 spec/lib/settings/scoped_settings_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index ebe768c523..326b871a1b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -73,6 +73,10 @@ class User < ApplicationRecord has_many :session_activations, dependent: :destroy + delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal, + :reduce_motion, :system_font_ui, :noindex, :theme, + to: :settings, prefix: :setting, allow_nil: false + def confirmed? confirmed_at.present? end @@ -136,42 +140,6 @@ class User < ApplicationRecord settings.default_privacy || (account.locked? ? 'private' : 'public') end - def setting_default_sensitive - settings.default_sensitive - end - - def setting_unfollow_modal - settings.unfollow_modal - end - - def setting_boost_modal - settings.boost_modal - end - - def setting_delete_modal - settings.delete_modal - end - - def setting_auto_play_gif - settings.auto_play_gif - end - - def setting_reduce_motion - settings.reduce_motion - end - - def setting_system_font_ui - settings.system_font_ui - end - - def setting_noindex - settings.noindex - end - - def setting_theme - settings.theme - end - def token_for_app(a) return nil if a.nil? || a.owner != self Doorkeeper::AccessToken diff --git a/spec/lib/settings/scoped_settings_spec.rb b/spec/lib/settings/scoped_settings_spec.rb new file mode 100644 index 0000000000..7566685b4a --- /dev/null +++ b/spec/lib/settings/scoped_settings_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Settings::ScopedSettings do + let(:object) { Fabricate(:user) } + let(:scoped_setting) { described_class.new(object) } + let(:val) { 'whatever' } + let(:methods) { %i(auto_play_gif default_sensitive unfollow_modal boost_modal delete_modal reduce_motion system_font_ui noindex theme) } + + describe '.initialize' do + it 'sets @object' do + scoped_setting = described_class.new(object) + expect(scoped_setting.instance_variable_get(:@object)).to be object + end + end + + describe '#method_missing' do + it 'sets scoped_setting.method_name = val' do + methods.each do |key| + scoped_setting.send("#{key}=", val) + expect(scoped_setting.send(key)).to eq val + end + end + end + + describe '#[]= and #[]' do + it 'sets [key] = val' do + methods.each do |key| + scoped_setting[key] = val + expect(scoped_setting[key]).to eq val + end + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 99aeca01b2..77a12c26d0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -177,27 +177,10 @@ RSpec.describe User, type: :model do end end - describe '#setting_auto_play_gif' do - it 'returns auto-play gif setting' do + describe 'settings' do + it 'is instance of Settings::ScopedSettings' do user = Fabricate(:user) - user.settings[:auto_play_gif] = false - expect(user.setting_auto_play_gif).to eq false - end - end - - describe '#setting_system_font_ui' do - it 'returns system font ui setting' do - user = Fabricate(:user) - user.settings[:system_font_ui] = false - expect(user.setting_system_font_ui).to eq false - end - end - - describe '#setting_boost_modal' do - it 'returns boost modal setting' do - user = Fabricate(:user) - user.settings[:boost_modal] = false - expect(user.setting_boost_modal).to eq false + expect(user.settings).to be_kind_of Settings::ScopedSettings end end @@ -219,22 +202,6 @@ RSpec.describe User, type: :model do end end - describe '#setting_unfollow_modal' do - it 'returns unfollow modal setting' do - user = Fabricate(:user) - user.settings[:unfollow_modal] = true - expect(user.setting_unfollow_modal).to eq true - end - end - - describe '#setting_delete_modal' do - it 'returns delete modal setting' do - user = Fabricate(:user) - user.settings[:delete_modal] = false - expect(user.setting_delete_modal).to eq false - end - end - describe 'whitelist' do around(:each) do |example| old_whitelist = Rails.configuration.x.email_whitelist