From a43020463d813ccc2a0121d20e1a37cc16f22c72 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 6 Jun 2014 16:41:01 +0200 Subject: [PATCH] normalize article and category attributes (closes foodcoops#294) --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/article.rb | 2 ++ app/models/article_category.rb | 2 ++ config/initializers/attribute_normalizer.rb | 11 +++++++++++ 5 files changed, 18 insertions(+) create mode 100644 config/initializers/attribute_normalizer.rb diff --git a/Gemfile b/Gemfile index b40a768f..faabaec9 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ gem 'resque' gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb gem 'protected_attributes' gem 'ruby-units' +gem 'attribute_normalizer' # 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' diff --git a/Gemfile.lock b/Gemfile.lock index 1085c7da..35e64f1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,7 @@ GEM acts_as_tree (1.6.1) activerecord (>= 3.0.0) arel (4.0.2) + attribute_normalizer (1.1.0) better_errors (1.1.0) coderay (>= 1.0.0) erubis (>= 2.6.6) @@ -398,6 +399,7 @@ PLATFORMS DEPENDENCIES acts_as_tree acts_as_versioned! + attribute_normalizer better_errors binding_of_caller bootstrap-datepicker-rails diff --git a/app/models/article.rb b/app/models/article.rb index b9cd5879..084fb26e 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -45,6 +45,8 @@ class Article < ActiveRecord::Base # Replace numeric seperator with database format 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 :available, -> { undeleted.where(availability: true) } diff --git a/app/models/article_category.rb b/app/models/article_category.rb index cc99ae35..ec1e3614 100644 --- a/app/models/article_category.rb +++ b/app/models/article_category.rb @@ -10,6 +10,8 @@ class ArticleCategory < ActiveRecord::Base # @return [Array
] Articles with this category. has_many :articles + normalize_attributes :name, :description + validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 } before_destroy :check_for_associated_articles diff --git a/config/initializers/attribute_normalizer.rb b/config/initializers/attribute_normalizer.rb new file mode 100644 index 00000000..c76bfcc1 --- /dev/null +++ b/config/initializers/attribute_normalizer.rb @@ -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