chinese_permalink 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,8 @@
1
+ global_autotest_file = File.expand_path('~/.autotest')
2
+ load(global_autotest_file) if File.exists?(global_autotest_file)
3
+
4
+ class Autotest
5
+ def test_files_for(filename)
6
+ Dir['test/**/*_test.rb']
7
+ end
8
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Richard Huang (flyerhzm@gmail.com)
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.textile ADDED
@@ -0,0 +1,101 @@
1
+ h1. ChinesePermalink
2
+
3
+ This plugin adds a capability for ar model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.
4
+
5
+ The permalink will be composed of id and the english value translated from chinese text.
6
+
7
+ For exmpale, title of post is '我是中国人', permalink of post is '1-i-am-a-chinese'
8
+
9
+ ***************************************************************************
10
+
11
+ h2. Install
12
+
13
+ * install dependency gem sishen-rtranslate:
14
+
15
+ <pre><code>sudo gem install sishen-rtranslate --source http://gems.github.com</code></pre>
16
+
17
+ * install chinese_permalink plugin
18
+
19
+ <pre><code>script/plugin install git://github.com/flyerhzm/chinese_permalink.git</code></pre>
20
+
21
+ ***************************************************************************
22
+
23
+ h2. Example
24
+
25
+ * Define chinese_permalink to model
26
+
27
+ <pre><code>
28
+ class Post < ActiveRecord::Base
29
+ # create permalink by chinese title, default permalink column is "permalink"
30
+ chinese_permalink :title
31
+ end
32
+ </code></pre>
33
+
34
+ Or
35
+
36
+ <pre><code>
37
+ class Post < ActiveRecord::Base
38
+ # create permalink by chinese category and title
39
+ chinese_permalink [:category, :title]
40
+ end
41
+ </code></pre>
42
+
43
+ Or
44
+
45
+ <pre><code>
46
+ class Post < ActiveRecord::Base
47
+ # create permalink by chinese title, store permalink to column "slug_url"
48
+ chinese_permalink :title, :permalink_field => :slug_url
49
+ end
50
+ </code></pre>
51
+
52
+
53
+ * Generate migration
54
+
55
+ <pre><code>ruby script/generate chinese_permalink_migration (migration name) (table name) (permalink column name)</code></pre>
56
+
57
+ For example:
58
+
59
+ <pre><code>ruby script/generate chinese_permalink_migration add_permalink_to_posts posts</code></pre>
60
+
61
+ Or
62
+
63
+ <pre><code>ruby script/generate chinese_permalink_migration add_permalink_to_posts posts slug_url</code></pre>
64
+
65
+
66
+ 3. Define ar to_param method
67
+
68
+ <pre><code>
69
+ class Post < ActiveRecord::Base
70
+ def to_param
71
+ permalink
72
+ end
73
+ end
74
+ </code></pre>
75
+
76
+ **************************************************************************
77
+
78
+ h2. Advance
79
+
80
+ You can add before_methods and after_methods to meet your business, for example:
81
+
82
+ <pre><code>
83
+ class Post < ActiveRecord::Base
84
+ chinese_permalink :title, :before_methods => :parse_c_sharp
85
+
86
+ def parse_c_sharp(permalink)
87
+ permalink.gsub('C#', 'c-sharp')
88
+ end
89
+ end
90
+
91
+ class Post < ActiveRecord::Base
92
+ chinese_permalink :title, :after_methods => :parse_pg
93
+
94
+ def parse_pg(permalink)
95
+ permalink.gsub('Procter &amp; Gamble', 'pg')
96
+ end
97
+ end
98
+ </code></pre>
99
+
100
+
101
+ Copyright (c) 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'jeweler'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the chinese_permalink plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the chinese_permalink plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'ChinesePermalink'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "chinese_permalink"
28
+ gemspec.summary = "This plugin adds a capability for AR model to create a seo permalink with your chinese text."
29
+ gemspec.description = "This plugin adds a capability for AR model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate."
30
+ gemspec.email = "flyerhzm@gmail.com"
31
+ gemspec.homepage = "http://github.com/flyerhzm/chinese_permalink"
32
+ gemspec.authors = ["Richard Huang"]
33
+ gemspec.add_dependency 'sishen-rtranslate'
34
+ gemspec.files.exclude '.gitignore'
35
+ gemspec.files.exclude 'log/*'
36
+ end
37
+ Jeweler::GemcutterTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,21 @@
1
+ # user this migration generator
2
+ # ruby script/generate chinese_permalink_migartion (migration name) (table name) [permalink column name]
3
+ # for example:
4
+ # ruby script/generate chinese_permalink_migration add_permalink_to_posts posts
5
+ # ruby script/generate chiense_permalink_migration add_permalink_to_posts posts slug_url
6
+ class ChinesePermalinkMigrationGenerator < Rails::Generator::NamedBase
7
+ attr_reader :permalink_table_name
8
+ attr_reader :permalink_field_name
9
+
10
+ def initialize(runtime_args, runtime_options = {})
11
+ @permalink_table_name = runtime_args[1]
12
+ @permalink_field_name = runtime_args[2] || 'permalink'
13
+ super
14
+ end
15
+
16
+ def manifest
17
+ record do |m|
18
+ m.migration_template 'migration.rb', 'db/migrate'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ class Add<%= permalink_field_name.capitalize %>To<%= permalink_table_name.capitalize %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= permalink_table_name %>, :<%= permalink_field_name %>, :string, :default => nil
4
+
5
+ <%= permalink_table_name.classify %>.reset_column_information
6
+ <%= permalink_table_name.classify %>.all.each do |obj|
7
+ obj.<%= permalink_field_name %> = nil
8
+ obj.save
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ remove_column :<%= permalink_table_name %>, :<%= permalink_field_name %>
14
+ end
15
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActiveRecord::Base.send :include, ChinesePermalink
@@ -0,0 +1,76 @@
1
+ begin
2
+ # rtranslate is a gem that can translate text based on google translate
3
+ require 'rtranslate'
4
+ rescue Object
5
+ puts "no rtranslate, you might want to look into it."
6
+ end
7
+
8
+ module ChinesePermalink
9
+
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ class <<base
13
+ attr_accessor :permalink_attrs
14
+ attr_accessor :permalink_field
15
+ attr_accessor :before_methods
16
+ attr_accessor :after_methods
17
+ end
18
+ end
19
+
20
+ private
21
+ def create_permalink
22
+ if self.permalink.nil?
23
+ chinese_permalink = self.class.permalink_attrs.collect do |attr_name|
24
+ chinese_value = self.send(attr_name)
25
+ end * '-'
26
+ self.class.before_methods.each do |method|
27
+ chinese_permalink = self.send(method, chinese_permalink)
28
+ end
29
+
30
+ english_permalink = Translate.t(chinese_permalink, 'CHINESE', 'ENGLISH')
31
+ self.class.after_methods.each do |method|
32
+ english_permalink = self.send(method, english_permalink)
33
+ end
34
+
35
+ english_permalink = remove_duplicate_dash(remove_tailing_dash(remove_non_ascii(remove_space(remove_punctuation(english_permalink))))).downcase
36
+ permalink = id.to_s + '-' + english_permalink
37
+ self.update_attribute(self.class.permalink_field, permalink)
38
+ end
39
+ end
40
+
41
+ def remove_tailing_dash(text)
42
+ text.gsub(/-+$/, '')
43
+ end
44
+
45
+ def remove_non_ascii(text)
46
+ text.gsub('_', '-').gsub(/[^-a-zA-Z0-9]/, '')
47
+ end
48
+
49
+ def remove_space(text)
50
+ text.gsub(/\s+/, '-')
51
+ end
52
+
53
+ def remove_punctuation(text)
54
+ text.gsub(/&#39;|&amp;|&quot;|&lt;|&gt;|\/|/, '')
55
+ end
56
+
57
+ def remove_duplicate_dash(text)
58
+ while text.index('--')
59
+ text.gsub!('--', '-')
60
+ end
61
+ text
62
+ end
63
+
64
+ module ClassMethods
65
+
66
+ def chinese_permalink(attr_names, options = {})
67
+ options = {:permalink_field => 'permalink'}.merge(options)
68
+ self.permalink_attrs = Array(attr_names)
69
+ self.permalink_field = options[:permalink_field]
70
+ self.before_methods = Array(options[:before_methods])
71
+ self.after_methods = Array(options[:after_methods])
72
+
73
+ after_save :create_permalink
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,113 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+
6
+ require File.join(File.dirname(__FILE__), '../lib/chinese_permalink')
7
+ require File.join(File.dirname(__FILE__), '../init')
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ :adapter => 'sqlite3',
11
+ :database => ':memory:'
12
+ )
13
+
14
+ def setup_db
15
+ ActiveRecord::Migration.verbose = false
16
+
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table :posts do |t|
19
+ t.string :title
20
+ t.string :category
21
+ t.string :permalink
22
+ end
23
+ end
24
+ end
25
+
26
+ def teardown_db
27
+ ActiveRecord::Base.connection.tables.each do |table|
28
+ ActiveRecord::Base.connection.drop_table(table)
29
+ end
30
+ end
31
+
32
+ class Post < ActiveRecord::Base
33
+ chinese_permalink :title
34
+ end
35
+
36
+ class CategoryPost < Post
37
+ chinese_permalink [:category, :title]
38
+ end
39
+
40
+ class ComplicatedBeforePost < Post
41
+ chinese_permalink :title, :before_methods => :parse_c_sharp
42
+
43
+ def parse_c_sharp(permalink)
44
+ permalink.gsub('C#', 'c-sharp')
45
+ end
46
+ end
47
+
48
+ class ComplicatedAfterPost < Post
49
+ chinese_permalink :title, :after_methods => :parse_pg
50
+
51
+ def parse_pg(permalink)
52
+ permalink.gsub('Procter &amp; Gamble', 'pg')
53
+ end
54
+ end
55
+
56
+ class ChinesePermalinkTest < Test::Unit::TestCase
57
+ def setup
58
+ setup_db
59
+ end
60
+
61
+ def teardown
62
+ teardown_db
63
+ end
64
+
65
+ def test_simple_chinese_title
66
+ post = Post.create(:title => '中国人')
67
+ assert_equal "#{post.id}-chinese", post.permalink
68
+
69
+ post = Post.create(:title => '我是中国人')
70
+ assert_equal "#{post.id}-i-am-a-chinese", post.permalink
71
+ end
72
+
73
+ def test_chinese_title_with_dash
74
+ post = Post.create(:title => '我是中国人——上海')
75
+ assert_equal "#{post.id}-i-am-a-chinese-shanghai", post.permalink
76
+
77
+ post = Post.create(:title => '我是中国人──上海')
78
+ assert_equal "#{post.id}-i-am-a-chinese-shanghai", post.permalink
79
+
80
+ post = Post.create(:title => '上海+中国')
81
+ assert_equal "#{post.id}-shanghai-china", post.permalink
82
+
83
+ post = Post.create(:title => '上海/中国')
84
+ assert_equal "#{post.id}-shanghai-china", post.permalink
85
+
86
+ post = Post.create(:title => '“工作”')
87
+ assert_equal "#{post.id}-work", post.permalink
88
+
89
+ post = Post.create(:title => '妈妈的礼物')
90
+ assert_equal "#{post.id}-moms-gift", post.permalink
91
+
92
+ post = Post.create(:title => '宝洁')
93
+ assert_equal "#{post.id}-procter-gamble", post.permalink
94
+
95
+ post = Post.create(:title => '自我介绍')
96
+ assert_equal "#{post.id}-self-introduction", post.permalink
97
+ end
98
+
99
+ def test_chinese_category_and_title
100
+ post = CategoryPost.create(:title => '我是中国人', :category => '介绍')
101
+ assert_equal "#{post.id}-introduction-i-am-a-chinese", post.permalink
102
+ end
103
+
104
+ def test_complicated_title_with_before_methods
105
+ post = ComplicatedBeforePost.create(:title => 'C#语言')
106
+ assert_equal "#{post.id}-c-sharp-language", post.permalink
107
+ end
108
+
109
+ def test_complicated_title_with_after_methods
110
+ post = ComplicatedAfterPost.create(:title => '宝洁')
111
+ assert_equal "#{post.id}-pg", post.permalink
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_permalink
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-24 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sishen-rtranslate
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 plugin adds a capability for AR model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.
26
+ email: flyerhzm@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.textile
33
+ files:
34
+ - .autotest
35
+ - MIT-LICENSE
36
+ - README.textile
37
+ - Rakefile
38
+ - VERSION
39
+ - generators/chinese_permalink_migration/chinese_permalink_migration_generator.rb
40
+ - generators/chinese_permalink_migration/templates/migration.rb
41
+ - init.rb
42
+ - lib/chinese_permalink.rb
43
+ - test/chines_permalink_test.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/flyerhzm/chinese_permalink
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: This plugin adds a capability for AR model to create a seo permalink with your chinese text.
72
+ test_files:
73
+ - test/chines_permalink_test.rb