1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-06 01:08:21 +01:00

refactored code to use Setting.get

This commit is contained in:
Sylvain 2020-05-13 15:02:03 +02:00
parent f47af21b42
commit f80eb230ca
24 changed files with 52 additions and 48 deletions

View File

@ -193,7 +193,7 @@ class API::AvailabilitiesController < API::ApiController
end end
def define_max_visibility def define_max_visibility
@visi_max_year = Setting.find_by(name: 'visibility_yearly').value.to_i.months.since @visi_max_year = Setting.get('visibility_yearly').to_i.months.since
@visi_max_other = Setting.find_by(name: 'visibility_others').value.to_i.months.since @visi_max_other = Setting.get('visibility_others').to_i.months.since
end end
end end

View File

@ -7,7 +7,7 @@ class API::MachinesController < API::ApiController
respond_to :json respond_to :json
def index def index
sort_by = Setting.find_by(name: 'machines_sort_by').value || 'default' sort_by = Setting.get('machines_sort_by') || 'default'
@machines = if sort_by == 'default' @machines = if sort_by == 'default'
Machine.includes(:machine_image, :plans) Machine.includes(:machine_image, :plans)
else else

View File

@ -15,7 +15,7 @@ class API::VersionController < API::ApiController
origin.save! origin.save!
end end
# get the last version # get the last version
update_status = Setting.find_by(name: 'hub_last_version')&.value || '{}' update_status = Setting.get('hub_last_version') || '{}'
json = JSON.parse(update_status) json = JSON.parse(update_status)
json['current'] = Version.current json['current'] = Version.current

View File

@ -7,6 +7,6 @@ class Rss::EventsController < Rss::RssController
@events = Event.includes(:event_image, :event_files, :availability, :category) @events = Event.includes(:event_image, :event_files, :availability, :category)
.where('availabilities.start_at >= ?', DateTime.current) .where('availabilities.start_at >= ?', DateTime.current)
.order('availabilities.start_at ASC').references(:availabilities).limit(10) .order('availabilities.start_at ASC').references(:availabilities).limit(10)
@fab_name = Setting.find_by(name: 'fablab_name').value @fab_name = Setting.get('fablab_name')
end end
end end

View File

@ -2,6 +2,6 @@ class Rss::ProjectsController < Rss::RssController
def index def index
@projects = Project.includes(:project_image, :users).published.order('created_at desc').limit(10) @projects = Project.includes(:project_image, :users).published.order('created_at desc').limit(10)
@fab_name = Setting.find_by(name: 'fablab_name').value @fab_name = Setting.get('fablab_name')
end end
end end

View File

@ -10,7 +10,7 @@ class Profile < ApplicationRecord
validates :first_name, presence: true, length: { maximum: 30 } validates :first_name, presence: true, length: { maximum: 30 }
validates :last_name, presence: true, length: { maximum: 30 } validates :last_name, presence: true, length: { maximum: 30 }
validates_numericality_of :phone, only_integer: true, allow_blank: false, if: -> { Rails.application.secrets.phone_required } validates_numericality_of :phone, only_integer: true, allow_blank: false, if: -> { Setting.get('phone_required') }
after_commit :update_invoicing_profile, if: :invoicing_data_was_modified?, on: [:update] after_commit :update_invoicing_profile, if: :invoicing_data_was_modified?, on: [:update]

View File

@ -91,4 +91,13 @@ class Setting < ApplicationRecord
admin = User.admins.first admin = User.admins.first
save && history_values.create(invoicing_profile: admin.invoicing_profile, value: val) save && history_values.create(invoicing_profile: admin.invoicing_profile, value: val)
end end
##
# Return the value of the requested setting
# Usage: Setting.get('my_setting')
# @return {string}
##
def self.get(name)
find_by(name: name)&.value
end
end end

View File

@ -34,11 +34,11 @@ class Stylesheet < ApplicationRecord
end end
def self.primary def self.primary
Setting.find_by(name: 'main_color')&.value Setting.get('main_color')
end end
def self.secondary def self.secondary
Setting.find_by(name: 'secondary_color')&.value Setting.get('secondary_color')
end end
def self.primary_light def self.primary_light
@ -124,7 +124,7 @@ class Stylesheet < ApplicationRecord
## ===== HOME PAGE ===== ## ===== HOME PAGE =====
def self.home_style def self.home_style
style = Setting.find_by(name: 'home_css')&.value style = Setting.get('home_css')
".home-page { #{style} }" ".home-page { #{style} }"
end end

View File

