normalize article and category attributes (closes foodcoops#294)

This commit is contained in:
wvengen 2014-06-06 16:41:01 +02:00
parent 7c2ecd8658
commit a43020463d
5 changed files with 18 additions and 0 deletions

View file

@ -41,6 +41,7 @@ gem 'resque'
gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb
gem 'protected_attributes' gem 'protected_attributes'
gem 'ruby-units' gem 'ruby-units'
gem 'attribute_normalizer'
# we use the git version of acts_as_versioned, and need to include it in this Gemfile # we use the git version of acts_as_versioned, and need to include it in this Gemfile
gem 'acts_as_versioned', git: 'git://github.com/technoweenie/acts_as_versioned.git' gem 'acts_as_versioned', git: 'git://github.com/technoweenie/acts_as_versioned.git'

View file

@ -80,6 +80,7 @@ GEM
acts_as_tree (1.6.1) acts_as_tree (1.6.1)
activerecord (>= 3.0.0) activerecord (>= 3.0.0)
arel (4.0.2) arel (4.0.2)
attribute_normalizer (1.1.0)
better_errors (1.1.0) better_errors (1.1.0)
coderay (>= 1.0.0) coderay (>= 1.0.0)
erubis (>= 2.6.6) erubis (>= 2.6.6)
@ -398,6 +399,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
acts_as_tree acts_as_tree
acts_as_versioned! acts_as_versioned!
attribute_normalizer
better_errors better_errors
binding_of_caller binding_of_caller
bootstrap-datepicker-rails bootstrap-datepicker-rails

View file

@ -45,6 +45,8 @@ class Article < ActiveRecord::Base
# Replace numeric seperator with database format # Replace numeric seperator with database format
localize_input_of :price, :tax, :deposit localize_input_of :price, :tax, :deposit
# Get rid of unwanted whitespace. {Unit#new} may even bork on whitespace.
normalize_attributes :name, :unit, :note, :manufacturer, :origin, :order_number
scope :undeleted, -> { where(deleted_at: nil) } scope :undeleted, -> { where(deleted_at: nil) }
scope :available, -> { undeleted.where(availability: true) } scope :available, -> { undeleted.where(availability: true) }

View file

@ -10,6 +10,8 @@ class ArticleCategory < ActiveRecord::Base
# @return [Array<Article>] Articles with this category. # @return [Array<Article>] Articles with this category.
has_many :articles has_many :articles
normalize_attributes :name, :description
validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 } validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 }
before_destroy :check_for_associated_articles before_destroy :check_for_associated_articles

View file

@ -0,0 +1,11 @@
# config/initializers/attribute_normalizer.rb
# @see https://github.com/mdeering/attribute_normalizer
AttributeNormalizer.configure do |config|
# The default normalizers if no :with option or block is given is to apply the :strip and :blank normalizers (in that order).
# You can change this if you would like as follows:
# config.default_normalizers = :strip, :blank
# You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
# the default normalizers for each attribute (e.g. config.default_normalizers)
# config.default_attributes = :name, :title
end