acts_as_permalink 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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.markdown ADDED
@@ -0,0 +1,51 @@
1
+ # Acts As Permalink
2
+
3
+ Manages permalink field on an ActiveRecord model to be used in place of the id field in Rails.
4
+
5
+ Written by Kevin McPhillips (kimos) in 2009.
6
+
7
+ Last updated March 2012.
8
+
9
+ [http://github.com/kimos/acts_as_permalink](http://github.com/kimos/acts_as_permalink)
10
+
11
+ [github@kevinmcphillips.ca](mailto:github@kevinmcphillips.ca)
12
+
13
+
14
+ ## Usage
15
+
16
+ The gem is on RubyGems:
17
+
18
+ gem 'acts_as_permalink'
19
+
20
+ And then just call it in your model:
21
+
22
+ class Post < ActiveRecord::Base
23
+ acts_as_permalink
24
+ end
25
+
26
+ That's about it.
27
+ The plugin expects string fields to be used to save the permalink on the model and to use as a source for the permalink. It defaults to use the fields named "title" and "permalink" which can be overridden by options:
28
+
29
+ :from => :title # Name of the active record column or function used to generate the permalink
30
+ :to => :permalink # Name of the column where the permalink will be stored
31
+ :max_length => 60 # Maximum number of characters the permalink will be
32
+
33
+ So, for example you have want to store your permalink in a column called "path_name" and you want to generate your permalink using first and last name, and you want to restrict it to 40 characters, your model would look like:
34
+
35
+ class User < ActiveRecord::Base
36
+ acts_as_permalink :from => :full_name, :to => :path, :max_length => 40
37
+
38
+ def full_name
39
+ first_name + last_name
40
+ end
41
+ end
42
+
43
+
44
+ ## History
45
+
46
+ * May 3, 2012 -- 0.3.1 -- Finally got it gemified.
47
+
48
+ * March 19, 2012 -- Updated to work with Rails 3.2
49
+
50
+ * Feb 13, 2010* -- Fixed collision problem with single table inheritance models. Removed dependency on andand gem.
51
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Acts::Permalink'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,5 @@
1
+ module Acts
2
+ module Permalink
3
+ VERSION = "0.3.1"
4
+ end
5
+ end
@@ -0,0 +1,87 @@
1
+ # ActsAsPermalink
2
+ module Acts #:nodoc:
3
+ module Permalink #:nodoc:
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def acts_as_permalink(options={})
11
+ # Read and scrub option for the column which will save the permalink
12
+ @permalink_column_name = options[:to].try(:to_sym) || :permalink
13
+
14
+ # Read and scrub the option for the column or function which will generate the permalink
15
+ @permalink_source = (options[:from].try(:to_sym) || :title)
16
+
17
+ # Read and validate the maximum length of the permalink
18
+ max_length = options[:max_length].to_i rescue 0
19
+ max_length = 60 unless max_length && max_length > 0
20
+ @permalink_length = max_length
21
+
22
+ if Rails.version >= "3"
23
+ before_validation :update_permalink, :on => :create
24
+ else
25
+ before_validation_on_create :update_permalink
26
+ end
27
+
28
+ validates_uniqueness_of @permalink_column_name
29
+ attr_readonly @permalink_column_name
30
+
31
+ include Acts::Permalink::InstanceMethods
32
+ extend Acts::Permalink::SingletonMethods
33
+ end
34
+
35
+ # Returns the unique permalink string for the passed in object.
36
+ def generate_permalink_for(obj)
37
+
38
+ # Find the source for the permalink
39
+ text = obj.send(obj.class.instance_variable_get('@permalink_source'))
40
+
41
+ # If it is blank then generate a random link
42
+ if text.blank?
43
+ text = obj.class.to_s.downcase + rand(10000).to_s
44
+
45
+ # If it is not blank, scrub
46
+ else
47
+ text = text.downcase.strip # make the string lowercase and scrub white space on either side
48
+ text = text.gsub(/[^a-z0-9\w]/, "_") # make any character that is not nupermic or alphabetic into an underscore
49
+ text = text.sub(/_+$/, "").sub(/^_+/, "") # remove underscores on either end, caused by non-simplified characters
50
+ text = text[0...obj.class.instance_variable_get('@permalink_length')] # trim to length
51
+ end
52
+
53
+ # Attempt to find the object by the permalink
54
+ if obj.class.base_class.send("find_by_#{obj.class.instance_variable_get('@permalink_column_name')}", text)
55
+ num = 1
56
+
57
+ # If we find the object we know there is a collision, so just add a number to the end until there is no collision
58
+ while obj.class.base_class.send("find_by_#{obj.class.instance_variable_get('@permalink_column_name')}", text + num.to_s)
59
+ num += 1
60
+ end
61
+
62
+ text = text + num.to_s
63
+ end
64
+ text
65
+ end
66
+ end
67
+
68
+ module SingletonMethods
69
+ end
70
+
71
+ module InstanceMethods
72
+
73
+ # Override this method so that find searches by permalink and not by id
74
+ def to_param
75
+ self.send(self.class.instance_variable_get('@permalink_column_name'))
76
+ end
77
+
78
+ # Generate the permalink and assign it directly via callback
79
+ def update_permalink
80
+ self.send("#{self.class.instance_variable_get('@permalink_column_name')}=", self.class.generate_permalink_for(self))
81
+ true
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ ActiveRecord::Base.send(:include, Acts::Permalink)
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_permalink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin McPhillips
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70310709515380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70310709515380
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70310709514580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70310709514580
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &70310709530460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70310709530460
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70310709530040 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70310709530040
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &70310709529480 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70310709529480
69
+ description: Manages permalink columns in active record models. Strips special charactes
70
+ and spaces, creates before validation, assures uniqueness. That kind of thing.
71
+ email:
72
+ - github@kevinmcphillips.ca
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - lib/acts_as_permalink/version.rb
78
+ - lib/acts_as_permalink.rb
79
+ - MIT-LICENSE
80
+ - Rakefile
81
+ - README.markdown
82
+ homepage: http://github.com/kimos/acts_as_permalink
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -3998614899780643219
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -3998614899780643219
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.11
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Automatically manage permalink fields for ActiveRecord models in Rails.
112
+ test_files: []