@ -41,8 +41,8 @@ class PDF::Invoice < Prawn::Document
else else
text I18n.t('invoices.invoice_reference', REF: invoice.reference), leading: 3 text I18n.t('invoices.invoice_reference', REF: invoice.reference), leading: 3
end end
if Setting.find_by(name: 'invoice_code-active').value == 'true' if Setting.get('invoice_code-active') == 'true'
text I18n.t('invoices.code', CODE: Setting.find_by(name: 'invoice_code-value').value), leading: 3 text I18n.t('invoices.code', CODE: Setting.get('invoice_code-value')), leading: 3
end end
if invoice.invoiced_type != WalletTransaction.name if invoice.invoiced_type != WalletTransaction.name
if invoice.is_a?(Avoir) if invoice.is_a?(Avoir)
@ -251,7 +251,7 @@ class PDF::Invoice < Prawn::Document
row(0).font_style = :bold row(0).font_style = :bold
column(1).style align: :right column(1).style align: :right
if Setting.find_by(name: 'invoice_VAT-active').value == 'true' if Setting.get('invoice_VAT-active') == 'true'
# Total incl. taxes # Total incl. taxes
row(-1).style align: :right row(-1).style align: :right
row(-1).background_color = 'E4E4E4' row(-1).background_color = 'E4E4E4'
@ -332,7 +332,7 @@ class PDF::Invoice < Prawn::Document
# important information # important information
move_down 40 move_down 40
txt = parse_html(Setting.find_by(name: 'invoice_text').value) txt = parse_html(Setting.get('invoice_text'))
txt.each_line do |line| txt.each_line do |line|
text line, style: :bold, inline_format: true text line, style: :bold, inline_format: true
end end
@ -340,7 +340,7 @@ class PDF::Invoice < Prawn::Document
# address and legals information # address and legals information
move_down 40 move_down 40
txt = parse_html(Setting.find_by(name: 'invoice_legals').value) txt = parse_html(Setting.get('invoice_legals'))
txt.each_line do |line| txt.each_line do |line|
text line, align: :right, leading: 4, inline_format: true text line, align: :right, leading: 4, inline_format: true
end end

View File

@ -4,8 +4,8 @@
class SlotPolicy < ApplicationPolicy class SlotPolicy < ApplicationPolicy
def update? def update?
# check that the update is allowed and the prevention delay has not expired # check that the update is allowed and the prevention delay has not expired
delay = Setting.find_by(name: 'booking_move_delay').value.to_i delay = Setting.get('booking_move_delay').to_i
enabled = (Setting.find_by(name: 'booking_move_enable').value == 'true') enabled = (Setting.get('booking_move_enable') == 'true')
# these condition does not apply to admins # these condition does not apply to admins
user.admin? || user.manager? || user.admin? || user.manager? ||

View File

@ -15,7 +15,7 @@ class AccountingExportService
@date_format = '%d/%m/%Y' @date_format = '%d/%m/%Y'
@label_max_length = 50 @label_max_length = 50
@export_zeros = false @export_zeros = false
@journal_code = Setting.find_by(name: 'accounting_journal_code')&.value || '' @journal_code = Setting.get('accounting_journal_code') || ''
@date_format = date_format @date_format = date_format
@columns = columns @columns = columns
end end

View File

@ -4,7 +4,7 @@
class Availabilities::StatusService class Availabilities::StatusService
def initialize(current_user_role) def initialize(current_user_role)
@current_user_role = current_user_role @current_user_role = current_user_role
@show_name = (@current_user_role == 'admin' || Setting.find_by(name: 'display_name_enable').value == 'true') @show_name = (@current_user_role == 'admin' || Setting.get('display_name_enable') == 'true')
end end
# check that the provided machine slot is reserved or not and modify it accordingly # check that the provided machine slot is reserved or not and modify it accordingly

View File

@ -48,7 +48,7 @@ class HealthService
end end
def self.stats def self.stats
enable = Setting.find_by(name: 'fab_analytics')&.value enable = Setting.get('fab_analytics')
return false if enable == 'false' return false if enable == 'false'
require 'openssl' require 'openssl'
@ -56,7 +56,7 @@ class HealthService
row_stats.to_json.to_s row_stats.to_json.to_s
key = Setting.find_by(name: 'hub_public_key')&.value key = Setting.get('hub_public_key')
return false unless key return false unless key
public_key = OpenSSL::PKey::RSA.new(key) public_key = OpenSSL::PKey::RSA.new(key)

View File

@ -4,7 +4,7 @@
class InvoiceReferenceService class InvoiceReferenceService
class << self class << self
def generate_reference(invoice, date: DateTime.current, avoir: false) def generate_reference(invoice, date: DateTime.current, avoir: false)
pattern = Setting.find_by(name: 'invoice_reference').value pattern = Setting.get('invoice_reference')
reference = replace_invoice_number_pattern(pattern, invoice) reference = replace_invoice_number_pattern(pattern, invoice)
reference = replace_date_pattern(reference, date) reference = replace_date_pattern(reference, date)
@ -31,7 +31,7 @@ class InvoiceReferenceService
end end
def generate_order_number(invoice) def generate_order_number(invoice)
pattern = Setting.find_by(name: 'invoice_order-nb').value pattern = Setting.get('invoice_order-nb')
# global invoice number (nn..nn) # global invoice number (nn..nn)
reference = pattern.gsub(/n+(?![^\[]*\])/) do |match| reference = pattern.gsub(/n+(?![^\[]*\])/) do |match|

