Move code from localize_input gem into Foodsoft

This commit is contained in:
Patrick Gansterer 2022-02-03 08:37:38 +01:00
parent c59715381e
commit 6a3636d8cb
9 changed files with 33 additions and 8 deletions

View file

@ -1,4 +1,5 @@
class Article < ApplicationRecord
include LocalizeInput
include PriceCalculation
# @!attribute name

View file

@ -1,4 +1,5 @@
class ArticlePrice < ApplicationRecord
include LocalizeInput
include PriceCalculation
# @!attribute price

View file

@ -1,4 +1,5 @@
class BankTransaction < ApplicationRecord
include LocalizeInput
# @!attribute external_id
# @return [String] Unique Identifier of the transaction within the bank account.
# @!attribute date

View file

@ -0,0 +1,25 @@
module LocalizeInput
extend ActiveSupport::Concern
class_methods do
def localize_input_of(*attr_names)
attr_names.flatten.each do |attr|
define_method "#{attr}=" do |input|
begin
if input.is_a? String
Rails.logger.debug "Input: #{input.inspect}"
separator = I18n.t("separator", :scope => "number.format")
delimiter = I18n.t("delimiter", :scope => "number.format")
input.gsub!(delimiter, "") if input.match(/\d+#{Regexp.escape(delimiter)}+\d+#{Regexp.escape(separator)}+\d+/) # Remove delimiter
input.gsub!(separator, ".") # Replace separator with db compatible character
end
self[attr] = input
rescue
Rails.logger.warn "Can't localize input: #{input}"
self[attr] = input
end
end
end
end
end
end

View file

@ -1,6 +1,8 @@
# financial transactions are the foodcoop internal financial transactions
# only ordergroups have an account balance and are happy to transfer money
class FinancialTransaction < ApplicationRecord
include LocalizeInput
belongs_to :ordergroup, optional: true
belongs_to :user
belongs_to :financial_link, optional: true

View file

@ -2,6 +2,8 @@
# The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity
#
class GroupOrderArticle < ApplicationRecord
include LocalizeInput
belongs_to :group_order
belongs_to :order_article
has_many :group_order_article_quantities, dependent: :destroy

View file

@ -1,5 +1,6 @@
class Invoice < ApplicationRecord
include CustomFields
include LocalizeInput
belongs_to :supplier
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'