vidibus-permalink 0.0.4 → 0.1.0

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.
@@ -1,38 +1,31 @@
1
- = Vidibus::Permalink
1
+ # Vidibus::Permalink
2
2
 
3
3
  This gem allows changeable permalinks. That may be an oxymoron, but it's really useful from a SEO perspective.
4
4
 
5
- It is part of the open source SOA framework Vidibus: http://vidibus.org
5
+ This gem is part of [Vidibus](http://vidibus.org), an open source toolset for building distributed (video) applications.
6
6
 
7
-
8
- == Installation
7
+ ## Installation
9
8
 
10
9
  Add the dependency to the Gemfile of your application:
11
10
 
11
+ ```
12
12
  gem "vidibus-permalink"
13
+ ```
13
14
 
14
15
  Then call bundle install on your console.
15
16
 
17
+ ## TODO
16
18
 
17
- == Usage
18
-
19
- TODO: describe
20
-
21
-
22
- == TODO
19
+ * Add controller extension for automatic dispatching.
20
+ * Limit length of permalinks.
21
+ * Refactor codebase so that incrementation is not limited to Permalink class.
23
22
 
24
- * Add controller extension for automatic dispatching
25
- * Limit length of permalinks
26
- * Refactor codebase so that incrementation is not limited to Permalink class
27
-
28
-
29
- == Ideas (for a separate gem)
23
+ ## Ideas (for a separate gem)
30
24
 
31
25
  * Catch 404s and store invalid routes.
32
26
  * Make invalid routes assignable from a web interface.
33
27
  * Try to suggest a matching Linkable by valid parts of the request path.
34
28
 
35
-
36
- == Copyright
29
+ ## Copyright
37
30
 
38
31
  Copyright (c) 2010 Andre Pankratz. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,35 +1,26 @@
1
- require "rubygems"
2
- require "rake"
3
- require "rake/rdoctask"
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ require "bundler"
4
+ require "rdoc/task"
4
5
  require "rspec"
5
6
  require "rspec/core/rake_task"
6
7
 
7
- begin
8
- require "jeweler"
9
- Jeweler::Tasks.new do |gem|
10
- gem.name = "vidibus-permalink"
11
- gem.summary = %Q{Permalink handling}
12
- gem.description = %Q{Allows changeable permalinks (good for SEO).}
13
- gem.email = "andre@vidibus.com"
14
- gem.homepage = "http://github.com/vidibus/vidibus-permalink"
15
- gem.authors = ["Andre Pankratz"]
16
- end
17
- Jeweler::GemcutterTasks.new
18
- rescue LoadError
19
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
- end
8
+ require "vidibus/permalink/version"
21
9
 
22
- Rspec::Core::RakeTask.new(:rcov) do |t|
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ RSpec::Core::RakeTask.new(:rcov) do |t|
23
13
  t.pattern = "spec/**/*_spec.rb"
24
14
  t.rcov = true
25
15
  t.rcov_opts = ["--exclude", "^spec,/gems/"]
26
16
  end
27
17
 
28
18
  Rake::RDocTask.new do |rdoc|
29
- version = File.exist?("VERSION") ? File.read("VERSION") : ""
30
19
  rdoc.rdoc_dir = "rdoc"
31
- rdoc.title = "vidibus-permalink #{version}"
20
+ rdoc.title = "vidibus-permalink #{Vidibus::Permalink::VERSION}"
32
21
  rdoc.rdoc_files.include("README*")
33
22
  rdoc.rdoc_files.include("lib/**/*.rb")
34
23
  rdoc.options << "--charset=utf-8"
35
24
  end
25
+
26
+ task :default => :rcov
@@ -7,6 +7,7 @@ class Permalink
7
7
  field :value
8
8
  field :linkable_class
9
9
  field :linkable_uuid
10
+ field :scope, :type => Array
10
11
  field :_current, :type => Boolean, :default => true
11
12
 
12
13
  before_save :set_current
@@ -49,6 +50,14 @@ class Permalink
49
50
  string
50
51
  end
51
52
 
53
+ def scope=(scope)
54
+ if array = scope
55
+ array = self.class.scope_list(scope)
56
+ self.write_attribute(:scope, array)
57
+ end
58
+ array
59
+ end
60
+
52
61
  # Returns true if this permalink is the current one
53
62
  # of the assigned linkable.
54
63
  def current?
@@ -67,7 +76,6 @@ class Permalink
67
76
  end
68
77
 
69
78
  class << self
70
-
71
79
  # Scope method for finding Permalinks for given object.
72
80
  def for_linkable(object)
73
81
  where(:linkable_uuid => object.uuid)
@@ -79,9 +87,14 @@ class Permalink
79
87
  where(:value => sanitize(value))
80
88
  end
81
89
 
90
+ def for_scope(scope)
91
+ return all unless scope
92
+ all_in(:scope => scope_list(scope))
93
+ end
94
+
82
95
  # Returns a dispatcher object for given path.
83
- def dispatch(path)
84
- Vidibus::Permalink::Dispatcher.new(path)
96
+ def dispatch(path, options = {})
97
+ Vidibus::Permalink::Dispatcher.new(path, options)
85
98
  end
86
99
 
87
100
  # Sanitizes string: Remove stopwords and format as permalink.
@@ -90,6 +103,11 @@ class Permalink
90
103
  return if string.blank?
91
104
  remove_stopwords(string).permalink
92
105
  end
106
+
107
+ def scope_list(scope)
108
+ return [] unless scope
109
+ scope.inject([]) { |array, (key, value)| array << "#{key}:#{value}"; array}
110
+ end
93
111
  end
94
112
 
95
113
  protected
@@ -6,8 +6,9 @@ module Vidibus
6
6
 
7
7
  # Initialize a new Dispatcher instance.
8
8
  # Provide an absolute +path+ to be dispatched.
9
- def initialize(path)
9
+ def initialize(path, options = {})
10
10
  self.path = path
11
+ @scope = options[:scope]
11
12
  end
12
13
 
13
14
  # Returns the path to dispatch.
@@ -58,9 +59,8 @@ module Vidibus
58
59
 
59
60
  private
60
61
 
61
- # TODO: Allow scopes
62
62
  def resolve_path
63
- results = ::Permalink.any_in(:value => parts)
63
+ results = ::Permalink.for_scope(@scope).any_in(:value => parts)
64
64
  links = Array.new(parts.length)
65
65
  done = {}
66
66
  for result in results
@@ -20,7 +20,7 @@ module Vidibus
20
20
 
21
21
  # Sets permalink attributes.
22
22
  # Usage:
23
- # permalink :some, :fields
23
+ # permalink :some, :fields, :scope => {:realm => "rugby"}
24
24
  def permalink(*args)
25
25
  options = args.extract_options!
26
26
  class_eval <<-EOS
@@ -50,6 +50,16 @@ module Vidibus
50
50
  permalink_repository.for_linkable(self).asc(:updated_at) if permalink_repository
51
51
  end
52
52
 
53
+ # Returns permalink scope.
54
+ def permalink_scope
55
+ @permalink_scope ||= self.class.permalink_options[:scope]
56
+ end
57
+
58
+ def find_or_initialize(value, scope)
59
+ permalink_repository.for_linkable(self).for_value(value).for_scope(scope).first ||
60
+ permalink_repository.new(:value => value, :scope => scope, :linkable => self)
61
+ end
62
+
53
63
  private
54
64
 
55
65
  # Initializes a new permalink object and sets permalink attribute.
@@ -69,7 +79,7 @@ module Vidibus
69
79
  return unless permalink.blank? or changed
70
80
  value = values.join(" ")
71
81
  if permalink_repository
72
- @permalink_object = permalink_repository.for_linkable(self).for_value(value).first || permalink_repository.new(:value => value, :linkable => self)
82
+ @permalink_object = find_or_initialize(value, permalink_scope)
73
83
  self.permalink = @permalink_object.value
74
84
  else
75
85
  self.permalink = ::Permalink.sanitize(value)
@@ -88,4 +98,4 @@ module Vidibus
88
98
  end
89
99
  end
90
100
  end
91
- end
101
+ end
@@ -0,0 +1,5 @@
1
+ module Vidibus
2
+ module Permalink
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -1,2 +1,2 @@
1
- require "permalink/mongoid"
2
- require "permalink/dispatcher"
1
+ require "vidibus/permalink/mongoid"
2
+ require "vidibus/permalink/dispatcher"
@@ -1,11 +1,10 @@
1
- require "rails"
1
+ require "active_support"
2
2
  require "mongoid"
3
3
  require "vidibus-core_extensions"
4
4
  require "vidibus-uuid"
5
5
  require "vidibus-words"
6
-
7
- $:.unshift(File.join(File.dirname(__FILE__), "vidibus"))
8
- require "permalink"
6
+
7
+ require "vidibus/permalink"
9
8
 
10
9
  if defined?(Rails)
11
10
  module Vidibus::Permalink
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-permalink
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Pankratz
@@ -15,12 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-09 00:00:00 +02:00
18
+ date: 2011-09-05 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
+ name: active_support
22
23
  prerelease: false
23
- type: :runtime
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
@@ -31,26 +31,25 @@ dependencies:
31
31
  - 3
32
32
  - 0
33
33
  version: "3.0"
34
- name: rails
34
+ type: :runtime
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
+ name: mongoid
37
38
  prerelease: false
38
- type: :runtime
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 3
44
+ hash: 7
45
45
  segments:
46
46
  - 2
47
- - 0
48
- version: "2.0"
49
- name: mongoid
47
+ version: "2"
48
+ type: :runtime
50
49
  version_requirements: *id002
51
50
  - !ruby/object:Gem::Dependency
51
+ name: vidibus-core_extensions
52
52
  prerelease: false
53
- type: :runtime
54
53
  requirement: &id003 !ruby/object:Gem::Requirement
55
54
  none: false
56
55
  requirements:
@@ -60,11 +59,11 @@ dependencies:
60
59
  segments:
61
60
  - 0
62
61
  version: "0"
63
- name: vidibus-core_extensions
62
+ type: :runtime
64
63
  version_requirements: *id003
65
64
  - !ruby/object:Gem::Dependency
65
+ name: vidibus-uuid
66
66
  prerelease: false
67
- type: :runtime
68
67
  requirement: &id004 !ruby/object:Gem::Requirement
69
68
  none: false
70
69
  requirements:
@@ -74,11 +73,11 @@ dependencies:
74
73
  segments:
75
74
  - 0
76
75
  version: "0"
77
- name: vidibus-uuid
76
+ type: :runtime
78
77
  version_requirements: *id004
79
78
  - !ruby/object:Gem::Dependency
79
+ name: vidibus-words
80
80
  prerelease: false
81
- type: :runtime
82
81
  requirement: &id005 !ruby/object:Gem::Requirement
83
82
  none: false
84
83
  requirements:
@@ -88,53 +87,55 @@ dependencies:
88
87
  segments:
89
88
  - 0
90
89
  version: "0"
91
- name: vidibus-words
90
+ type: :runtime
92
91
  version_requirements: *id005
93
92
  - !ruby/object:Gem::Dependency
93
+ name: bundler
94
94
  prerelease: false
95
- type: :development
96
95
  requirement: &id006 !ruby/object:Gem::Requirement
97
96
  none: false
98
97
  requirements:
99
98
  - - ">="
100
99
  - !ruby/object:Gem::Version
101
- hash: 3
100
+ hash: 23
102
101
  segments:
102
+ - 1
103
103
  - 0
104
- version: "0"
105
- name: jeweler
104
+ - 0
105
+ version: 1.0.0
106
+ type: :development
106
107
  version_requirements: *id006
107
108
  - !ruby/object:Gem::Dependency
109
+ name: rspec
108
110
  prerelease: false
109
- type: :development
110
111
  requirement: &id007 !ruby/object:Gem::Requirement
111
112
  none: false
112
113
  requirements:
113
- - - ">="
114
+ - - ~>
114
115
  - !ruby/object:Gem::Version
115
- hash: 3
116
+ hash: 7
116
117
  segments:
117
- - 0
118
- version: "0"
119
- name: rake
118
+ - 2
119
+ version: "2"
120
+ type: :development
120
121
  version_requirements: *id007
121
122
  - !ruby/object:Gem::Dependency
123
+ name: rr
122
124
  prerelease: false
123
- type: :development
124
125
  requirement: &id008 !ruby/object:Gem::Requirement
125
126
  none: false
126
127
  requirements:
127
- - - ~>
128
+ - - ">="
128
129
  - !ruby/object:Gem::Version
129
- hash: 7
130
+ hash: 3
130
131
  segments:
131
- - 2
132
- version: "2"
133
- name: rspec
132
+ - 0
133
+ version: "0"
134
+ type: :development
134
135
  version_requirements: *id008
135
136
  - !ruby/object:Gem::Dependency
137
+ name: rake
136
138
  prerelease: false
137
- type: :development
138
139
  requirement: &id009 !ruby/object:Gem::Requirement
139
140
  none: false
140
141
  requirements:
@@ -144,11 +145,11 @@ dependencies:
144
145
  segments:
145
146
  - 0
146
147
  version: "0"
147
- name: rr
148
+ type: :development
148
149
  version_requirements: *id009
149
150
  - !ruby/object:Gem::Dependency
151
+ name: rcov
150
152
  prerelease: false
151
- type: :development
152
153
  requirement: &id010 !ruby/object:Gem::Requirement
153
154
  none: false
154
155
  requirements:
@@ -158,7 +159,7 @@ dependencies:
158
159
  segments:
159
160
  - 0
160
161
  version: "0"
161
- name: relevance-rcov
162
+ type: :development
162
163
  version_requirements: *id010
163
164
  description: Allows changeable permalinks (good for SEO).
164
165
  email: andre@vidibus.com
@@ -166,31 +167,20 @@ executables: []
166
167
 
167
168
  extensions: []
168
169
 
169
- extra_rdoc_files:
170
- - LICENSE
171
- - README.rdoc
170
+ extra_rdoc_files: []
171
+
172
172
  files:
173
- - .bundle/config
174
- - .document
175
- - .rspec
176
- - Gemfile
177
- - LICENSE
178
- - README.rdoc
179
- - Rakefile
180
- - VERSION
181
- - app/models/permalink.rb
182
- - lib/vidibus-permalink.rb
183
- - lib/vidibus/permalink.rb
184
173
  - lib/vidibus/permalink/dispatcher.rb
185
174
  - lib/vidibus/permalink/mongoid.rb
186
- - spec/models.rb
187
- - spec/permalink_spec.rb
188
- - spec/spec_helper.rb
189
- - spec/vidibus/permalink/dispatcher_spec.rb
190
- - spec/vidibus/permalink/mongoid_spec.rb
191
- - vidibus-permalink.gemspec
175
+ - lib/vidibus/permalink/version.rb
176
+ - lib/vidibus/permalink.rb
177
+ - lib/vidibus-permalink.rb
178
+ - app/models/permalink.rb
179
+ - LICENSE
180
+ - README.md
181
+ - Rakefile
192
182
  has_rdoc: true
193
- homepage: http://github.com/vidibus/vidibus-permalink
183
+ homepage: https://github.com/vidibus/vidibus-permalink
194
184
  licenses: []
195
185
 
196
186
  post_install_message:
@@ -212,10 +202,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
202
  requirements:
213
203
  - - ">="
214
204
  - !ruby/object:Gem::Version
215
- hash: 3
205
+ hash: 23
216
206
  segments:
217
- - 0
218
- version: "0"
207
+ - 1
208
+ - 3
209
+ - 6
210
+ version: 1.3.6
219
211
  requirements: []
220
212
 
221
213
  rubyforge_project:
data/.bundle/config DELETED
@@ -1,2 +0,0 @@
1
- ---
2
- BUNDLE_DISABLE_SHARED_GEMS: "1"
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --colour
2
- --format nested
data/Gemfile DELETED
@@ -1,15 +0,0 @@
1
- source :rubygems
2
-
3
- gem "rails", "~> 3.0"
4
- gem "mongoid", "~> 2.0"
5
- gem "vidibus-core_extensions"
6
- gem "vidibus-uuid"
7
- gem "vidibus-words"
8
-
9
- group :development do
10
- gem "jeweler"
11
- gem "rake"
12
- gem "rspec", "~> 2"
13
- gem "rr"
14
- gem "relevance-rcov"
15
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.4
data/spec/models.rb DELETED
@@ -1,20 +0,0 @@
1
- require "vidibus-uuid"
2
-
3
- class Asset
4
- include Mongoid::Document
5
- include Vidibus::Uuid::Mongoid
6
- field :label
7
- end
8
-
9
- class Category
10
- include Mongoid::Document
11
- include Vidibus::Uuid::Mongoid
12
- field :label
13
- end
14
-
15
- # class Article
16
- # include Mongoid::Document
17
- # field :label
18
- # field :date, :format => Date
19
- # #permalink :label, :date
20
- # end
@@ -1,231 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Permalink" do
4
- let(:asset) {Asset.create!(:label => "Something")}
5
- let(:category) {Category.create!(:label => "Else")}
6
- let(:this) {Permalink.create!(:value => "Hey Joe!", :linkable => asset)}
7
- let(:another) {Permalink.create!(:value => "Something", :linkable => asset)}
8
-
9
- def create_permalink(options = {})
10
- options[:value] ||= "Super Trouper"
11
- options[:linkable] ||= asset
12
- Permalink.create!(options)
13
- end
14
-
15
- def stub_stopwords(list)
16
- I18n.backend.store_translations :en, :vidibus => {:stopwords => list}
17
- end
18
-
19
- describe "validation" do
20
- it "should pass with valid attributes" do
21
- this.should be_valid
22
- end
23
-
24
- it "should fail without a value" do
25
- this.value = nil
26
- this.should be_invalid
27
- end
28
-
29
- it "should fail without an UUID given on linkable" do
30
- asset.uuid = nil
31
- expect {this.linkable}.to raise_error(Permalink::UuidRequiredError)
32
- end
33
-
34
- it "should fail if linkable_uuid is invalid" do
35
- this.linkable_uuid = "something"
36
- this.should be_invalid
37
- end
38
-
39
- it "should fail if linkable_uuid is missing" do
40
- this.linkable_class = nil
41
- this.should be_invalid
42
- end
43
- end
44
-
45
- describe "deleting" do
46
- let(:last) {Permalink.create!(:value => "Buh!", :linkable => asset)}
47
-
48
- before do
49
- stub_time!("04.11.2010")
50
- this
51
- stub_time!("05.11.2010")
52
- another
53
- end
54
-
55
- it "should not affect other permalinks of the same linkable unless the deleted permalink was the current one" do
56
- this.reload.destroy.should be_true
57
- another.reload.current?.should be_true
58
- end
59
-
60
- it "should set the lastly updated permalink as current if the deleted permalink was the current one" do
61
- last.destroy.should be_true
62
- another.reload.current?.should be_true
63
- end
64
-
65
- it "should not affect other permalinks but the last one if the deleted permalink was the current one" do
66
- last.destroy.should be_true
67
- this.reload.current?.should be_false
68
- end
69
- end
70
-
71
- describe "#linkable=" do
72
- let(:this) { Permalink.new }
73
-
74
- it "should set linkable_uuid" do
75
- this.linkable = asset
76
- this.linkable_uuid.should eql(asset.uuid)
77
- end
78
-
79
- it "should set linkable_class" do
80
- this.linkable = asset
81
- this.linkable_class.should eql("Asset")
82
- end
83
- end
84
-
85
- describe "#linkable" do
86
- before {this.instance_variable_set("@linkable", nil)}
87
-
88
- it "should fetch the linkable object" do
89
- this.linkable.should eql(asset)
90
- end
91
-
92
- it "should return nil if no linkable_class has been set" do
93
- this.linkable_class = nil
94
- this.linkable.should be_nil
95
- end
96
-
97
- it "should return nil if no linkable_uuid has been set" do
98
- this.linkable_uuid = nil
99
- this.linkable.should be_nil
100
- end
101
- end
102
-
103
- describe "#value" do
104
- it "should be sanitized when set" do
105
- this.value = "Hey Joe!"
106
- this.value.should eql("hey-joe")
107
- end
108
-
109
- context "with stop words" do
110
- before {stub_stopwords(%w[its a])}
111
-
112
- it "should be cleaned from stop words before validation" do
113
- this.value = "It's a beautiful day."
114
- this.value.should eql("beautiful-day")
115
- end
116
-
117
- it "should not be cleaned from stop words if the resulting value would be empty" do
118
- this.value = "It's a..."
119
- this.value.should eql("it-s-a")
120
- end
121
-
122
- it "should not be cleaned from stop words if the resulting value already exists" do
123
- Permalink.create!(:value => "It's a beautiful day.", :linkable => asset)
124
- this = Permalink.new(:value => "It's a beautiful day.")
125
- this.value.should eql("it-s-a-beautiful-day")
126
- end
127
- end
128
-
129
- describe "incrementation" do
130
- it "should be performed unless value is unique" do
131
- this.value = another.value
132
- this.save.should be_true
133
- this.value.should_not eql(another.value)
134
- end
135
-
136
- it "should not be performed unless value did change" do
137
- this.update_attributes(:value => "It's a beautiful day.")
138
- dont_allow(this).increment
139
- this.value = "It's a beautiful day."
140
- end
141
-
142
- it "should append 2 as first number" do
143
- first = create_permalink
144
- create_permalink.value.should eql("super-trouper-2")
145
- end
146
-
147
- it "should append 3 if 2 is already taken" do
148
- create_permalink
149
- create_permalink
150
- create_permalink.value.should eql("super-trouper-3")
151
- end
152
-
153
- it "should append 2 if 3 is taken but 2 has been deleted" do
154
- create_permalink
155
- second = create_permalink
156
- create_permalink
157
- second.reload.destroy
158
- create_permalink.value.should eql("super-trouper-2")
159
- end
160
- end
161
- end
162
-
163
- describe "#current?" do
164
- it "should return true after creation" do
165
- this.current?.should be_true
166
- end
167
-
168
- it "should return true after update" do
169
- another
170
- this.save
171
- this.reload.current?.should be_true
172
- end
173
-
174
- it "should return false on all other permalinks of the assigned linkable" do
175
- first = create_permalink
176
- second = create_permalink
177
- third = create_permalink
178
- first.reload.current?.should be_false
179
- second.reload.current?.should be_false
180
- third.reload.current?.should be_true
181
- end
182
-
183
- it "should not affect permalinks of other linkables" do
184
- this
185
- another = Permalink.create!(:value => "Buh!", :linkable => category)
186
- another.current?.should be_true
187
- this.reload.current?.should be_true
188
- end
189
- end
190
-
191
- describe "#current" do
192
- before {this; another}
193
-
194
- it "should return self for the current permalink" do
195
- another.reload.current.should eql(another)
196
- end
197
-
198
- it "should return the current permalink of the given linkable" do
199
- this.reload.current.should eql(another)
200
- end
201
- end
202
-
203
- describe ".for_value" do
204
- it "should return finder conditions to retreive permalinks for the given value" do
205
- this; another
206
- Permalink.for_value("Hey Joe!").to_a.should have(1).permalink
207
- end
208
- end
209
-
210
- describe ".for_linkable" do
211
- it "should return finder conditions to retreive permalinks for the given object" do
212
- this
213
- Permalink.create!(:value => "Buh!", :linkable => category)
214
- Permalink.for_linkable(asset).to_a.should have(1).permalink
215
- end
216
- end
217
-
218
- describe ".dispatch" do
219
- it "should return a Vidibus::Permalink::Dispatcher object" do
220
- Permalink.dispatch("/something").should be_a(Vidibus::Permalink::Dispatcher)
221
- end
222
- end
223
-
224
- describe ".sanitize" do
225
- before {stub_stopwords(%w[its a])}
226
-
227
- it "should return a sanitized string without stopwords" do
228
- Permalink.sanitize("It's a beautiful day.").should eql("beautiful-day")
229
- end
230
- end
231
- end
data/spec/spec_helper.rb DELETED
@@ -1,43 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
-
4
- require "rubygems"
5
- require "rspec"
6
- require "rr"
7
- require "mongoid"
8
- require "active_support/core_ext"
9
- require "vidibus-core_extensions"
10
- require "vidibus-uuid"
11
- require "vidibus-words"
12
-
13
- require "models"
14
- require "vidibus-permalink"
15
- require "app/models/permalink"
16
-
17
- Mongoid.configure do |config|
18
- name = "vidibus-permalink_test"
19
- host = "localhost"
20
- config.master = Mongo::Connection.new.db(name)
21
- config.logger = nil
22
- end
23
-
24
- RSpec.configure do |config|
25
- config.mock_with :rr
26
- config.before(:each) do
27
- Mongoid.master.collections.select {|c| c.name !~ /system/}.each(&:drop)
28
- end
29
- end
30
-
31
- I18n.load_path += Dir[File.join('config', 'locales', '**', '*.{rb,yml}')]
32
-
33
- # Helper for stubbing time. Define String to be set as Time.now.
34
- # Usage:
35
- # stub_time('01.01.2010 14:00')
36
- # stub_time(2.days.ago)
37
- #
38
- def stub_time!(string = nil)
39
- string ||= Time.now.to_s(:db)
40
- now = Time.parse(string.to_s)
41
- stub(Time).now {now}
42
- now
43
- end
@@ -1,148 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Vidibus::Permalink::Dispatcher" do
4
- describe "Dispatcher" do
5
-
6
- let(:category) {Category.create!}
7
- let(:asset) {Asset.create!}
8
- let(:category_permalink) {Permalink.create!(:value => "Something", :linkable => category)}
9
- let(:asset_permalink) {Permalink.create!(:value => "Pretty", :linkable => asset)}
10
- let(:this) {Vidibus::Permalink::Dispatcher.new("/something/pretty")}
11
-
12
- describe "initializing" do
13
- it "should require a path" do
14
- expect {Vidibus::Permalink::Dispatcher.new}.to raise_error(ArgumentError)
15
- end
16
-
17
- it "should require an absolute request path" do
18
- expect {Vidibus::Permalink::Dispatcher.new("something/pretty")}.
19
- to raise_error(Vidibus::Permalink::Dispatcher::PathError)
20
- end
21
-
22
- it "should accept an absolute request path" do
23
- this.should be_a(Vidibus::Permalink::Dispatcher)
24
- end
25
- end
26
-
27
- describe "#path" do
28
- it "should return the given request path" do
29
- this.path.should eql("/something/pretty")
30
- end
31
- end
32
-
33
- describe "#path=" do
34
- it "should set the request path" do
35
- this.path = "/something/nasty"
36
- this.path.should eql("/something/nasty")
37
- end
38
- end
39
-
40
- describe "#parts" do
41
- it "should contain the parts of the given path" do
42
- this.parts.should eql(%w[something pretty])
43
- end
44
-
45
- it "should deal with empty parts of path" do
46
- this.path = "/something//pretty"
47
- this.parts.should eql(%w[something pretty])
48
- end
49
-
50
- it "should ignore params" do
51
- this.path = "/something/pretty?hello=world"
52
- this.parts.should eql(%w[something pretty])
53
- end
54
-
55
- it "should ignore file extension" do
56
- this.path = "/something/pretty?hello=world"
57
- this.parts.should eql(%w[something pretty])
58
- end
59
- end
60
-
61
- describe "#objects" do
62
- before do
63
- category_permalink
64
- asset_permalink
65
- end
66
-
67
- it "should contain all permalinks of given path" do
68
- this.objects.should eql([category_permalink, asset_permalink])
69
- end
70
-
71
- it "should reflect the order of the parts in request path" do
72
- this = Vidibus::Permalink::Dispatcher.new("/pretty/something")
73
- this.objects.should eql([asset_permalink, category_permalink])
74
- end
75
-
76
- it "should contain empty records for unresolvable parts of the path" do
77
- this = Vidibus::Permalink::Dispatcher.new("/some/pretty")
78
- this.objects.should eql([nil, asset_permalink])
79
- end
80
-
81
- it "should not contain more than one permalink per linkable" do
82
- Permalink.create!(:value => "New", :linkable => asset)
83
- this = Vidibus::Permalink::Dispatcher.new("/pretty/new")
84
- this.objects.should eql([asset_permalink, nil])
85
- end
86
- end
87
-
88
- describe "found?" do
89
- before do
90
- category_permalink
91
- asset_permalink
92
- end
93
-
94
- it "should return true if all parts of the request path could be resolved" do
95
- this.found?.should be_true
96
- end
97
-
98
- it "should return false if any part of the request path could not be resolved" do
99
- this = Vidibus::Permalink::Dispatcher.new("/some/pretty")
100
- this.found?.should be_false
101
- end
102
- end
103
-
104
- describe "#redirect?" do
105
- before do
106
- category_permalink
107
- asset_permalink
108
- Permalink.create!(:value => "New", :linkable => asset)
109
- end
110
-
111
- it "should return true if any part of the path is not current" do
112
- this.redirect?.should be_true
113
- end
114
-
115
- it "should return false if all parts of the request path are current" do
116
- this = Vidibus::Permalink::Dispatcher.new("/something/new")
117
- this.redirect?.should be_false
118
- end
119
-
120
- it "should return nil if path could not be resolved" do
121
- this = Vidibus::Permalink::Dispatcher.new("/something/ugly")
122
- this.redirect?.should be_nil
123
- end
124
- end
125
-
126
- describe "#redirect_path" do
127
- before do
128
- category_permalink
129
- asset_permalink
130
- Permalink.create!(:value => "New", :linkable => asset)
131
- end
132
-
133
- it "should return the current request path" do
134
- this = Vidibus::Permalink::Dispatcher.new("/something/pretty")
135
- this.redirect_path.should eql("/something/new")
136
- end
137
-
138
- it "should return nil if redirecting is not necessary" do
139
- this = Vidibus::Permalink::Dispatcher.new("/something/new")
140
- this.redirect_path.should be_nil
141
- end
142
-
143
- it "should not raise an error if no current permalink object is present" do
144
- pending("this has to be solved!")
145
- end
146
- end
147
- end
148
- end
@@ -1,189 +0,0 @@
1
- require "spec_helper"
2
-
3
- class Base
4
- include Mongoid::Document
5
- include Vidibus::Uuid::Mongoid
6
- include Vidibus::Permalink::Mongoid
7
- end
8
-
9
- class Model < Base
10
- field :name
11
- permalink :name
12
- end
13
-
14
- class Appointment < Base
15
- field :reason
16
- field :location
17
- permalink :reason, :location
18
- end
19
-
20
- class Car < Base
21
- field :make
22
- end
23
-
24
- describe "Vidibus::Permalink::Mongoid" do
25
-
26
- let(:john) {Model.new(:name => "John Malkovich")}
27
- let(:appointment) {Appointment.create(:location => "Bistro", :reason => "Lunch")}
28
-
29
- describe "validation" do
30
- it "should fail if permalink is blank" do
31
- model = Model.new(:permalink => "")
32
- model.should be_invalid
33
- model.errors[:permalink].should have(1).error
34
- end
35
- end
36
-
37
- describe "destroying" do
38
- it "should trigger deleting of all permalink objects with linkable" do
39
- appointment.destroy
40
- Permalink.all.to_a.should have(:no).permalinks
41
- end
42
-
43
- it "should not delete permalink objects of other linkables" do
44
- john.save
45
- appointment.destroy
46
- Permalink.all.to_a.should have(1).permalink
47
- end
48
- end
49
-
50
- describe "#permalink" do
51
- it "should set permalink attribute before validation" do
52
- john.valid?
53
- john.permalink.should eql("john-malkovich")
54
- end
55
-
56
- it "should persist the permalink" do
57
- john.save
58
- john = Model.first
59
- john.permalink.should eql("john-malkovich")
60
- end
61
-
62
- it "should create a permalink object from given attribute after creation" do
63
- john.save
64
- permalink = Permalink.first
65
- permalink.value.should eql("john-malkovich")
66
- end
67
-
68
- it "should not store a new permalink object unless attribute value did change" do
69
- john.save
70
- john.save
71
- Permalink.all.to_a.should have(1).object
72
- end
73
-
74
- it "should store a new permalink if attributes change" do
75
- john.save
76
- john.update_attributes(:name => "Inkognito")
77
- john.reload.permalink.should eql("inkognito")
78
- end
79
-
80
- it "should store a new permalink object if permalink changes" do
81
- john.save
82
- john.update_attributes(:name => "Inkognito")
83
- permalinks = Permalink.all.to_a
84
- permalinks.should have(2).permalinks
85
- permalinks.last.value.should eql("inkognito")
86
- permalinks.last.should be_current
87
- end
88
-
89
- it "should should set a former permalink object as current if possible" do
90
- john.save
91
- john.update_attributes(:name => "Inkognito")
92
- john.update_attributes(:name => "John Malkovich")
93
- permalinks = Permalink.all.to_a
94
- permalinks.should have(2).objects
95
- permalinks.first.should be_current
96
- end
97
-
98
- it "should accept multiple attributes" do
99
- appointment.permalink.should eql("lunch-bistro")
100
- end
101
-
102
- it "should be updatable" do
103
- appointment.update_attributes(:reason => "Drinking")
104
- appointment.permalink.should eql("drinking-bistro")
105
- end
106
-
107
- it "should raise an error unless permalink attributes have been defined" do
108
- expect {Car.create(:make => "Porsche")}.to raise_error(Car::PermalinkConfigurationError)
109
- end
110
-
111
- context "with :repository option set to false" do
112
- before {Model.permalink(:name, :repository => false)}
113
-
114
- it "should be proper" do
115
- john = Model.create(:name => "John Malkovich")
116
- john.permalink.should eql("john-malkovich")
117
- end
118
-
119
- it "should not be stored as permalink object when :repository option is set to false" do
120
- Model.create(:name => "John Malkovich")
121
- Permalink.all.should be_empty
122
- end
123
-
124
- it "should be unique for model" do
125
- pending "Allow incrementation for class that serves as repository."
126
- Model.create(:name => "John Malkovich")
127
- john = Model.create(:name => "John Malkovich")
128
- john.permalink.should_not eql("john-malkovich")
129
- end
130
- end
131
- end
132
-
133
- describe "#permalink_object" do
134
- it "should return the current permalink object" do
135
- appointment.update_attributes(:reason => "Drinking")
136
- permalink = appointment.permalink_object
137
- permalink.should be_a(Permalink)
138
- permalink.value.should eql(appointment.permalink)
139
- permalink.should be_current
140
- end
141
-
142
- it "should return the permalink object assigned recently" do
143
- appointment.reason = "Drinking"
144
- appointment.valid?
145
- appointment.permalink_object.should be_a_new_record
146
- end
147
- end
148
-
149
- describe "#permalink_objects" do
150
- it "should return all permalink objects ordered by time of update" do
151
- stub_time!("04.11.2010")
152
- appointment.update_attributes(:reason => "Drinking")
153
- stub_time!("05.11.2010")
154
- appointment.update_attributes(:reason => "Lunch")
155
- permalinks = appointment.permalink_objects
156
- permalinks[0].value.should eql("drinking-bistro")
157
- permalinks[1].value.should eql("lunch-bistro")
158
- end
159
-
160
- it "should only return permalink objects assigned to the current linkable" do
161
- john.save
162
- appointment.permalink_objects.to_a.should have(1).permalink
163
- end
164
- end
165
-
166
- describe "#permalink_repository" do
167
- it "should default to Vidibus::Permalink" do
168
- Car.permalink(:whatever)
169
- Car.new.permalink_repository.should eql(Permalink)
170
- end
171
-
172
- it "should be nil if :repository option is set to false" do
173
- Car.permalink(:whatever, :repository => false)
174
- Car.new.permalink_repository.should be_nil
175
- end
176
- end
177
-
178
- describe ".permalink" do
179
- it "should set .permalink_attributes" do
180
- Car.permalink(:whatever, :it, :takes)
181
- Car.permalink_attributes.should eql([:whatever, :it, :takes])
182
- end
183
-
184
- it "should set .permalink_options" do
185
- Car.permalink(:whatever, :it, :takes, :repository => false)
186
- Car.permalink_options.should eql({:repository => false})
187
- end
188
- end
189
- end
@@ -1,84 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{vidibus-permalink}
8
- s.version = "0.0.4"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Andre Pankratz"]
12
- s.date = %q{2011-05-09}
13
- s.description = %q{Allows changeable permalinks (good for SEO).}
14
- s.email = %q{andre@vidibus.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".bundle/config",
21
- ".document",
22
- ".rspec",
23
- "Gemfile",
24
- "LICENSE",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION",
28
- "app/models/permalink.rb",
29
- "lib/vidibus-permalink.rb",
30
- "lib/vidibus/permalink.rb",
31
- "lib/vidibus/permalink/dispatcher.rb",
32
- "lib/vidibus/permalink/mongoid.rb",
33
- "spec/models.rb",
34
- "spec/permalink_spec.rb",
35
- "spec/spec_helper.rb",
36
- "spec/vidibus/permalink/dispatcher_spec.rb",
37
- "spec/vidibus/permalink/mongoid_spec.rb",
38
- "vidibus-permalink.gemspec"
39
- ]
40
- s.homepage = %q{http://github.com/vidibus/vidibus-permalink}
41
- s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.6.2}
43
- s.summary = %q{Permalink handling}
44
-
45
- if s.respond_to? :specification_version then
46
- s.specification_version = 3
47
-
48
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
- s.add_runtime_dependency(%q<rails>, ["~> 3.0"])
50
- s.add_runtime_dependency(%q<mongoid>, ["~> 2.0"])
51
- s.add_runtime_dependency(%q<vidibus-core_extensions>, [">= 0"])
52
- s.add_runtime_dependency(%q<vidibus-uuid>, [">= 0"])
53
- s.add_runtime_dependency(%q<vidibus-words>, [">= 0"])
54
- s.add_development_dependency(%q<jeweler>, [">= 0"])
55
- s.add_development_dependency(%q<rake>, [">= 0"])
56
- s.add_development_dependency(%q<rspec>, ["~> 2"])
57
- s.add_development_dependency(%q<rr>, [">= 0"])
58
- s.add_development_dependency(%q<relevance-rcov>, [">= 0"])
59
- else
60
- s.add_dependency(%q<rails>, ["~> 3.0"])
61
- s.add_dependency(%q<mongoid>, ["~> 2.0"])
62
- s.add_dependency(%q<vidibus-core_extensions>, [">= 0"])
63
- s.add_dependency(%q<vidibus-uuid>, [">= 0"])
64
- s.add_dependency(%q<vidibus-words>, [">= 0"])
65
- s.add_dependency(%q<jeweler>, [">= 0"])
66
- s.add_dependency(%q<rake>, [">= 0"])
67
- s.add_dependency(%q<rspec>, ["~> 2"])
68
- s.add_dependency(%q<rr>, [">= 0"])
69
- s.add_dependency(%q<relevance-rcov>, [">= 0"])
70
- end
71
- else
72
- s.add_dependency(%q<rails>, ["~> 3.0"])
73
- s.add_dependency(%q<mongoid>, ["~> 2.0"])
74
- s.add_dependency(%q<vidibus-core_extensions>, [">= 0"])
75
- s.add_dependency(%q<vidibus-uuid>, [">= 0"])
76
- s.add_dependency(%q<vidibus-words>, [">= 0"])
77
- s.add_dependency(%q<jeweler>, [">= 0"])
78
- s.add_dependency(%q<rake>, [">= 0"])
79
- s.add_dependency(%q<rspec>, ["~> 2"])
80
- s.add_dependency(%q<rr>, [">= 0"])
81
- s.add_dependency(%q<relevance-rcov>, [">= 0"])
82
- end
83
- end
84
-