permalink 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,30 @@
1
- has_permalink
2
- =============
1
+ Permalink
2
+ =========
3
3
 
4
4
  Instalation
5
5
  -----------
6
6
 
7
- You can use I18n-JS as plugin and gem. Choose what's best for you!
8
-
9
- script/plugin install git://github.com/fnando/has_permalink.git
10
-
11
- or
12
7
  gem install permalink
13
8
 
14
9
  Usage
15
10
  -----
16
11
 
17
- Add the method call `has_permalink` to your model. Your model should have a `permalink` attribute.
12
+ Add the method call `permalink` to your model. Your model should have a `permalink` attribute.
18
13
 
19
14
  class Page < ActiveRecord::Base
20
- has_permalink :title
15
+ permalink :title
21
16
  end
22
17
 
23
18
  You can specify the permalink field:
24
19
 
25
20
  class page < ActiveRecord::Base
26
- has_permalink :title, :to => :title_permalink
21
+ permalink :title, :to => :title_permalink
27
22
  end
28
23
 
29
- If you don't want to use `has_permalink`, you can call `'some text'.to_permalink` string method and
24
+ If you don't want to use `permalink`, you can call `'some text'.to_permalink` string method and
30
25
  manage the permalink process by yourself.
31
26
 
32
- Permalinks are not unique by default. `has_permalink` overrides `to_param` as following:
27
+ Permalinks are not unique by default. `permalink` overrides `to_param` as following:
33
28
 
34
29
  def to_param
35
30
  "#{id}-#{permalink}"
@@ -38,15 +33,15 @@ Permalinks are not unique by default. `has_permalink` overrides `to_param` as fo
38
33
  You can define the `to_param` format:
39
34
 
40
35
  class Page < ActiveRecord::Base
41
- has_permalink :title, :to_param => %w(id permalink page)
36
+ permalink :title, :to_param => %w(id permalink page)
42
37
  end
43
38
 
44
39
  The above settings will generate something link `100-some-title-page`. By overriding `to_param` method you don't have to change a thing on your app routes.
45
40
 
46
- If you want to generate unique permalink, use the option `:unique`:
41
+ If you want to generate unique permalink, use the `:unique` option:
47
42
 
48
43
  class Page < ActiveRecord::Base
49
- has_permalink :title, :unique => true, :to_param => :permalink
44
+ permalink :title, :unique => true, :to_param => :permalink
50
45
  end
51
46
 
52
47
  The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this means that characters will properly replaced from `áéíó` to `aeio`, for instance.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + "/lib/permalink/version"
4
4
  desc "Default: run unit tests."
5
5
  task :default => :test
6
6
 
7
- desc "Test the i18n-js plugin."
7
+ desc "Run tests"
8
8
  Rake::TestTask.new(:test) do |t|
9
9
  t.libs << "lib"
10
10
  t.libs << "test"
@@ -18,11 +18,11 @@ begin
18
18
  JEWEL = Jeweler::Tasks.new do |gem|
19
19
  gem.name = "permalink"
20
20
  gem.email = "fnando.vieira@gmail.com"
21
- gem.homepage = "http://github.com/fnando/has_permalink"
21
+ gem.homepage = "http://github.com/fnando/permalink"
22
22
  gem.authors = ["Nando Vieira"]
23
- gem.version = SimplesIdeias::Permalink::Version::STRING
23
+ gem.version = Permalink::Version::STRING
24
24
  gem.summary = "ActiveRecord plugin for automatically converting fields to permalinks."
25
- gem.files = FileList["README.markdown", "init.rb", "{lib,test}/**/*", "Rakefile"]
25
+ gem.files = FileList["README.markdown", "{lib,test}/**/*", "Rakefile"]
26
26
  end
27
27
 
28
28
  Jeweler::GemcutterTasks.new
@@ -1,94 +1,92 @@
1
1
  require "permalink/string_ext"
2
2
  require "active_record"
3
3
 
4
- module SimplesIdeias
5
- module Permalink
6
- def self.included(base)
7
- base.extend ClassMethods
4
+ module Permalink
5
+ def self.included(base)
6
+ base.extend ClassMethods
8
7
 
9
- class << base
10
- attr_accessor :has_permalink_options
11
- end
8
+ class << base
9
+ attr_accessor :permalink_options
12
10
  end
11
+ end
13
12
 
14
- module ClassMethods
15
- # has_permalink :title
16
- # has_permalink :title, :to => :custom_permalink_field
17
- # has_permalink :title, :to => :permalink, :to_param => [:id, :permalink]
18
- # has_permalink :title, :unique => true
19
- def has_permalink(from, options={})
20
- options = {
21
- :to => :permalink,
22
- :to_param => [:id, :permalink],
23
- :unique => false
24
- }.merge(options)
25
-
26
- self.has_permalink_options = {
27
- :from_column_name => from,
28
- :to_column_name => options[:to],
29
- :to_param => [options[:to_param]].flatten,
30
- :unique => options[:unique]
31
- }
32
-
33
- include SimplesIdeias::Permalink::InstanceMethods
34
-
35
- before_validation :create_permalink
36
- before_save :create_permalink
37
- end
13
+ module ClassMethods
14
+ # permalink :title
15
+ # permalink :title, :to => :custom_permalink_field
16
+ # permalink :title, :to => :permalink, :to_param => [:id, :permalink]
17
+ # permalink :title, :unique => true
18
+ def permalink(from, options={})
19
+ options = {
20
+ :to => :permalink,
21
+ :to_param => [:id, :permalink],
22
+ :unique => false
23
+ }.merge(options)
24
+
25
+ self.permalink_options = {
26
+ :from_column_name => from,
27
+ :to_column_name => options[:to],
28
+ :to_param => [options[:to_param]].flatten,
29
+ :unique => options[:unique]
30
+ }
31
+
32
+ include InstanceMethods
33
+
34
+ before_validation :create_permalink
35
+ before_save :create_permalink
38
36
  end