View File

@ -7,7 +7,7 @@
<meta name="description" content=""> <meta name="description" content="">
<meta name="author" content=""> <meta name="author" content="">
<title><%=Setting.find_by(name: 'fablab_name').value%></title> <title><%=Setting.get('fablab_name')%></title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,600italic,700,800,700italic' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,600italic,700,800,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700,300italic' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700,300italic' rel='stylesheet' type='text/css'>

View File

@ -1,5 +1,5 @@
<% fablab_name = Setting.find_by(name: 'fablab_name').value %> <% fablab_name = Setting.get('fablab_name') %>
<% fablab_gender = Setting.find_by(name: 'name_genre').value %> <% fablab_gender = Setting.get('name_genre') %>
<% primary_color = Stylesheet.primary %> <% primary_color = Stylesheet.primary %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

View File

@ -18,7 +18,7 @@
url_path = File.join(root_url, 'sso-redirect') url_path = File.join(root_url, 'sso-redirect')
%> %>
<p><%= t(".body.the_platform") %> <%= Setting.find_by(name: 'fablab_name').value %> <%= t(".body.is_changing_its_auth_system_and_will_now_use") %> <%= active_provider.name %> <%= t(".body.instead_of") %> <%= AuthProvider.find_by(status: 'previous').name %>.</p> <p><%= t(".body.the_platform") %> <%= Setting.get('fablab_name') %> <%= t(".body.is_changing_its_auth_system_and_will_now_use") %> <%= active_provider.name %> <%= t(".body.instead_of") %> <%= AuthProvider.find_by(status: 'previous').name %>.</p>
<p><%= t('.body.consequence_of_the_modification') %></p> <p><%= t('.body.consequence_of_the_modification') %></p>

View File

