Skip to content

Commit

Permalink
add some data normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Jun 20, 2024
1 parent bedff06 commit 742b88a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/models/observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Observer < ApplicationRecord
mount_base64_uploader :logo, LogoUploader
attr_accessor :delete_logo

normalizes :name, :address, :information_name, :data_name, :information_phone, :data_phone, with: -> { _1.strip }
normalizes :information_email, :data_email, with: -> { _1.strip.downcase }

has_many :countries_observers
has_many :countries, through: :countries_observers

Expand Down
7 changes: 3 additions & 4 deletions app/models/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
class Operator < ApplicationRecord
has_paper_trail

validates :name, presence: true, uniqueness: {case_sensitive: false}

mount_base64_uploader :logo, LogoUploader
attr_accessor :delete_logo

TYPES = ["Logging company", "Artisanal", "Community forest", "Estate", "Industrial agriculture", "Mining company",
"Sawmill", "Other", "Unknown"].freeze

normalizes :name, :details, :address, :website, with: -> { _1.strip }

belongs_to :country, inverse_of: :operators, optional: true
belongs_to :holding, inverse_of: :operators, optional: true
has_many :all_operator_documents, class_name: "OperatorDocument"
Expand Down Expand Up @@ -82,8 +82,7 @@ class Operator < ApplicationRecord

after_save :update_operator_name_on_fmus, if: :saved_change_to_name?

validates :name, presence: true
validates :name, uniqueness: {case_sensitive: false}
validates :name, presence: true, uniqueness: {case_sensitive: false}
validates :website, url: true, if: lambda { |x| x.website.present? }
validates :operator_type, inclusion: {in: TYPES, message: "can't be %{value}. Valid values are: #{TYPES.join(", ")} "}
validates :country, presence: true, on: :create, unless: :special_unknown?
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class User < ApplicationRecord
TEMP_EMAIL_REGEX = /\Achange@tmp/
PERMISSIONS = %w[operator ngo ngo_manager government]

normalizes :name, with: -> { _1.strip }

enum permissions_request: {operator: 1, ngo: 2, ngo_manager: 4, government: 6, holding: 7}

belongs_to :country, inverse_of: :users, optional: true
Expand Down
23 changes: 23 additions & 0 deletions lib/tasks/single_use/data_normalization.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace :normalize do
# TODO: remove it after the data normalization is done
task data: :environment do
ActiveRecord::Base.transaction do
User.unscoped.find_each do |user|
%i[name].each { |attr| user.normalize_attribute(attr) }
user.save!(validate: false)
end

Observer.unscoped.find_each do |observer|
%i[name address information_name information_email information_phone data_name data_email data_phone].each do |attr|
observer.normalize_attribute(attr)
end
observer.save!(validate: false)
end

Operator.unscoped.find_each do |operator|
%i[name details address website].each { |attr| operator.normalize_attribute(attr) }
operator.save!(validate: false)
end
end
end
end

0 comments on commit 742b88a

Please sign in to comment.