37
+ end
39
38
 
40
- module InstanceMethods
41
- def to_param
42
- to_param_option = self.class.has_permalink_options[:to_param]
43
-
44
- to_param_option.compact.collect do |name|
45
- if respond_to?(name)
46
- send(name).to_s
47
- else
48
- name.to_s
49
- end
50
- end.reject(&:blank?).join('-')
51
- end
39
+ module InstanceMethods
40
+ def to_param
41
+ to_param_option = self.class.permalink_options[:to_param]
52
42
 
53
- private
54
- def next_available_permalink(_permalink)
55
- the_permalink = _permalink
43
+ to_param_option.compact.collect do |name|
44
+ if respond_to?(name)
45
+ send(name).to_s
46
+ else
47
+ name.to_s
48
+ end
49
+ end.reject(&:blank?).join('-')
50
+ end
56
51
 
57
- if self.class.has_permalink_options[:unique]
58
- suffix = 2
52
+ private
53
+ def next_available_permalink(_permalink)
54
+ the_permalink = _permalink
59
55
 
60
- while self.class.first(:conditions => {to_permalink_name => the_permalink}, :select => self.class.primary_key)
61
- the_permalink = "#{_permalink}-#{suffix}"
62
- suffix += 1
63
- end
64
- end
56
+ if self.class.permalink_options[:unique]
57
+ suffix = 2
65
58
 
66
- the_permalink
59
+ while self.class.first(:conditions => {to_permalink_name => the_permalink}, :select => self.class.primary_key)
60
+ the_permalink = "#{_permalink}-#{suffix}"
61
+ suffix += 1
62
+ end
67
63
  end
68
64
 
69
- def from_permalink_name
70
- self.class.has_permalink_options[:from_column_name]
71
- end
65
+ the_permalink
66
+ end
72
67
 
73
- def to_permalink_name
74
- self.class.has_permalink_options[:to_column_name]
75
- end
68
+ def from_permalink_name
69
+ self.class.permalink_options[:from_column_name]
70
+ end
76
71
 
77
- def from_permalink_value
78
- read_attribute(from_permalink_name)
79
- end
72
+ def to_permalink_name
73
+ self.class.permalink_options[:to_column_name]
74
+ end
80
75
 
81
- def to_permalink_value
82
- read_attribute(to_permalink_name)
83
- end
76
+ def from_permalink_value
77
+ read_attribute(from_permalink_name)
78
+ end
84
79
 
85
- def create_permalink
86
- unless from_permalink_value.blank? || !to_permalink_value.blank?
87
- write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink))
88
- end
80
+ def to_permalink_value
81
+ read_attribute(to_permalink_name)
82
+ end
83
+
84
+ def create_permalink
85
+ unless from_permalink_value.blank? || !to_permalink_value.blank?
86
+ write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink))
89
87
  end
90
88
  end
91
89
  end
92
90
  end
93
91
 
94
- ActiveRecord::Base.send(:include, SimplesIdeias::Permalink)
92
+ ActiveRecord::Base.send(:include, Permalink)
@@ -1,10 +1,8 @@
1
- module SimplesIdeias
2
- module Permalink
3
- module Version
4
- MAJOR = 0
5
- MINOR = 1
6
- PATCH = 1
7
- STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
- end
1
+ module Permalink
2
+ module Version
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
9
7
  end
10
8
  end
@@ -1,3 +1,3 @@
1
1
  class Beer < ActiveRecord::Base
2
- has_permalink :name
2
+ permalink :name
3
3
  end
@@ -1,3 +1,3 @@
1
1
  class Donut < ActiveRecord::Base
2
- has_permalink :flavor, :to => :slug, :to_param => [:slug, :id, 'permalink']
2
+ permalink :flavor, :to => :slug, :to_param => [:slug, :id, 'permalink']
3
3
  end
@@ -1,5 +1,5 @@
1
1
  class Post < ActiveRecord::Base
2
- has_permalink :title, :unique => true, :to_param => :permalink
3
-
2
+ permalink :title, :unique => true, :to_param => :permalink
3
+
4
4
  validates_uniqueness_of :permalink
5
5
  end
@@ -1,3 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
- has_permalink :name, :to => :permalink, :to_param => [:id, " ", nil, "\t", :permalink]
2
+ permalink :name, :to => :permalink, :to_param => [:id, " ", nil, "\t", :permalink]
3
3
  end
@@ -6,5 +6,5 @@ require "active_support/test_case"
6
6
  require "permalink"
7
7
 
8
8
  Dir.glob(File.dirname(__FILE__) + "/models/*.rb").each {|r| require r }
9
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
10
- load('schema.rb')
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
+ load("schema.rb")
metadata CHANGED
@@ -3,10 +3,10 @@ name: permalink
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 0
7
- - 1
8
6
  - 1
9
- version: 0.1.1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-09 00:00:00 -03:00
17
+ date: 2010-10-06 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,7 +29,6 @@ extra_rdoc_files:
29
29
  files:
30
30
  - README.markdown
31
31
  - Rakefile
32
- - init.rb
33
32
  - lib/permalink.rb
34
33
  - lib/permalink/string_ext.rb
35
34
  - lib/permalink/version.rb
@@ -41,7 +40,7 @@ files:
41
40
  - test/schema.rb
42
41
  - test/test_helper.rb
43
42
  has_rdoc: true
44
- homepage: http://github.com/fnando/has_permalink
43
+ homepage: http://github.com/fnando/permalink
45
44
  licenses: []
46
45
 
47
46
  post_install_message:
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require "permalink"