djanowski-permalink_fu 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ module PermalinkFu
2
+ module Spec
3
+ module Matchers
4
+ class PermalinkTo
5
+ def initialize(permalink, to_param = nil)
6
+ @expected_permalink = permalink
7
+ @expected_to_param = to_param
8
+ end
9
+
10
+ def matches?(target)
11
+ @target = target
12
+ @permalink, @to_param = target.permalink, target.to_param
13
+ @permalink == @expected_permalink && @to_param == expected_to_param
14
+ end
15
+
16
+ def failure_message
17
+ message = []
18
+
19
+ message << "to permalink to #{@expected_permalink.inspect} (got #{@permalink.inspect})" if @permalink != @expected_permalink
20
+ message << "to generate a URL like #{expected_to_param.inspect} (got #{@to_param.inspect})" if @to_param != expected_to_param
21
+
22
+ "expected #{message.join(' and ')}"
23
+ end
24
+
25
+ private
26
+ def expected_to_param
27
+ @expected_to_param.blank? ? [@target.id, @permalink].delete_if(&:blank?).join('-') : @expected_to_param
28
+ end
29
+ end
30
+
31
+ # Provides a matcher for nicer specs:
32
+ #
33
+ # describe Article do
34
+ # it "has a permalink" do
35
+ # Article.create(:title => 'Lorem ipsum').should permalink_to('lorem-ipsum')
36
+ # end
37
+ # end
38
+ def permalink_to(*args)
39
+ PermalinkTo.new(*args)
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/permalink_fu.rb CHANGED
@@ -47,8 +47,9 @@ module PermalinkFu
47
47
  # has_permalink :title, :as => :slug, :scope => :blog_id
48
48
  # end
49
49
  #
50
- def has_permalink(attr_names = [], options = {})
51
- attr_names = :to_s if attr_names.blank?
50
+ def has_permalink(*args)
51
+ options = args.extract_options!
52
+ attr_names = args.first || :to_s
52
53
  self.permalink_attributes = Array(attr_names)
53
54
  self.permalink_field = (options.delete(:as) || 'permalink').to_s
54
55
  self.permalink_options = options
@@ -75,7 +76,9 @@ module PermalinkFu
75
76
 
76
77
  module ToParamWithID
77
78
  def to_param
78
- "#{id}-#{send(self.class.permalink_field)}"
79
+ permalink = send(self.class.permalink_field)
80
+ return super if new_record? || permalink.blank?
81
+ "#{id}-#{permalink}"
79
82
  end
80
83
  end
81
84
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: djanowski-permalink_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Janowski
@@ -22,6 +22,7 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README.textile
24
24
  files:
25
+ - lib/permalink_fu/spec/matchers.rb
25
26
  - lib/permalink_fu.rb
26
27
  - init.rb
27
28
  - README.markdown