@ -1,8 +1,8 @@
<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> <%= render 'notifications_mailer/shared/hello', recipient: @recipient %>
<% <%
name = Setting.find_by(name: 'fablab_name').value name = Setting.get('fablab_name')
gender = Setting.find_by(name: 'name_genre').value gender = Setting.get('name_genre')
%> %>
<p><%= _t('.body.role_changed_html', <p><%= _t('.body.role_changed_html',
{ {

View File

@ -17,8 +17,8 @@
<p><%= _t('.body.intro', <p><%= _t('.body.intro',
{ {
GENDER: Setting.find_by(name: 'name_genre').value, GENDER: Setting.get('name_genre'),
FABLAB: Setting.find_by(name: 'fablab_name').value FABLAB: Setting.get('fablab_name')
}) })
# messageFormat # messageFormat
%> <%= link_to URI(root_url).host, root_url %>. %> <%= link_to URI(root_url).host, root_url %>.

View File

@ -918,8 +918,8 @@ end
unless Setting.find_by(name: 'link_name').try(:value) unless Setting.find_by(name: 'link_name').try(:value)
include ApplicationHelper # rubocop:disable Style/MixinUsage include ApplicationHelper # rubocop:disable Style/MixinUsage
name = Setting.find_by(name: 'fablab_name').value name = Setting.get('fablab_name')
gender = Setting.find_by(name: 'name_genre').value gender = Setting.get('name_genre')
setting = Setting.find_or_initialize_by(name: 'link_name') setting = Setting.find_or_initialize_by(name: 'link_name')
setting.value = _t('app.public.common.about_the_fablab', NAME: name, GENDER: gender) setting.value = _t('app.public.common.about_the_fablab', NAME: name, GENDER: gender)
setting.save setting.save

View File

@ -112,11 +112,6 @@ This is useful if you have your own invoicing system and you want to prevent Fab
If set to 'true', the wallet will be disabled. If set to 'true', the wallet will be disabled.
This is useful if you won't use wallet system. This is useful if you won't use wallet system.
<a name="PHONE_REQUIRED"></a>
PHONE_REQUIRED
If set to 'false' the phone number won't be required to register a new user on the software.
<a name="BOOK_SLOT_AT_SAME_TIME"></a> <a name="BOOK_SLOT_AT_SAME_TIME"></a>
BOOK_SLOT_AT_SAME_TIME BOOK_SLOT_AT_SAME_TIME

View File

@ -3,8 +3,8 @@
# Fab-manager central hub (remote host) # Fab-manager central hub (remote host)
class FabHub class FabHub
def self.version_check_payload def self.version_check_payload
uuid = Setting.find_by(name: 'uuid')&.value uuid = Setting.get('uuid')
origin = Setting.find_by(name: 'origin')&.value || "#{Rails.application.secrets.default_protocol}://#{Rails.application.secrets.default_host}" origin = Setting.get('origin') || "#{Rails.application.secrets.default_protocol}://#{Rails.application.secrets.default_host}"
{ {
uuid: uuid, uuid: uuid,
origin: origin, origin: origin,

View File

@ -10,7 +10,7 @@ class Version
# currently published # currently published
def self.up_to_date? def self.up_to_date?
hub_version = Setting.find_by(name: 'hub_last_version')&.value hub_version = Setting.get('hub_last_version')
return unless hub_version return unless hub_version
json = JSON.parse(hub_version) json = JSON.parse(hub_version)

View File

@ -51,7 +51,7 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
# test values # test values
# first line = client line # first line = client line
journal_code = Setting.find_by(name: 'accounting_journal_code').value journal_code = Setting.get('accounting_journal_code')
assert_equal journal_code, data[0][I18n.t('accounting_export.journal_code')], 'Wrong journal code' assert_equal journal_code, data[0][I18n.t('accounting_export.journal_code')], 'Wrong journal code'
first_invoice = Invoice.first first_invoice = Invoice.first
@ -59,10 +59,10 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
assert_equal entry_date, DateTime.parse(data[0][I18n.t('accounting_export.date')]), 'Wrong date' assert_equal entry_date, DateTime.parse(data[0][I18n.t('accounting_export.date')]), 'Wrong date'
if first_invoice.paid_with_stripe? if first_invoice.paid_with_stripe?
card_client_code = Setting.find_by(name: 'accounting_card_client_code').value card_client_code = Setting.get('accounting_card_client_code')
assert_equal card_client_code, data[0][I18n.t('accounting_export.account_code')], 'Account code for card client is wrong' assert_equal card_client_code, data[0][I18n.t('accounting_export.account_code')], 'Account code for card client is wrong'
card_client_label = Setting.find_by(name: 'accounting_card_client_label').value card_client_label = Setting.get('accounting_card_client_label')
assert_equal card_client_label, data[0][I18n.t('accounting_export.account_label')], 'Account label for card client is wrong' assert_equal card_client_label, data[0][I18n.t('accounting_export.account_label')], 'Account label for card client is wrong'
else else
STDERR.puts "WARNING: unable to test accurately accounting export: invoice #{first_invoice.id} was not paid by card" STDERR.puts "WARNING: unable to test accurately accounting export: invoice #{first_invoice.id} was not paid by card"
@ -93,10 +93,10 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
assert_equal entry_date, DateTime.parse(data[1][I18n.t('accounting_export.date')]), 'Wrong date' assert_equal entry_date, DateTime.parse(data[1][I18n.t('accounting_export.date')]), 'Wrong date'
if first_invoice.subscription_invoice? if first_invoice.subscription_invoice?
subscription_code = Setting.find_by(name: 'accounting_subscription_code').value subscription_code = Setting.get('accounting_subscription_code')
assert_equal subscription_code, data[1][I18n.t('accounting_export.account_code')], 'Account code for subscription is wrong' assert_equal subscription_code, data[1][I18n.t('accounting_export.account_code')], 'Account code for subscription is wrong'
subscription_label = Setting.find_by(name: 'accounting_subscription_label').value subscription_label = Setting.get('accounting_subscription_label')
assert_equal subscription_label, data[1][I18n.t('accounting_export.account_label')], 'Account label for subscription is wrong' assert_equal subscription_label, data[1][I18n.t('accounting_export.account_label')], 'Account label for subscription is wrong'
end end
@ -122,10 +122,10 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
client_row[I18n.t('accounting_export.line_label')], client_row[I18n.t('accounting_export.line_label')],
'Line label does not contains the reference to the invoiced item' 'Line label does not contains the reference to the invoiced item'
machine_code = Setting.find_by(name: 'accounting_Machine_code').value machine_code = Setting.get('accounting_Machine_code')
assert_equal machine_code, item_row[I18n.t('accounting_export.account_code')], 'Account code for machine reservation is wrong' assert_equal machine_code, item_row[I18n.t('accounting_export.account_code')], 'Account code for machine reservation is wrong'
machine_label = Setting.find_by(name: 'accounting_Machine_label').value machine_label = Setting.get('accounting_Machine_label')
assert_equal machine_label, item_row[I18n.t('accounting_export.account_label')], 'Account label for machine reservation is wrong' assert_equal machine_label, item_row[I18n.t('accounting_export.account_label')], 'Account label for machine reservation is wrong'
else else