objectreload-permalinks 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
1
3
  require 'rubygems'
2
4
  require 'rake'
3
5
 
@@ -8,7 +10,9 @@ begin
8
10
  gem.summary = "Simple way to create permalinks."
9
11
  gem.email = "gems@objectreload.com"
10
12
  gem.homepage = "http://github.com/objectreload/permalinks"
11
- gem.authors = ["Mateusz Drozdzynski", "Ewa Limanowka"]
13
+ gem.authors = ["Mateusz Drożdżyński", "Ewa Limanówka", "Jakub Kuźma"]
14
+ gem.add_dependency "activesupport", ">=2.0"
15
+ gem.add_development_dependency "test-unit", ">=2.0"
12
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
17
  end
14
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.1
data/init.rb CHANGED
@@ -1,2 +1,5 @@
1
- # Include hook code here
2
- require File.dirname(__FILE__) + '/lib/permalinks'
1
+ require File.join(File.dirname(__FILE__), "lib", "permalinks")
2
+
3
+ if defined?(ActiveRecord::Base)
4
+ ActiveRecord::Base.extend(Permalinks::HasPermalink)
5
+ end
data/lib/permalinks.rb CHANGED
@@ -1,15 +1,45 @@
1
+ require "iconv"
2
+ require "singleton"
3
+
4
+ require "active_support/core_ext/object/misc"
5
+
6
+ require "permalinks/configuration"
7
+ require "permalinks/has_permalink"
8
+
1
9
  module Permalinks
2
- def self.included(base)
3
- base.extend ClassMethods
4
- end
5
-
6
- module ClassMethods
7
- def has_permalink(field = :name)
8
- define_method(:to_param) do
9
- "#{id}-#{send(field).to_s.strip.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/, '-')
10
+ class << self
11
+ def to_param(*args)
12
+ returning(args * "-") do |result|
13
+ replace_using_table!(result, configuration.substitutions)
14
+ transliterate!(result, configuration.input_encoding)
15
+ result.downcase!
16
+ replace_non_alnum!(result)
17
+ remove_double_dashes!(result)
18
+ remove_trailing_dashes!(result)
10
19
  end
11
20
  end
21
+
22
+ private
23
+
24
+ def replace_using_table!(str, table)
25
+ table.each { |from, to| str.gsub!(from, to) }
26
+ end
27
+
28
+ def transliterate!(str, encoding)
29
+ result = Iconv.iconv("ASCII//TRANSLIT", encoding.to_s, str).first
30
+ str.replace(result)
31
+ end
32
+
33
+ def replace_non_alnum!(str)
34
+ str.gsub!(/[^a-z0-9]/, "-")
35
+ end
36
+
37
+ def remove_double_dashes!(str)
38
+ str.gsub!(/-+/, "-")
39
+ end
40
+
41
+ def remove_trailing_dashes!(str)
42
+ str.gsub!(/-+\Z/, "")
43
+ end
12
44
  end
13
45
  end
14
-
15
- ActiveRecord::Base.send(:include, Permalinks)
@@ -0,0 +1,19 @@
1
+ module Permalinks
2
+ class Configuration
3
+ include Singleton
4
+
5
+ attr_writer :substitutions, :input_encoding
6
+
7
+ def substitutions
8
+ @substitutions ||= {}
9
+ end
10
+
11
+ def input_encoding
12
+ @input_encoding ||= "UTF-8"
13
+ end
14
+ end
15
+
16
+ def self.configuration
17
+ returning(Configuration.instance) { |c| yield(c) if block_given? }
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Permalinks
2
+ module HasPermalink
3
+ def has_permalink(field = :name)
4
+ define_method(:to_param) { Permalinks.to_param(id, send(field)) }
5
+ end
6
+ end
7
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{objectreload-permalinks}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Mateusz Drozdzynski", "Ewa Limanowka"]
12
- s.date = %q{2009-10-20}
11
+ s.authors = ["Mateusz Drożdżyński", "Ewa Limanówka", "Jakub Kuźma"]
12
+ s.date = %q{2010-02-15}
13
13
  s.email = %q{gems@objectreload.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README"
@@ -20,12 +20,13 @@ Gem::Specification.new do |s|
20
20
  "Rakefile",
21
21
  "VERSION",
22
22
  "init.rb",
23
- "install.rb",
24
23
  "lib/permalinks.rb",
24
+ "lib/permalinks/configuration.rb",
25
+ "lib/permalinks/has_permalink.rb",
25
26
  "objectreload-permalinks.gemspec",
26
- "test/permalinks_test.rb",
27
- "test/test_helper.rb",
28
- "uninstall.rb"
27
+ "test/country_test.rb",
28
+ "test/post_test.rb",
29
+ "test/test_helper.rb"
29
30
  ]
