56 lines
1.1 KiB
Ruby
56 lines
1.1 KiB
Ruby
# app/models/configuracao.rb
|
|
class Configuracao < ApplicationRecord
|
|
# Chaves válidas do sistema
|
|
CHAVES = %w[
|
|
preco_entrega
|
|
preco_retirada
|
|
preco_bonus
|
|
preco_desconto
|
|
notificacao_whatsapp
|
|
notificacao_email
|
|
empresa_nome
|
|
empresa_logo
|
|
].freeze
|
|
|
|
CHAVES_MOEDA = %w[
|
|
preco_entrega
|
|
preco_retirada
|
|
preco_bonus
|
|
preco_desconto
|
|
].freeze
|
|
|
|
validates :chave, presence: true, inclusion: { in: CHAVES }, uniqueness: true
|
|
validates :valor, presence: true
|
|
|
|
# ── Acesso rápido ───────────────────────────────────────────
|
|
|
|
def self.valor(chave)
|
|
find_by(chave: chave)&.valor
|
|
end
|
|
|
|
def self.preco_entrega
|
|
valor('preco_entrega').to_f
|
|
end
|
|
|
|
def self.preco_retirada
|
|
valor('preco_retirada').to_f
|
|
end
|
|
|
|
def self.preco_bonus
|
|
valor('preco_bonus').to_f
|
|
end
|
|
|
|
def self.preco_desconto
|
|
valor('preco_desconto').to_f
|
|
end
|
|
|
|
def moeda?
|
|
CHAVES_MOEDA.include?(chave)
|
|
end
|
|
|
|
def valor_formatado
|
|
return "R$ #{format('%.2f', valor.to_f).gsub('.', ',')}" if moeda?
|
|
valor
|
|
end
|
|
end
|