Skip to content

Commit 5d08881

Browse files
committed
Allow multiple blanks for handles
1 parent e0f5b0b commit 5d08881

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

lib/concern.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ def acts_as_permalink(options={})
1313
attr_readonly self.acts_as_permalink_config.to
1414
end
1515

16+
allow_blank = self.acts_as_permalink_config.allow_blank
1617
if self.acts_as_permalink_config.scope
17-
validates self.acts_as_permalink_config.to, uniqueness: {scope: self.acts_as_permalink_config.scope}
18+
validates self.acts_as_permalink_config.to, uniqueness: {scope: self.acts_as_permalink_config.scope, allow_blank: allow_blank}
1819
else
19-
validates self.acts_as_permalink_config.to, uniqueness: true
20+
validates self.acts_as_permalink_config.to, uniqueness: {allow_blank: allow_blank}
2021
end
2122

22-
2323
include Acts::Permalink::InstanceMethods
2424
end
2525
end
@@ -38,6 +38,7 @@ def build_permalink
3838
config = self.class.base_class.acts_as_permalink_config
3939

4040
text = self.public_send(config.from)
41+
return nil if text.blank? && config.allow_blank
4142
text = [self.class.base_class.to_s, rand(10000)].join(config.separator) if text.blank?
4243
text = Acts::Permalink::Conversion.convert(text, separator: config.separator, max_length: config.max_length)
4344

lib/config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Acts
22
module Permalink
33
class Config
4-
attr_reader :to, :from, :separator, :max_length, :scope, :allow_update
4+
attr_reader :to, :from, :separator, :max_length, :scope, :allow_update, :allow_blank
55

66
def initialize(options={})
77
@config = options.with_indifferent_access
@@ -14,6 +14,7 @@ def initialize(options={})
1414
@max_length = @config[:max_length].to_i rescue 0
1515
@max_length = 60 unless @max_length > 0
1616
@allow_update = !!@config[:allow_update]
17+
@allow_blank = !!@config[:allow_blank]
1718
end
1819
end
1920
end

spec/lib/acts_as_permalink_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ class LongThing < ActiveRecord::Base
8383

8484
end
8585

86+
describe "column that allows blank" do
87+
class LongThing < ActiveRecord::Base
88+
acts_as_permalink max_length: 100, allow_blank: true
89+
end
90+
91+
it "allows multiple blank titles" do
92+
expect(LongThing.create!(title: "").permalink).to be_nil
93+
expect(LongThing.create!(title: "").permalink).to be_nil
94+
end
95+
96+
it "provides permalink" do
97+
expect(LongThing.create!(title: "").permalink).to be_nil
98+
expect(LongThing.create!(title: "My New Thing").permalink).to eq("my-new-think")
99+
end
100+
101+
end
102+
86103
describe "single table inheritance" do
87104
class Thing < ActiveRecord::Base
88105
acts_as_permalink

spec/lib/config_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
expect(config.max_length).to eq(60)
1111
expect(config.scope).to be_nil
1212
expect(config.allow_update).to be_falsy
13+
expect(config.allow_blank).to be_falsy
1314
end
1415

1516
it "allows the underscore property" do

0 commit comments

Comments
 (0)