30
31
  s.homepage = %q{http://github.com/objectreload/permalinks}
31
32
  s.rdoc_options = ["--charset=UTF-8"]
@@ -33,7 +34,8 @@ Gem::Specification.new do |s|
33
34
  s.rubygems_version = %q{1.3.5}
34
35
  s.summary = %q{Simple way to create permalinks.}
35
36
  s.test_files = [
36
- "test/permalinks_test.rb",
37
+ "test/country_test.rb",
38
+ "test/post_test.rb",
37
39
  "test/test_helper.rb"
38
40
  ]
39
41
 
@@ -42,9 +44,15 @@ Gem::Specification.new do |s|
42
44
  s.specification_version = 3
43
45
 
44
46
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0"])
48
+ s.add_development_dependency(%q<test-unit>, [">= 2.0"])
45
49
  else
50
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
51
+ s.add_dependency(%q<test-unit>, [">= 2.0"])
46
52
  end
47
53
  else
54
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
55
+ s.add_dependency(%q<test-unit>, [">= 2.0"])
48
56
  end
49
57
  end
50
58
 
@@ -0,0 +1,66 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "test_helper"
4
+
5
+ class Country
6
+ extend Permalinks::HasPermalink
7
+ attr_accessor :name, :id
8
+ has_permalink
9
+ end
10
+
11
+ class CountryTest < Test::Unit::TestCase
12
+ def setup
13
+ @country = Country.new
14
+ @country.name = "United Kingdom"
15
+ @country.id = 1
16
+ end
17
+
18
+ test "country with regular name" do
19
+ assert_equal "#{@country.id}-united-kingdom", @country.to_param
20
+ end
21
+
22
+ test "country name with a lot of white spaces" do
23
+ @country.name = "United Kingdom "
24
+ assert_equal "#{@country.id}-united-kingdom", @country.to_param
25
+ end
26
+
27
+ test "country with id equal nil" do
28
+ @country.name = "2-tratata"
29
+ @country.id = nil
30
+ assert_equal "#{@country.id}-2-tratata", @country.to_param
31
+ end
32
+
33
+ test "country name with capital letters" do
34
+ @country.name = " USA "
35
+ assert_equal "#{@country.id}-usa", @country.to_param
36
+ end
37
+
38
+ if RUBY_VERSION >= "1.9" # FIXME: iconv doesn't work properly on 1.8.x
39
+ test "country name in polish" do
40
+ @country.name = "Wybrzeże Kości Słoniowej"
41
+ assert_equal "#{@country.id}-wybrzeze-kosci-sloniowej", @country.to_param
42
+ end
43
+
44
+ test "country name in german without custom substitutions" do
45
+ Permalinks.configuration { |config| config.substitutions = {} }
46
+ @country.name = "Türkei"
47
+ assert_equal "#{@country.id}-turkei", @country.to_param
48
+ end
49
+ end
50
+
51
+ test "country name in german with custom substitutions" do
52
+ Permalinks.configuration do |config|
53
+ config.substitutions = {
54
+ "ä" => "ae",
55
+ "Ä" => "AE",
56
+ "ö" => "oe",
57
+ "Ö" => "OE",
58
+ "ü" => "ue",
59
+ "Ü" => "UE"
60
+ }
61
+ end
62
+
63
+ @country.name = "Türkei"
64
+ assert_equal "#{@country.id}-tuerkei", @country.to_param
65
+ end
66
+ end
data/test/post_test.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "test_helper"
2
+
3
+ class Post
4
+ extend Permalinks::HasPermalink
5
+ attr_accessor :title, :id
6
+ has_permalink :title
7
+ end
8
+
9
+ class PostTest < Test::Unit::TestCase
10
+ def setup
11
+ @post = Post.new
12
+ @post.title = "Something cool"
13
+ @post.id = 1
14
+ end
15
+
16
+ test "regular post title" do
17
+ assert_equal "#{@post.id}-something-cool", @post.to_param
18
+ end
19
+
20
+ test "post title with a lot of white spaces" do
21
+ @post.title = " Snow is cold "
22
+ assert_equal "#{@post.id}-snow-is-cold", @post.to_param
23
+ end
24
+
25
+ test "post title with capital letters" do
26
+ @post.title = " SOMETHING i really like to DO is sleeping "
27
+ assert_equal "#{@post.id}-something-i-really-like-to-do-is-sleeping", @post.to_param
28
+ end
29
+ end
data/test/test_helper.rb CHANGED
@@ -1,29 +1,6 @@
1
- require 'rubygems'
2
- require 'activerecord'
3
- require File.dirname(__FILE__)+'/../lib/permalinks'
4
- require 'shoulda'
5
- require 'logger'
1
+ require "rubygems"
6
2
 
7
- ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
8
- ActiveRecord::Base.establish_connection('sqlite3')
3
+ gem "test-unit" # for 1.8 compatibility
4
+ require "test/unit"
9
5
 
10
- ActiveRecord::Base.logger = Logger.new(STDERR)
11
- ActiveRecord::Base.logger.level = Logger::WARN
12
-
13
- ActiveRecord::Schema.define(:version => 0) do
14
- create_table :countries do |t|
15
- t.string :name, :default => ''
16
- end
17
-
18
- create_table :posts do |t|
19
- t.string :title, :default => ''
20
- end
21
- end
22
-
23
- class Country < ActiveRecord::Base
24
- has_permalink
25
- end
26
-
27
- class Post < ActiveRecord::Base
28
- has_permalink :title
29
- end
6
+ require File.join(File.dirname(__FILE__), "..", "lib", "permalinks")
metadata CHANGED
@@ -1,19 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objectreload-permalinks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - Mateusz Drozdzynski
8
- - Ewa Limanowka
7
+ - "Mateusz Dro\xC5\xBCd\xC5\xBCy\xC5\x84ski"
8
+ - "Ewa Liman\xC3\xB3wka"
9
+ - "Jakub Ku\xC5\xBAma"
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2009-10-20 00:00:00 +01:00
14
+ date: 2010-02-15 00:00:00 +00:00
14
15
  default_executable:
15
- dependencies: []
16
-
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: activesupport
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "2.0"
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit
29
+ type: :development
30
+ version_requirement:
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "2.0"
36
+ version:
17
37
  description:
18
38
  email: gems@objectreload.com
19
39
  executables: []
@@ -28,12 +48,13 @@ files:
28
48
  - Rakefile
29
49
  - VERSION
30
50
  - init.rb
31
- - install.rb
32
51
  - lib/permalinks.rb
52
+ - lib/permalinks/configuration.rb
53
+ - lib/permalinks/has_permalink.rb
33
54
  - objectreload-permalinks.gemspec
34
- - test/permalinks_test.rb
55
+ - test/country_test.rb
56
+ - test/post_test.rb
35
57
  - test/test_helper.rb
36
- - uninstall.rb
37
58
  has_rdoc: true
38
59
  homepage: http://github.com/objectreload/permalinks
39
60
  licenses: []
@@ -63,5 +84,6 @@ signing_key:
63
84
  specification_version: 3
64
85
  summary: Simple way to create permalinks.
65
86
  test_files:
66
- - test/permalinks_test.rb
87
+ - test/country_test.rb
88
+ - test/post_test.rb
67
89
  - test/test_helper.rb
data/install.rb DELETED
@@ -1 +0,0 @@
1
- # Install hook code here
@@ -1,74 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class CountryTest < Test::Unit::TestCase
4
- context "Country model" do
5
- setup do
6
- @country = Country.new :name => "United Kingdom"
7
- @country.save!
8
- end
9
- subject { @country }
10
-
11
- context "regular country name" do
12
- should "return permalink" do
13
- assert_equal "#{@country.id}-united-kingdom", @country.to_param
14
- end
15
- end
16
-
17
- context "country name with a lot of white spaces" do
18
- should "return permalink" do
19
- @country.name = "United Kingdom "
20
- @country.save!
21
- assert_equal "#{@country.id}-united-kingdom", @country.to_param
22
- end
23
- end
24
-
25
- context "country with id equal nil" do
26
- should "return permalink" do
27
- @country.name = "2-tratata"
28
- @country.id = nil
29
- @country.save!
30
- assert_equal "#{@country.id}-2-tratata", @country.to_param
31
- end
32
- end
33
-
34
- context "country name with capital letters" do
35
- should "return permalink" do
36
- @country.name = " USA "
37
- @country.save!
38
- assert_equal "#{@country.id}-usa", @country.to_param
39
- end
40
- end
41
- end
42
- end
43
-
44
- class PostTest < Test::Unit::TestCase
45
- context "Post model" do
46
- setup do
47
- @post = Post.new :title => "Something cool"
48
- @post.save!
49
- end
50
- subject { @post }
51
-
52
- context "regular post title" do
53
- should "return permalink" do
54
- assert_equal "#{@post.id}-something-cool", @post.to_param
55
- end
56
- end
57
-
58
- context "post title with a lot of white spaces" do
59
- should "return permalink" do
60
- @post.title = " Snow is cold "
61
- @post.save!
62
- assert_equal "#{@post.id}-snow-is-cold", @post.to_param
63
- end
64
- end
65
-
66
- context "post title with capital letters" do
67
- should "return permalink" do
68
- @post.title = " SOMETHING i really like to DO is sleeping "
69
- @post.save!
70
- assert_equal "#{@post.id}-something-i-really-like-to-do-is-sleeping", @post.to_param
71
- end
72
- end
73
- end
74
- end
data/uninstall.rb DELETED
@@ -1 +0,0 @@
1
- # Uninstall hook code here