2020-01-22 11:53:40 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Setting is a configuration element of the platform. Only administrators are allowed to modify Settings
|
2022-09-27 11:14:27 +02:00
|
|
|
# For some settings, changing them will involve some callback actions (like rebuilding the stylesheets
|
|
|
|
# if the theme color Setting has changed).
|
2020-01-22 11:53:40 +01:00
|
|
|
# A full history of the previous values is kept in database with the date and the author of the change
|
2020-01-27 17:10:29 +01:00
|
|
|
# after_update callback is handled by SettingService
|
2020-03-25 10:16:47 +01:00
|
|
|
class Setting < ApplicationRecord
|
2023-01-26 09:48:37 +01:00
|
|
|
include SettingsHelper
|
|
|
|
|
2022-09-27 11:14:27 +02:00
|
|
|
has_many :history_values, dependent: :destroy
|
2023-01-26 09:48:37 +01:00
|
|
|
|
|
|
|
# The full list of settings is declared in SettingsHelper
|
|
|
|
validates :name, inclusion: { in: SETTINGS }
|
2020-06-09 13:09:31 +02:00
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def value
|
2020-12-22 14:43:08 +01:00
|
|
|
last_value = history_values.order(HistoryValue.arel_table['created_at'].desc).limit(1).first
|
2018-12-17 16:02:02 +01:00
|
|
|
last_value&.value
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
def value_at(date)
|
|
|
|
val = history_values.order(HistoryValue.arel_table['created_at'].desc).where('created_at <= ?', date).limit(1).first
|
|
|
|
val&.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_update
|
|
|
|
first_value = history_values.order(HistoryValue.arel_table['created_at'].asc).limit(1).first
|
|
|
|
first_value&.created_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_value
|
|
|
|
first_value = history_values.order(HistoryValue.arel_table['created_at'].asc).limit(1).first
|
|
|
|
first_value&.value
|
|
|
|
end
|
|
|
|
|
2019-04-23 12:58:01 +02:00
|
|
|
def last_update
|
2020-12-22 14:43:08 +01:00
|
|
|
last_value = history_values.order(HistoryValue.arel_table['created_at'].desc).limit(1).first
|
2019-04-23 12:58:01 +02:00
|
|
|
last_value&.created_at
|
|
|
|
end
|
|
|
|
|
2023-01-24 16:48:05 +01:00
|
|
|
def previous_value
|
2023-01-27 17:31:16 +01:00
|
|
|
last_two = history_values.order(HistoryValue.arel_table['created_at'].desc).limit(2)
|
|
|
|
return nil if last_two.count < 2
|
|
|
|
|
|
|
|
last_two.last&.value
|
2023-01-24 16:48:05 +01:00
|
|
|
end
|
|
|
|
|
2020-06-17 12:20:51 +02:00
|
|
|
def previous_update
|
2023-01-27 17:31:16 +01:00
|
|
|
last_two = history_values.order(HistoryValue.arel_table['created_at'].desc).limit(2)
|
|
|
|
return nil if last_two.count < 2
|
|
|
|
|
|
|
|
last_two.last&.created_at
|
2020-06-17 12:20:51 +02:00
|
|
|
end
|
|
|
|
|
2022-09-27 11:14:27 +02:00
|
|
|
# @deprecated, prefer Setting.set() instead
|
2018-12-17 16:02:02 +01:00
|
|
|
def value=(val)
|
|
|
|
admin = User.admins.first
|
2019-06-03 16:51:43 +02:00
|
|
|
save && history_values.create(invoicing_profile: admin.invoicing_profile, value: val)
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2020-05-13 15:02:03 +02:00
|
|
|
|
2020-05-13 15:52:01 +02:00
|
|
|
# Return the value of the requested setting, if any.
|
2023-01-25 16:40:55 +01:00
|
|
|
# @example Setting.get('my_setting') #=> "foo"
|
|
|
|
# @param name [String]
|
|
|
|
# @return [String,Boolean]
|
2020-05-13 15:02:03 +02:00
|
|
|
def self.get(name)
|
2022-10-26 14:32:19 +02:00
|
|
|
res = find_by('LOWER(name) = ? ', name.downcase)&.value
|
2020-05-13 15:52:01 +02:00
|
|
|
|
|
|
|
# handle boolean values
|
|
|
|
return true if res == 'true'
|
|
|
|
return false if res == 'false'
|
|
|
|
|
|
|
|
res
|
2020-05-13 15:02:03 +02:00
|
|
|
end
|
2020-05-26 13:59:40 +02:00
|
|
|
|
|
|
|
# Create or update the provided setting with the given value
|
2023-01-25 16:40:55 +01:00
|
|
|
# @example Setting.set('my_setting', true)
|
2020-05-26 13:59:40 +02:00
|
|
|
# Optionally (but recommended when possible), the user updating the value can be provided as the third parameter
|
|
|
|
# Eg.: Setting.set('my_setting', true, User.find_by(slug: 'admin'))
|
2023-01-25 16:40:55 +01:00
|
|
|
# @param name [String]
|
|
|
|
# @param value [String,Boolean,Numeric,NilClass]
|
2020-05-26 13:59:40 +02:00
|
|
|
def self.set(name, value, user = User.admins.first)
|
|
|
|
setting = find_or_initialize_by(name: name)
|
|
|
|
setting.save && setting.history_values.create(invoicing_profile: user.invoicing_profile, value: value.to_s)
|
|
|
|
end
|
2022-03-21 17:59:38 +01:00
|
|
|
|
|
|
|
# Check if the given setting was set
|
2023-01-25 16:40:55 +01:00
|
|
|
# @param name [String]
|
|
|
|
# @return [Boolean]
|
2022-03-21 17:59:38 +01:00
|
|
|
def self.set?(name)
|
2022-09-27 11:14:27 +02:00
|
|
|
!find_by(name: name)&.value.nil?
|
2022-03-21 17:59:38 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|