From 6a3636d8cb161decef0e7ae88940559323312ea5 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Thu, 3 Feb 2022 08:37:38 +0100 Subject: [PATCH] Move code from localize_input gem into Foodsoft --- Gemfile | 1 - Gemfile.lock | 7 ------- app/models/article.rb | 1 + app/models/article_price.rb | 1 + app/models/bank_transaction.rb | 1 + app/models/concerns/localize_input.rb | 25 +++++++++++++++++++++++++ app/models/financial_transaction.rb | 2 ++ app/models/group_order_article.rb | 2 ++ app/models/invoice.rb | 1 + 9 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 app/models/concerns/localize_input.rb diff --git a/Gemfile b/Gemfile index fe7ceeed..a46969fc 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,6 @@ gem 'haml-rails' gem 'kaminari' gem 'simple_form' gem 'inherited_resources' -gem 'localize_input', git: 'https://github.com/carchrae/localize_input.git' gem 'daemons' gem 'doorkeeper' gem 'doorkeeper-i18n' diff --git a/Gemfile.lock b/Gemfile.lock index cbffaaac..7c08ec69 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,3 @@ -GIT - remote: https://github.com/carchrae/localize_input.git - revision: 597e33bb42a3b0fd58c3ea7d4f5a208c2911b43d - specs: - localize_input (0.1.0) - GIT remote: https://github.com/technoweenie/acts_as_versioned.git revision: 63b1fc8529d028fae632fe80ec0cb25df56cd76b @@ -606,7 +600,6 @@ DEPENDENCIES kaminari less-rails listen - localize_input! mailcatcher meta_request midi-smtp-server diff --git a/app/models/article.rb b/app/models/article.rb index f8c129b6..76a68605 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,4 +1,5 @@ class Article < ApplicationRecord + include LocalizeInput include PriceCalculation # @!attribute name diff --git a/app/models/article_price.rb b/app/models/article_price.rb index cca4c200..f6879eac 100644 --- a/app/models/article_price.rb +++ b/app/models/article_price.rb @@ -1,4 +1,5 @@ class ArticlePrice < ApplicationRecord + include LocalizeInput include PriceCalculation # @!attribute price diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index fa1a3da5..ff2dc9ef 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -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 diff --git a/app/models/concerns/localize_input.rb b/app/models/concerns/localize_input.rb new file mode 100644 index 00000000..8198d7b2 --- /dev/null +++ b/app/models/concerns/localize_input.rb @@ -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 diff --git a/app/models/financial_transaction.rb b/app/models/financial_transaction.rb index 5c26058b..bd2c4e58 100644 --- a/app/models/financial_transaction.rb +++ b/app/models/financial_transaction.rb @@ -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 diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index 8df4fa59..5a02734d 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -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 diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8a9dde9f..f2a8866f 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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'