Append Logic for converted_name before save db

parent 942f54ff
Pipeline #899 canceled with stages
in 0 seconds
class ApplicationRecord < ActiveRecord::Base class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
def convert_attribute def convert_attribute(val)
self.converted_name = normalize_attribute.mb_chars.normalize(:kd).gsub(/[Đđ]/, 'd').gsub(/[^\x00-\x7F]/,'').gsub(/[\W+]/,' ').downcase.to_s.split(' ').join('-') return '' if val.blank?
[val, rand(10000)] = normalize_attribute.mb_chars.normalize(:kd).gsub(/[Đđ]/, 'd').gsub(/[^\x00-\x7F]/,'').gsub(/[\W+]/,' ').downcase.to_s.split(' ').join('-')
end end
end end
class City < ApplicationRecord class City < ApplicationRecord
before_save :convert_attribute before_save :set_converted_name
has_many :city_jobs has_many :city_jobs
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
...@@ -11,7 +12,7 @@ class City < ApplicationRecord ...@@ -11,7 +12,7 @@ class City < ApplicationRecord
private private
def normalize_attribute def set_converted_name
"#{name} #{rand(10000)}" converted_name = convert_attribute(name)
end end
end end
class Company < ApplicationRecord class Company < ApplicationRecord
before_save :convert_attribute before_save :set_converted_name
has_many :jobs has_many :jobs
private private
def normalize_attribute def set_converted_name
"#{name} #{rand(10000)}" converted_name = convert_attribute(name)
end end
end end
class Industry < ApplicationRecord class Industry < ApplicationRecord
before_save :convert_attribute before_save :set_converted_name
has_many :industry_jobs has_many :industry_jobs
has_many :jobs, through: :industry_jobs has_many :jobs, through: :industry_jobs
...@@ -8,7 +9,7 @@ class Industry < ApplicationRecord ...@@ -8,7 +9,7 @@ class Industry < ApplicationRecord
private private
def normalize_attribute def set_converted_name
"#{name} #{rand(10000)}" converted_name = convert_attribute(name)
end end
end end
class Job < ApplicationRecord class Job < ApplicationRecord
before_save :convert_attribute before_save :set_converted_name
belongs_to :company belongs_to :company
has_many :city_jobs has_many :city_jobs
has_many :cities, through: :city_jobs has_many :cities, through: :city_jobs
...@@ -26,8 +27,8 @@ class Job < ApplicationRecord ...@@ -26,8 +27,8 @@ class Job < ApplicationRecord
private private
def normalize_attribute def set_converted_name
"#{title} #{rand(10000)}" converted_name = convert_attribute(title)
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment