allow to match category names on import/sync
This commit is contained in:
parent
16b78ba2a0
commit
d9c61b2db3
8 changed files with 30 additions and 6 deletions
|
|
@ -12,10 +12,26 @@ class ArticleCategory < ActiveRecord::Base
|
|||
|
||||
normalize_attributes :name, :description
|
||||
|
||||
validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 }
|
||||
validates :name, :presence => true, :uniqueness => true, :length => { :minimum => 2 }
|
||||
|
||||
before_destroy :check_for_associated_articles
|
||||
|
||||
# Find a category that matches a category name; may return nil.
|
||||
# TODO more intelligence like remembering earlier associations (global and/or per-supplier)
|
||||
def self.find_match(category)
|
||||
return if category.blank? or category.length < 3
|
||||
c = nil
|
||||
## exact match - not needed, will be returned by next query as well
|
||||
#c ||= ArticleCategory.where(name: category).first
|
||||
# case-insensitive substring match (take the closest match = shortest)
|
||||
c = ArticleCategory.where('name LIKE ?', "%#{category}%") unless c and c.any?
|
||||
# case-insensitive phrase present in category description
|
||||
c = ArticleCategory.where('description LIKE ?', "%#{category}%").select {|s| s.description.match /(^|,)\s*#{category}\s*(,|$)/i} unless c and c.any?
|
||||
# return closest match if there are multiple
|
||||
c = c.sort_by {|s| s.name.length}.first if c.respond_to? :sort_by
|
||||
c
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Deny deleting the category when there are associated articles.
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ class SharedArticle < ActiveRecord::Base
|
|||
:deposit => deposit,
|
||||
:unit_quantity => unit_quantity,
|
||||
:order_number => number,
|
||||
:article_category => ArticleCategory.find_match(category),
|
||||
# convert to db-compatible-string
|
||||
:shared_updated_on => updated_on.to_formatted_s(:db)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue