Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
language: ruby
sudo: false
script: bundle exec rspec
rvm:
- "2.0.0"
- "2.2.1"
script: bundle exec rspec
- "2.2.2"
- "2.3.1"
gemfile:
- Gemfile
- gemfiles/rails-4.2.gemfile
matrix:
exclude:
- rvm: "2.0.0"
gemfile: Gemfile
6 changes: 6 additions & 0 deletions gemfiles/rails-4.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

gemspec path: '..'

gem 'rails', '~> 4.2'
gem 'rack', '< 2.0'
7 changes: 4 additions & 3 deletions lib/concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def acts_as_permalink(options={})
attr_readonly self.acts_as_permalink_config.to
end

allow_blank = self.acts_as_permalink_config.allow_blank
if self.acts_as_permalink_config.scope
validates self.acts_as_permalink_config.to, uniqueness: {scope: self.acts_as_permalink_config.scope}
validates self.acts_as_permalink_config.to, uniqueness: {scope: self.acts_as_permalink_config.scope, allow_blank: allow_blank}
else
validates self.acts_as_permalink_config.to, uniqueness: true
validates self.acts_as_permalink_config.to, uniqueness: {allow_blank: allow_blank}
end


include Acts::Permalink::InstanceMethods
end
end
Expand All @@ -38,6 +38,7 @@ def build_permalink
config = self.class.base_class.acts_as_permalink_config

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

Expand Down
3 changes: 2 additions & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Acts
module Permalink
class Config
attr_reader :to, :from, :separator, :max_length, :scope, :allow_update
attr_reader :to, :from, :separator, :max_length, :scope, :allow_update, :allow_blank

def initialize(options={})
@config = options.with_indifferent_access
Expand All @@ -14,6 +14,7 @@ def initialize(options={})
@max_length = @config[:max_length].to_i rescue 0
@max_length = 60 unless @max_length > 0
@allow_update = !!@config[:allow_update]
@allow_blank = !!@config[:allow_blank]
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/acts_as_permalink_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ class LongThing < ActiveRecord::Base

end

describe "column that allows blank" do
class BlankThing < ActiveRecord::Base
acts_as_permalink max_length: 100, allow_blank: true
end

it "allows multiple blank titles" do
expect(BlankThing.create!(title: "").permalink).to be_nil
expect(BlankThing.create!(title: "").permalink).to be_nil
end

it "provides permalink" do
expect(BlankThing.create!(title: "").permalink).to be_nil
expect(BlankThing.create!(title: "My New Thing").permalink).to eq("my-new-think")
end

end

describe "single table inheritance" do
class Thing < ActiveRecord::Base
acts_as_permalink
Expand Down
1 change: 1 addition & 0 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
expect(config.max_length).to eq(60)
expect(config.scope).to be_nil
expect(config.allow_update).to be_falsy
expect(config.allow_blank).to be_falsy
end

it "allows the underscore property" do
Expand Down