Skip to content

Commit

Permalink
improve validation and remove stupid permalink validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bumann committed Nov 13, 2008
1 parent 5f05161 commit 5baff9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
23 changes: 14 additions & 9 deletions lib/find_by_param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,30 @@ class Post < ActiveRecord::Base
<tt>:field</tt>:: The name of your permalink column. make_permalink first checks if there is a column.
<tt>:prepend_id</tt>:: [true|false] Do you want to prepend the ID to the permalink? for URLs like: posts/123-my-post-title - find_by_param uses the ID column to search.
<tt>:escape</tt>:: [true|false] Do you want to escape the permalink value? (strip chars like öä?&?) - actually you must do that
=end
def make_permalink(options={})
options[:field] ||= "permalink"
options[:param] = options.delete(:with)
options[:param] = options[:with] # :with => :login - but if we have a spcific permalink column we need to set :param to the name of that column
options[:escape] ||= true
options[:prepend_id] ||= false
options[:param_size] ||= 50
options[:validate] ||= true

# validate if there is something we can use as param. you can overwrite the validate_param_is_not_blank method to customize the validation and the error messge.
if !options[:prepend_id] || !options[:validate]
validate :validate_param_is_not_blank
end

if self.column_names.include?(options[:field].to_s)
options[:field_to_encode] = options[:param]
options[:param] = options[:field]

before_validation :save_permalink
validates_uniqueness_of options[:param]
validates_presence_of options[:param]
before_save :save_permalink
end

self.permalink_options = options
extend Railslove::Plugins::FindByParam::SingletonMethods
include Railslove::Plugins::FindByParam::InstanceMethods
rescue
puts "[find_by_param error] database not available"
puts "[find_by_param error] database not available?"
end
end

Expand Down Expand Up @@ -123,7 +124,7 @@ def to_param
def save_permalink
return unless self.class.column_names.include?(permalink_options[:field].to_s)
counter = 0
base_value = escape_and_truncate_for_permalink(read_attribute(permalink_options[:field_to_encode]))
base_value = escape_and_truncate_for_permalink(read_attribute(permalink_options[:with]))
permalink_value = "#{base_value}".downcase

conditions = ["#{self.class.table_name}.#{permalink_options[:field]} = ?", permalink_value]
Expand All @@ -139,6 +140,10 @@ def save_permalink
true
end

def validate_param_is_not_blank
errors.add(permalink_options[:with], "must have at least one non special character (a-z 0-9)") if self.escape( self.send(permalink_options[:with]) ).blank?
end

def escape(value)
"#{value.respond_to?("parameterize") ? value.parameterize : self.class.escape(value)}"
end
Expand Down
18 changes: 12 additions & 6 deletions test/find_by_param_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_default_should_return_id
def test_permalink_should_be_saved
Post.class_eval "make_permalink :with => :title"
post = Post.create(:title=>"hey ho let's go!")
assert_equal post.to_param, "hey-ho-let-s-go"
assert_equal "hey-ho-let-s-go", post.to_param
assert_equal post.permalink, post.to_param
end

Expand All @@ -32,7 +32,7 @@ def test_permalink_should_be_trunkated
def test_permalink_should_be_trunkated_to_custom_size
Post.class_eval "make_permalink :with => :title, :param_size=>10"
post = Post.create(:title=>"thisoneisaveryveryveryveryveryveryverylonglonglonglongtitlethisoneisaveryveryveryveryveryveryverylonglonglonglongtitle")
assert_equal post.to_param, "thisoneisa"
assert_equal "thisoneisa",post.to_param
assert_equal post.permalink, post.to_param
end

Expand All @@ -43,6 +43,12 @@ def test_should_search_field_for_to_param_field
assert_equal user, User.find_by_param!("bumi")
end

def test_should_validate_presence_of_the_field_used_to_create_the_param
User.class_eval "make_permalink :with => :login"
user = User.create(:login=>nil)
assert_equal false, user.valid?
end

def test_to_param_should_perpend_id
Article.class_eval "make_permalink :with => :title, :prepend_id=>true "
article = Article.create(:title=>"hey ho let's go!")
Expand All @@ -53,7 +59,7 @@ def test_should_increment_counter_if_not_unique
Post.class_eval "make_permalink :with => :title"
Post.create(:title=>"my awesome title!")
post = Post.create(:title=>"my awesome title!")
assert_equal post.to_param, "my-awesome-title-1"
assert_equal "my-awesome-title-1", post.to_param
assert_equal post.permalink, post.to_param
end

Expand All @@ -72,11 +78,11 @@ def test_does_not_leak_options
Post.class_eval "make_permalink :with => :title"
User.class_eval "make_permalink :with => :login"
Article.class_eval "make_permalink :with => :title, :prepend_id => true"
assert_equal( {:param => "permalink", :param_size => 50, :field => "permalink", :field_to_encode => :title, :prepend_id => false, :escape => true}, Post.permalink_options)
assert_equal( {:param => "permalink", :param_size => 50, :field => "permalink", :prepend_id => false, :escape => true, :with => :title, :validate => true}, Post.permalink_options)

assert_equal( {:param => :login, :param_size => 50, :field => "permalink", :prepend_id => false, :escape => true}, User.permalink_options)
assert_equal( {:param => :login, :param_size => 50, :field => "permalink", :prepend_id => false, :escape => true, :with => :login, :validate => true}, User.permalink_options)

assert_equal( {:param => :title, :param_size => 50, :field => "permalink", :prepend_id => true, :escape => true}, Article.permalink_options)
assert_equal( {:param => :title, :param_size => 50, :field => "permalink", :prepend_id => true, :escape => true, :with => :title, :validate => true}, Article.permalink_options)
end

end

0 comments on commit 5baff9f

Please sign in to comment.