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
|
2022-09-27 11:14:27 +02:00
|
|
|
has_many :history_values, dependent: :destroy
|
2020-06-15 13:20:48 +02:00
|
|
|
# The following list contains all the settings that can be customized from the Fab-manager's UI.
|
2021-06-10 10:39:42 +02:00
|
|
|
# A few of them that are system settings, that should not be updated manually (uuid, origin...).
|
2016-03-23 18:39:41 +01:00
|
|
|
validates :name, inclusion:
|
2018-12-17 16:02:02 +01:00
|
|
|
{ in: %w[about_title
|
2016-03-23 18:39:41 +01:00
|
|
|
about_body
|
|
|
|
about_contacts
|
2019-04-18 15:57:56 +02:00
|
|
|
privacy_draft
|
2019-04-18 12:30:00 +02:00
|
|
|
privacy_body
|
|
|
|
privacy_dpo
|
2016-03-23 18:39:41 +01:00
|
|
|
twitter_name
|
|
|
|
home_blogpost
|
|
|
|
machine_explications_alert
|
|
|
|
training_explications_alert
|
|
|
|
training_information_message
|
|
|
|
subscription_explications_alert
|
|
|
|
invoice_logo
|
|
|
|
invoice_reference
|
|
|
|
invoice_code-active
|
|
|
|
invoice_code-value
|
|
|
|
invoice_order-nb
|
|
|
|
invoice_VAT-active
|
|
|
|
invoice_VAT-rate
|
2021-12-23 09:25:48 +01:00
|
|
|
invoice_VAT-rate_Machine
|
|
|
|
invoice_VAT-rate_Training
|
|
|
|
invoice_VAT-rate_Space
|
|
|
|
invoice_VAT-rate_Event
|
|
|
|
invoice_VAT-rate_Subscription
|
2022-09-08 20:37:22 +02:00
|
|
|
invoice_VAT-rate_Product
|
2016-03-23 18:39:41 +01:00
|
|
|
invoice_text
|
|
|
|
invoice_legals
|
|
|
|
booking_window_start
|
|
|
|
booking_window_end
|
|
|
|
booking_move_enable
|
|
|
|
booking_move_delay
|
|
|
|
booking_cancel_enable
|
|
|
|
booking_cancel_delay
|
|
|
|
main_color
|
|
|
|
secondary_color
|
|
|
|
fablab_name
|
2016-08-17 12:49:52 +02:00
|
|
|
name_genre
|
|
|
|
reminder_enable
|
2016-11-29 16:46:19 +01:00
|
|
|
reminder_delay
|
2017-02-15 09:48:27 +01:00
|
|
|
event_explications_alert
|
2017-08-24 18:34:18 +02:00
|
|
|
space_explications_alert
|
|
|
|
visibility_yearly
|
2018-07-24 12:24:51 +02:00
|
|
|
visibility_others
|
2018-09-18 15:30:54 +02:00
|
|
|
display_name_enable
|
2019-07-30 16:06:35 +02:00
|
|
|
machines_sort_by
|
|
|
|
accounting_journal_code
|
2019-09-17 12:35:34 +02:00
|
|
|
accounting_card_client_code
|
|
|
|
accounting_card_client_label
|
2019-09-19 11:51:14 +02:00
|
|
|
accounting_wallet_client_code
|
|
|
|
accounting_wallet_client_label
|
|
|
|
accounting_other_client_code
|
|
|
|
accounting_other_client_label
|
2019-07-30 16:06:35 +02:00
|
|
|
accounting_wallet_code
|
|
|
|
accounting_wallet_label
|
|
|
|
accounting_VAT_code
|
|
|
|
accounting_VAT_label
|
|
|
|
accounting_subscription_code
|
|
|
|
accounting_subscription_label
|
|
|
|
accounting_Machine_code
|
|
|
|
accounting_Machine_label
|
|
|
|
accounting_Training_code
|
|
|
|
accounting_Training_label
|
|
|
|
accounting_Event_code
|
|
|
|
accounting_Event_label
|
|
|
|
accounting_Space_code
|
2020-01-13 17:05:53 +01:00
|
|
|
accounting_Space_label
|
2022-09-08 20:37:22 +02:00
|
|
|
accounting_Product_code
|
|
|
|
accounting_Product_label
|
2020-01-13 17:05:53 +01:00
|
|
|
hub_last_version
|
2020-01-15 10:46:04 +01:00
|
|
|
hub_public_key
|
2020-01-21 16:16:12 +01:00
|
|
|
fab_analytics
|
2020-01-22 11:53:40 +01:00
|
|
|
link_name
|
2020-01-27 17:10:29 +01:00
|
|
|
home_content
|
2020-04-07 17:53:19 +02:00
|
|
|
home_css
|
|
|
|
origin
|
2020-05-12 15:04:06 +02:00
|
|
|
uuid
|
2020-05-19 16:20:59 +02:00
|
|
|
phone_required
|
2020-05-19 17:08:11 +02:00
|
|
|
tracking_id
|
2020-05-20 17:18:19 +02:00
|
|
|
book_overlapping_slots
|
2020-05-25 17:36:53 +02:00
|
|
|
slot_duration
|
2020-05-26 10:53:49 +02:00
|
|
|
events_in_calendar
|
2020-05-26 15:39:28 +02:00
|
|
|
spaces_module
|
2020-05-26 18:07:07 +02:00
|
|
|
plans_module
|
2020-05-27 12:29:45 +02:00
|
|
|
invoicing_module
|
2020-05-27 15:39:39 +02:00
|
|
|
facebook_app_id
|
2020-05-27 16:35:30 +02:00
|
|
|
twitter_analytics
|
|
|
|
recaptcha_site_key
|
2020-05-27 18:49:53 +02:00
|
|
|
recaptcha_secret_key
|
2020-06-01 12:15:16 +02:00
|
|
|
feature_tour_display
|
2020-06-01 17:32:32 +02:00
|
|
|
email_from
|
2020-06-03 16:25:13 +02:00
|
|
|
disqus_shortname
|
|
|
|
allowed_cad_extensions
|
2020-06-08 11:38:49 +02:00
|
|
|
allowed_cad_mime_types
|
|
|
|
openlab_app_id
|
2020-06-08 16:15:44 +02:00
|
|
|
openlab_app_secret
|
2020-06-08 17:42:59 +02:00
|
|
|
openlab_default
|
2020-06-09 13:09:31 +02:00
|
|
|
online_payment_module
|
|
|
|
stripe_public_key
|
2020-06-10 16:37:11 +02:00
|
|
|
stripe_secret_key
|
2020-06-15 10:58:15 +02:00
|
|
|
stripe_currency
|
2020-06-15 11:57:13 +02:00
|
|
|
invoice_prefix
|
2020-06-15 12:44:43 +02:00
|
|
|
confirmation_required
|
2020-06-17 12:20:51 +02:00
|
|
|
wallet_module
|
2020-09-29 09:39:32 +02:00
|
|
|
statistics_module
|
2020-12-22 16:39:37 +01:00
|
|
|
upcoming_events_shown
|
2021-03-03 15:28:56 +01:00
|
|
|
payment_schedule_prefix
|
2021-03-10 15:23:56 +01:00
|
|
|
trainings_module
|
2021-03-24 14:34:24 +01:00
|
|
|
address_required
|
2021-05-24 16:34:27 +02:00
|
|
|
accounting_Error_code
|
2021-05-25 14:06:30 +02:00
|
|
|
accounting_Error_label
|
2021-03-31 16:03:51 +02:00
|
|
|
payment_gateway
|
|
|
|
payzen_username
|
|
|
|
payzen_password
|
|
|
|
payzen_endpoint
|
|
|
|
payzen_public_key
|
2021-04-07 16:21:12 +02:00
|
|
|
payzen_hmac
|
2021-06-15 11:57:43 +02:00
|
|
|
payzen_currency
|
2021-06-25 17:24:34 +02:00
|
|
|
public_agenda_module
|
2021-09-20 19:43:05 +02:00
|
|
|
renew_pack_threshold
|
2021-10-21 18:09:36 +02:00
|
|
|
pack_only_for_subscription
|
2021-12-21 16:32:02 +01:00
|
|
|
overlapping_categories
|
2022-01-04 15:27:58 +01:00
|
|
|
extended_prices_in_same_day
|
2022-03-23 13:14:34 +01:00
|
|
|
public_registrations
|
|
|
|
accounting_Pack_code
|
2022-05-02 17:54:58 +02:00
|
|
|
accounting_Pack_label
|
2022-05-04 10:06:00 +02:00
|
|
|
facebook
|
|
|
|
twitter
|
|
|
|
viadeo
|
|
|
|
linkedin
|
|
|
|
instagram
|
|
|
|
youtube
|
|
|
|
vimeo
|
|
|
|
dailymotion
|
|
|
|
github
|
|
|
|
echosciences
|
|
|
|
pinterest
|
|
|
|
lastfm
|
2022-05-11 11:54:00 +02:00
|
|
|
flickr
|
2022-05-11 15:45:49 +02:00
|
|
|
machines_module
|
2022-03-18 19:44:30 +01:00
|
|
|
user_change_group
|
|
|
|
user_validation_required
|
2022-07-27 09:59:23 +02:00
|
|
|
user_validation_required_list
|
2022-09-27 11:14:27 +02:00
|
|
|
show_username_in_admin_list
|
2022-09-28 17:45:48 +02:00
|
|
|
store_module
|
2022-10-12 16:48:39 +02:00
|
|
|
store_withdrawal_instructions
|
|
|
|
store_hidden] }
|
2021-10-22 14:50:02 +02:00
|
|
|
# WARNING: when adding a new key, you may also want to add it in:
|
|
|
|
# - config/locales/en.yml#settings
|
|
|
|
# - app/frontend/src/javascript/models/setting.ts#SettingName
|
|
|
|
# - db/seeds.rb (to set the default value)
|
|
|
|
# - app/policies/setting_policy.rb#public_whitelist (if the setting can be read by anyone)
|
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
|
|
|
|
|
2020-06-17 12:20:51 +02:00
|
|
|
def previous_update
|
|
|
|
previous_value = history_values.order(HistoryValue.arel_table['created_at'].desc).limit(2).last
|
|
|
|
previous_value&.created_at
|
|
|
|
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.
|
2020-05-13 15:02:03 +02:00
|
|
|
# Usage: Setting.get('my_setting')
|
2021-01-04 18:12:44 +01:00
|
|
|
# @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
|
|
|
|
# Usage: Setting.set('my_setting', true)
|
|
|
|
# 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'))
|
|
|
|
##
|
|
|
|
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
|
|
|
|
##
|
|
|
|
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
|