Skip to content

Commit

Permalink
adding validations for blog paths
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Oct 22, 2013
1 parent bab3e44 commit 2735adb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ source 'http://rubygems.org'

gemspec

gem 'comfortable_mexican_sofa',
:github => 'comfy/comfortable-mexican-sofa',
:branch => '1.10'

group :development do
gem 'awesome_print'
gem 'better_errors'
Expand Down
11 changes: 10 additions & 1 deletion app/models/blog/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ class Blog::Blog < ActiveRecord::Base
validates :identifier,
:format => { :with => /\A\w[a-z0-9_-]*\z/i }
validates :path,
:uniqueness => { :scope => :site_id },
:format => { :with => /\A\w[a-z0-9_-]*\z/i },
:if => 'path.present?'
:presence => true,
:if => 'restricted_path?'

protected

def restricted_path?
(self.class.count > 1 && self.persisted?) ||
(self.class.count >= 1 && self.new_record?)
end

end
2 changes: 1 addition & 1 deletion comfy_blog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']

# s.add_dependency 'comfortable_mexican_sofa', '~> 1.9.0'
s.add_dependency 'comfortable_mexican_sofa', '>= 1.10.0'
end
28 changes: 26 additions & 2 deletions test/models/blog/blog_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,38 @@ def test_fixtures_validity
def test_validation
blog = Blog::Blog.new
assert blog.invalid?
assert_errors_on blog, :site_id, :label, :identifier
assert_errors_on blog, :site_id, :label, :identifier, :path
end

def test_validation_path_presence
blog_a = blog_blogs(:default)
refute blog_a.path.present?

blog_b = cms_sites(:default).blogs.new(
:label => 'Test Blog A',
:identifier => 'test-blog-a'
)
assert blog_b.invalid?
assert_errors_on blog_b, :path

blog_a.update_column(:path, 'blog-a')
assert blog_b.invalid?
assert_errors_on blog_b, :path

blog_b.path = 'blog-a'
assert blog_b.invalid?
assert_errors_on blog_b, :path

blog_b.path = 'blog-b'
assert blog_b.valid?
end

def test_creation
assert_difference 'Blog::Blog.count' do
cms_sites(:default).blogs.create(
:label => 'Test Blog',
:identifier => 'test-blog'
:identifier => 'test-blog',
:path => 'test-blog'
)
end
end
Expand Down

0 comments on commit 2735adb

Please sign in to comment.