Merge pull request #3264 from evazion/fix-3263
Fix #3263: Remove leading whitespace on pool names
This commit is contained in:
@@ -36,7 +36,7 @@ class PoolsController < ApplicationController
|
|||||||
|
|
||||||
def create
|
def create
|
||||||
@pool = Pool.create(params[:pool])
|
@pool = Pool.create(params[:pool])
|
||||||
flash[:notice] = "Pool created"
|
flash[:notice] = @pool.valid? ? "Pool created" : @pool.errors.full_messages.join("; ")
|
||||||
respond_with(@pool)
|
respond_with(@pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ require 'ostruct'
|
|||||||
class Pool < ApplicationRecord
|
class Pool < ApplicationRecord
|
||||||
class RevertError < Exception ; end
|
class RevertError < Exception ; end
|
||||||
|
|
||||||
validates_uniqueness_of :name, :case_sensitive => false
|
validates_uniqueness_of :name, :case_sensitive => false, :if => :name_changed?
|
||||||
validates_format_of :name, :with => /\A[^,]+\Z/, :message => "cannot have commas"
|
validate :validate_name, :if => :name_changed?
|
||||||
validates_inclusion_of :category, :in => %w(series collection)
|
validates_inclusion_of :category, :in => %w(series collection)
|
||||||
validate :updater_can_change_category
|
validate :updater_can_change_category
|
||||||
validate :name_does_not_conflict_with_metatags
|
|
||||||
validate :updater_can_remove_posts
|
validate :updater_can_remove_posts
|
||||||
belongs_to :creator, :class_name => "User"
|
belongs_to :creator, :class_name => "User"
|
||||||
belongs_to :updater, :class_name => "User"
|
belongs_to :updater, :class_name => "User"
|
||||||
@@ -15,7 +14,6 @@ class Pool < ApplicationRecord
|
|||||||
before_validation :normalize_name
|
before_validation :normalize_name
|
||||||
before_validation :initialize_is_active, :on => :create
|
before_validation :initialize_is_active, :on => :create
|
||||||
before_validation :initialize_creator, :on => :create
|
before_validation :initialize_creator, :on => :create
|
||||||
before_validation :strip_name
|
|
||||||
after_save :update_category_pseudo_tags_for_posts_async
|
after_save :update_category_pseudo_tags_for_posts_async
|
||||||
after_save :create_version
|
after_save :create_version
|
||||||
after_create :synchronize!
|
after_create :synchronize!
|
||||||
@@ -119,7 +117,7 @@ class Pool < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.normalize_name(name)
|
def self.normalize_name(name)
|
||||||
name.gsub(/\s+/, "_")
|
name.gsub(/[_[:space:]]+/, "_").gsub(/\A_|_\z/, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.normalize_name_for_search(name)
|
def self.normalize_name_for_search(name)
|
||||||
@@ -138,7 +136,7 @@ class Pool < ApplicationRecord
|
|||||||
if name =~ /^\d+$/
|
if name =~ /^\d+$/
|
||||||
where("id = ?", name.to_i).first
|
where("id = ?", name.to_i).first
|
||||||
elsif name
|
elsif name
|
||||||
where("lower(name) = ?", normalize_name(name).mb_chars.downcase).first
|
where("lower(name) = ?", normalize_name_for_search(name)).first
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
@@ -356,10 +354,6 @@ class Pool < ApplicationRecord
|
|||||||
super + [:creator_name]
|
super + [:creator_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def strip_name
|
|
||||||
self.name = name.to_s.strip
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_category_pseudo_tags_for_posts_async
|
def update_category_pseudo_tags_for_posts_async
|
||||||
if category_changed?
|
if category_changed?
|
||||||
delay(:queue => "default").update_category_pseudo_tags_for_posts
|
delay(:queue => "default").update_category_pseudo_tags_for_posts
|
||||||
@@ -387,12 +381,18 @@ class Pool < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def name_does_not_conflict_with_metatags
|
def validate_name
|
||||||
if %w(any none series collection).include?(name.downcase.tr(" ", "_"))
|
case name
|
||||||
errors[:base] << "Pools cannot have the following names: any, none, series, collection"
|
when /\A(any|none|series|collection)\z/i
|
||||||
false
|
errors[:name] << "cannot be any of the following names: any, none, series, collection"
|
||||||
else
|
when /,/
|
||||||
true
|
errors[:name] << "cannot contain commas"
|
||||||
|
when /\*/
|
||||||
|
errors[:name] << "cannot contain asterisks"
|
||||||
|
when ""
|
||||||
|
errors[:name] << "cannot be blank"
|
||||||
|
when /\A[0-9]+\z/
|
||||||
|
errors[:name] << "cannot contain only digits"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory(:pool) do
|
factory(:pool) do
|
||||||
name {(rand(1_000_000) + 100).to_s}
|
name {"pool_" + (rand(1_000_000) + 100).to_s}
|
||||||
association :creator, :factory => :user
|
association :creator, :factory => :user
|
||||||
description {FFaker::Lorem.sentences.join(" ")}
|
description {FFaker::Lorem.sentences.join(" ")}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -249,7 +249,10 @@ class PoolTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "normalize its name" do
|
should "normalize its name" do
|
||||||
@pool.update_attributes(:name => "A B")
|
@pool.update(:name => " A B ")
|
||||||
|
assert_equal("A_B", @pool.name)
|
||||||
|
|
||||||
|
@pool.update(:name => "__A__B__")
|
||||||
assert_equal("A_B", @pool.name)
|
assert_equal("A_B", @pool.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -257,6 +260,18 @@ class PoolTest < ActiveSupport::TestCase
|
|||||||
@pool.update_attributes(:post_ids => " 1 2 ")
|
@pool.update_attributes(:post_ids => " 1 2 ")
|
||||||
assert_equal("1 2", @pool.post_ids)
|
assert_equal("1 2", @pool.post_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when validating names" do
|
||||||
|
should_not allow_value("foo,bar").for(:name)
|
||||||
|
should_not allow_value("foo*bar").for(:name)
|
||||||
|
should_not allow_value("123").for(:name)
|
||||||
|
should_not allow_value("___").for(:name)
|
||||||
|
should_not allow_value(" ").for(:name)
|
||||||
|
|
||||||
|
%w[any none series collection].each do |type|
|
||||||
|
should_not allow_value(type).for(:name)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "An existing pool" do
|
context "An existing pool" do
|
||||||
|
|||||||
Reference in New Issue
Block a user