Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- update to allow subclassing permalink models

  • Loading branch information...
commit 8a19c9e01eddd0f04a6b2ba8e1c8019c3f904c15 1 parent e44a9ff
@adamsalter adamsalter authored
View
6 lib/permalink_fu.rb
@@ -93,7 +93,7 @@ class << base
class << base
alias_method :define_attribute_methods_without_permalinks, :define_attribute_methods
alias_method :define_attribute_methods, :define_attribute_methods_with_permalinks
- end
+ end unless base.respond_to?(:define_attribute_methods_without_permalinks)
end
def define_attribute_methods_with_permalinks
@@ -158,7 +158,9 @@ def create_permalink_for(attr_names)
private
def should_create_permalink?
- if self.class.permalink_options[:if]
+ if self.class.permalink_field.blank?
+ false
+ elsif self.class.permalink_options[:if]
evaluate_method(self.class.permalink_options[:if])
elsif self.class.permalink_options[:unless]
!evaluate_method(self.class.permalink_options[:unless])
View
31 test/permalink_fu_test.rb
@@ -12,6 +12,37 @@ class PermalinkFuTest < Test::Unit::TestCase
@@extra = { 'some-)()()-ExtRa!/// .data==?> to \/\/test'.freeze => 'some-extra-data-to-test'.freeze }
+ def test_basemodel
+ @m = BaseModel.new
+ assert @m.valid?
+ assert_equal @m.id, nil
+ assert_equal @m.title, nil
+ assert_equal @m.permalink, nil
+ assert_equal @m.extra, nil
+ assert_equal @m.foo, nil
+ end
+
+ def test_set_new_permalink_attributes_on_sub_class
+ @m = ClassModel.new
+ @m.title = 'foo'
+ @m.extra = 'bar'
+ assert @m.valid?
+ assert_equal @m.permalink, 'foo'
+
+ @m = SubClassHasPermalinkModel.new
+ @m.title = 'foo'
+ @m.extra = 'bar'
+ assert @m.valid?
+ assert_equal @m.permalink, 'foo-bar'
+ end
+
+ def test_should_not_inherit_permalink_attributes
+ @m = SubClassNoPermalinkModel.new
+ @m.title = 'foo'
+ assert @m.valid?
+ assert_equal @m.permalink, nil
+ end
+
def test_should_escape_permalinks
@@samples.each do |from, to|
assert_equal to, PermalinkFu.escape(from)
View
15 test/test_helper.rb
@@ -6,7 +6,7 @@
require 'ruby-debug'
Debugger.start
rescue LoadError
- puts "no ruby debugger"
+ # no ruby debugger
end
gem 'activerecord'
@@ -18,7 +18,7 @@ class BaseModel < ActiveRecord::Base
@@columns ||= []
def self.column(name, sql_type = nil, default = nil, null = true)
- columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
end
def self.exists?(*args)
@@ -33,6 +33,17 @@ def self.exists?(*args)
end
+class ClassModel < BaseModel
+ has_permalink :title
+end
+
+class SubClassHasPermalinkModel < ClassModel
+ has_permalink [:title, :extra]
+end
+
+class SubClassNoPermalinkModel < ClassModel
+end
+
class MockModel < BaseModel
def self.exists?(conditions)
if conditions[1] == 'foo' || conditions[1] == 'bar' ||
Please sign in to comment.
Something went wrong with that request. Please try again.