diff --git a/app/models/community.rb b/app/models/community.rb index b3b19af..ba6936c 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -1,2 +1,8 @@ class Community < Profile + + def name=(value) + super(value) + self.identifier = value.to_slug + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index 0700105..927c002 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -19,7 +19,7 @@ class Profile < ActiveRecord::Base acts_as_searchable :fields => [ :name, :identifier ] # Valid identifiers must match this format. - IDENTIFIER_FORMAT = /^[a-z][a-z0-9_]*[a-z0-9]$/ + IDENTIFIER_FORMAT = /^[a-z][a-z0-9]+([_-][a-z0-9]+)*$/ # These names cannot be used as identifiers for Profiles RESERVED_IDENTIFIERS = %w[ diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 930a1b1..987adaf 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -6,4 +6,10 @@ class CommunityTest < Test::Unit::TestCase assert_kind_of Profile, Community.new end + should 'convert name into identifier' do + c = Community.new(:name =>'My shiny new Community') + assert_equal 'My shiny new Community', c.name + assert_equal 'my-shiny-new-community', c.identifier + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index e66088e..91eed4a 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -27,6 +27,10 @@ class ProfileTest < Test::Unit::TestCase p.identifier = 'right_format' p.valid? assert ! p.errors.invalid?(:identifier) + + p.identifier = 'identifier-with-dashes' + p.valid? + assert ! p.errors.invalid?(:identifier), 'Profile should accept identifier with dashes' end def test_has_domains -- libgit2 0.21.2