Skip to content

Commit

Permalink
- update to allow subclassing permalink models
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsalter committed Aug 27, 2009
1 parent e44a9ff commit 8a19c9e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/permalink_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
31 changes: 31 additions & 0 deletions test/permalink_fu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'ruby-debug'
Debugger.start
rescue LoadError
puts "no ruby debugger"
# no ruby debugger
end

gem 'activerecord'
Expand All @@ -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)
Expand All @@ -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' ||
Expand Down

0 comments on commit 8a19c9e

Please sign in to comment.