[noosfero/noosfero][next] 3 commits: Refactor profile identifier validation to put the code in a single place
Leandro Nunes
gitlab at gitlab.com
Wed Apr 15 16:25:55 BRT 2015
Leandro Nunes pushed to next at Noosfero / noosfero
Commits:
498b0dad by Victor Costa at 2015-04-15T15:57:56Z
Refactor profile identifier validation to put the code in a single place
- - - - -
16d3aa02 by Victor Costa at 2015-04-15T16:09:29Z
New config to define an exclusion pattern for profile identifier
- - - - -
5225e5d4 by Leandro Nunes at 2015-04-15T19:25:37Z
Merge branch 'exclude_identifier_pattern' into 'next'
Add a config option that define a exclusion pattern for profile identifiers
See merge request !548
- - - - -
3 changed files:
- app/models/profile.rb
- config/noosfero.yml.dist
- test/unit/profile_test.rb
Changes:
=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -317,16 +317,25 @@ class Profile < ActiveRecord::Base
@top_level_articles ||= Article.top_level_for(self)
end
- def self.is_available?(identifier, environment)
- !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil?
+ def self.is_available?(identifier, environment, profile_id=nil)
+ return false unless identifier =~ IDENTIFIER_FORMAT &&
+ !RESERVED_IDENTIFIERS.include?(identifier) &&
+ (NOOSFERO_CONF['exclude_profile_identifier_pattern'].blank? || identifier !~ /#{NOOSFERO_CONF['exclude_profile_identifier_pattern']}/)
+ return true if environment.nil?
+
+ profiles = environment.profiles.where(:identifier => identifier)
+ profiles = profiles.where(['id != ?', profile_id]) if profile_id.present?
+ !profiles.exists?
end
validates_presence_of :identifier, :name
- validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? }
- validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
- validates_uniqueness_of :identifier, :scope => :environment_id
validates_length_of :nickname, :maximum => 16, :allow_nil => true
validate :valid_template
+ validate :valid_identifier
+
+ def valid_identifier
+ errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id)
+ end
def valid_template
if template_id.present? && template && !template.is_template
=====================================
config/noosfero.yml.dist
=====================================
--- a/config/noosfero.yml.dist
+++ b/config/noosfero.yml.dist
@@ -10,6 +10,7 @@ development:
exception_recipients: [admin at example.com]
max_upload_size: 5MB
hours_until_user_activation_check: 72
+ exclude_profile_identifier_pattern: index(\..*)?|home(\..*)?
test:
=====================================
test/unit/profile_test.rb
=====================================
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -1613,6 +1613,16 @@ class ProfileTest < ActiveSupport::TestCase
assert_equal false, Profile.is_available?('identifier-test', Environment.default)
end
+ should 'not be available if identifier match with custom exclusion pattern' do
+ NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*')
+ assert_equal false, Profile.is_available?('identifier-test', Environment.default)
+ end
+
+ should 'be available if identifier do not match with custom exclusion pattern' do
+ NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*')
+ assert_equal false, Profile.is_available?('test-identifier', Environment.default)
+ end
+
should 'not have long descriptions' do
long_description = 'a' * 600
profile = Profile.new
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/6edafc75557df3ba3db072d36f8a1ab20886635c...5225e5d44e9b6d7866ddd47845d81236b301e696
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150415/e021cd24/attachment-0001.html>
More information about the Noosfero-dev
mailing list