acts_as_nice_url 1.0.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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [Nicolas Cavigneaux]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,61 @@
1
+ -*-markdown-*-
2
+
3
+ NiceUrl
4
+ =======
5
+
6
+ This acts_as extension provides the capabilities for creating a nice url based on an attribute of the current object. You can set / unset the object id in front of the URL and choose the object attribute to use to generate the URL.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ In your Rails app root, use the following command-line :
12
+
13
+ cd vendor/plugins
14
+ hg clone http://bitbucket.org/Bounga/acts_as_nice_url/
15
+
16
+ or install it system-wide :
17
+
18
+ $ sudo gem install flash_helper
19
+
20
+ and require it in Rails::Initializer (environment.rb) :
21
+
22
+ config.gem 'flash_helper'
23
+
24
+
25
+ Example
26
+ -------
27
+
28
+ In your model :
29
+
30
+ class Player < ActiveRecord::Base
31
+ acts_as_nice_url :id => false, :title => :full_name
32
+ end
33
+
34
+ then in your views :
35
+
36
+ link_to(:controller => :players, :action => :show, :id => @player)
37
+
38
+ or
39
+
40
+ player_path(@player)
41
+
42
+ **Use id only in some case**
43
+
44
+
45
+ If you want to use the **id only** in the generated URL, you have to fill the id parameter with the id only, not the full object :
46
+
47
+ link_to(:controller => :players, :action => :show, :id => @player.id)
48
+
49
+ or
50
+
51
+ player_path(@player.id)
52
+
53
+ Other
54
+ -----
55
+
56
+ For more information see [Project homepage](http://www.bitbucket.org/Bounga/acts_as_nice_url/)
57
+
58
+ Problems, comments, and suggestions are welcome on the [ticket system](http://www.bitbucket.org/Bounga/acts_as_nice_url/issues/new/)
59
+
60
+
61
+ Copyright (c) 2008 Nicolas Cavigneaux, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,69 @@
1
+ require 'rake/gempackagetask'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/contrib/rubyforgepublisher'
5
+ require 'rubyforge'
6
+
7
+ SPEC = Gem::Specification.new do |s|
8
+ s.name = 'acts_as_nice_url'
9
+ s.version = '1.0.0'
10
+ s.authors = ['Nicolas Cavigneaux']
11
+ s.email = 'nico@bounga.org'
12
+ s.homepage = 'http://www.bitbucket.org/Bounga/acts_as_nice_url'
13
+ s.rubyforge_project = %q{nice-url}
14
+ s.summary = 'A Ruby on Rails extension to generate pretty URLs from models data '
15
+ s.description = 'This Ruby on Rails acts_as extension provides the capabilities for creating a nice url based on an attribute of the current object. You can set / unset the object id in front of the URL and choose the object attribute to use to generate the URL.'
16
+ s.files = [ "Rakefile", "init.rb", "install.rb", "README", "LICENSE", "uninstall.rb" ] +
17
+ Dir.glob("{bin,assets,doc,lib,tasks,test}/**/*")
18
+ s.test_file = "test/nice_url_test.rb"
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ['README']
21
+ s.require_paths = ["lib"]
22
+ s.add_dependency('activerecord')
23
+ end
24
+
25
+ desc 'run unit tests'
26
+ task :default => :test
27
+
28
+ task :gem
29
+ Rake::GemPackageTask.new(SPEC) do |pkg|
30
+ pkg.need_zip = true
31
+ pkg.need_tar_bz2 = true
32
+ end
33
+
34
+ desc "Install gem file #{SPEC.name}-#{SPEC.version}.gem"
35
+ task :install => [:gem] do
36
+ sh "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem"
37
+ end
38
+
39
+ desc "Publish documentation to RubyForge"
40
+ task :publish_doc => [:rdoc] do
41
+ rf = Rake::RubyForgePublisher.new(SPEC.rubyforge_project, 'bounga')
42
+ rf.upload
43
+ puts "Published documentation to RubyForge"
44
+ end
45
+
46
+ desc "Release gem #{SPEC.name}-#{SPEC.version}.gem"
47
+ task :release => [:gem, :publish_doc] do
48
+ rf = RubyForge.new.configure
49
+ puts "Logging in"
50
+ rf.login
51
+
52
+ puts "Releasing #{SPEC.name} v.#{SPEC.version}"
53
+
54
+ files = Dir.glob('pkg/*.{zip,bz2,gem}')
55
+ rf.add_release SPEC.rubyforge_project, SPEC.rubyforge_project, SPEC.version, *files
56
+ end
57
+
58
+ Rake::TestTask.new(:test) do |t|
59
+ t.libs << 'lib'
60
+ t.pattern = 'test/**/*_test.rb'
61
+ t.verbose = true
62
+ end
63
+
64
+ Rake::RDocTask.new(:rdoc) do |rdoc|
65
+ rdoc.title = 'Acts_as_nice_url'
66
+ rdoc.options << '--line-numbers' << '--inline-source'
67
+ rdoc.rdoc_files.include('README')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'nice_url'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/nice_url.rb ADDED
@@ -0,0 +1,71 @@
1
+ module Bounga
2
+ module Acts
3
+ module NiceUrl
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ # This acts_as extension provides the capabilities for creating a nice url based on an attribute of the current object.
9
+ # You can set / unset the object id in front of the URL and choose the object attribute to use to generate the URL.
10
+ #
11
+ # This extension requires Iconv to be install on your system
12
+ #
13
+ # Author example:
14
+ #
15
+ # class Author < ActiveRecord::Base
16
+ # acts_as_nice_url :id => false, :title => :full_name
17
+ # end
18
+ module ClassMethods
19
+ # Configuration options are:
20
+ #
21
+ # * +id+ - specifies if the object id has to be in front of the URL or not (default: +true+)
22
+ # * +title+ - specifies the object attribute to use to generate the URL. You can use a symbol
23
+ # or a string (default: +title+)
24
+ def acts_as_nice_url(options = {})
25
+ include Bounga::Acts::NiceUrl::InstanceMethods
26
+
27
+ configuration = { :id => true, :title => :title }
28
+ configuration.update(options) if options.is_a?(Hash)
29
+
30
+ class_eval <<-EOV
31
+ def nice_title
32
+ #{configuration[:title]}
33
+ end
34
+
35
+ def nice_id
36
+ #{configuration[:id]}
37
+ end
38
+ EOV
39
+ end
40
+ end
41
+
42
+ # All the methods available to a record that has had <tt>acts_as_nice_url</tt> specified.
43
+ module InstanceMethods
44
+ def to_param
45
+ # Convert non-ascii characters
46
+ s = Iconv.new('us-ascii//TRANSLIT', 'utf-8').iconv(nice_title.to_s).strip.downcase
47
+ # Remove spaces
48
+ s = s.gsub(/\s+/, '-')
49
+ # Replace dots
50
+ s = s.gsub('.', '-')
51
+ # Remove reserved characters
52
+ s = s.gsub(/[^a-z0-9\-]/, '')
53
+ # Remove multiple dashes
54
+ s = s.gsub(/([\-]){2,}/, '\1')
55
+ # Remove ending dashes
56
+ s = s.gsub(/[\-]+$/, '')
57
+
58
+ if nice_id
59
+ s = [self.id, s].join('-')
60
+ end
61
+
62
+ return s
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ ActiveRecord::Base.send(:include, Bounga::Acts::NiceUrl)
70
+
71
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :nice_url do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,103 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'iconv'
6
+
7
+ require "#{File.dirname(__FILE__)}/../init"
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :authors do |t|
14
+ t.string :name, :title
15
+ end
16
+ end
17
+ end
18
+
19
+ def teardown_db
20
+ ActiveRecord::Base.connection.tables.each do |table|
21
+ ActiveRecord::Base.connection.drop_table(table)
22
+ end
23
+ end
24
+
25
+ class Mixin < ActiveRecord::Base
26
+ def self.table_name() "authors" end
27
+ end
28
+
29
+ class AuthorDefault < Mixin
30
+ acts_as_nice_url
31
+ end
32
+
33
+ class AuthorWithoutId < Mixin
34
+ acts_as_nice_url :id => false
35
+ end
36
+
37
+ class AuthorTuned < Mixin
38
+ acts_as_nice_url :title => :name
39
+ end
40
+
41
+ class AuthorTunedWithoutId < Mixin
42
+ acts_as_nice_url :id => false, :title => :name
43
+ end
44
+
45
+ class AuthorString < Mixin
46
+ acts_as_nice_url :title => 'name'
47
+ end
48
+
49
+ class NiceUrlTest < Test::Unit::TestCase
50
+
51
+ def setup
52
+ setup_db
53
+ Mixin.create! :name => 'Nicolas Cavigneaux', :title => 'Rails lover'
54
+ end
55
+
56
+ def teardown
57
+ teardown_db
58
+ end
59
+
60
+ def test_defaults
61
+ assert_equal "1-rails-lover", AuthorDefault.find(:first).to_param
62
+ end
63
+
64
+ def test_without_id
65
+ assert_equal "rails-lover", AuthorWithoutId.find(:first).to_param
66
+ end
67
+
68
+ def test_tuned
69
+ assert_equal "1-nicolas-cavigneaux", AuthorTuned.find(:first).to_param
70
+ end
71
+
72
+ def test_tuned_without_id
73
+ assert_equal "nicolas-cavigneaux", AuthorTunedWithoutId.find(:first).to_param
74
+ end
75
+
76
+ def test_string
77
+ assert_equal "1-nicolas-cavigneaux", AuthorString.find(:first).to_param
78
+ end
79
+
80
+ def test_non_ascii_char
81
+ author = AuthorDefault.find(:first)
82
+ author.title = "Réalisation de tests"
83
+ assert_equal "1-realisation-de-tests", author.to_param
84
+ end
85
+
86
+ def test_reserved_char
87
+ author = AuthorDefault.find(:first)
88
+ author.title = "Rails lover%/...#?*!"
89
+ assert_equal "1-rails-lover", author.to_param
90
+ end
91
+
92
+ def test_multiple_dashes
93
+ author = AuthorDefault.find(:first)
94
+ author.title = "Rails ---lover"
95
+ assert_equal "1-rails-lover", author.to_param
96
+ end
97
+
98
+ def test_end_dashes
99
+ author = AuthorDefault.find(:first)
100
+ author.title = "Rails lover---"
101
+ assert_equal "1-rails-lover", author.to_param
102
+ end
103
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_nice_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Cavigneaux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-27 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: This Ruby on Rails acts_as extension provides the capabilities for creating a nice url based on an attribute of the current object. You can set / unset the object id in front of the URL and choose the object attribute to use to generate the URL.
26
+ email: nico@bounga.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - Rakefile
35
+ - init.rb
36
+ - install.rb
37
+ - README
38
+ - LICENSE
39
+ - uninstall.rb
40
+ - lib/nice_url.rb
41
+ - tasks/nice_url_tasks.rake
42
+ - test/nice_url_test.rb
43
+ has_rdoc: true
44
+ homepage: http://www.bitbucket.org/Bounga/acts_as_nice_url
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: nice-url
65
+ rubygems_version: 1.3.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: A Ruby on Rails extension to generate pretty URLs from models data
69
+ test_files:
70
+ - test/nice_url_test.rb