has_friendly_name 0.2.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/README.md ADDED
@@ -0,0 +1,41 @@
1
+ Digineo HasFriendlyName gem
2
+ =========
3
+
4
+ ## Description
5
+ HasFriendlyName adds a search engine friendly name to your active record model.
6
+ It also extends the String class with a "to_friendly" method
7
+
8
+ ## Installation:
9
+ add the following to your config/enviroment.rb
10
+
11
+ config.gem "has_friendly_name"
12
+
13
+
14
+ ## Add friendly_name column to your model
15
+
16
+ ## Examples & usage
17
+
18
+ ### add an unique friendly_name to your model
19
+ If your user model has a name method and you want an unique friendly_name just use:
20
+
21
+ class User < ActiveRecord::Base
22
+ has_friendly_name
23
+ end
24
+
25
+ first record with name *myStylish~Name* gets the following friendly_name: *mystylish-name*
26
+
27
+ the second record with the same name gets the following friendly_name: *mystylish-name-2*
28
+
29
+ ### customize
30
+ HasFriendlyName comes with serveal configuration options:
31
+
32
+ class User < ActiveRecord::Base
33
+ has_friendly_name(:unique => false, :from => :title, :downcase => false, :titelize => true, :seperator => "_")
34
+ end
35
+
36
+ first record with name *myStylish~Name* gets the following friendly_name: *MyStylish_Name*
37
+
38
+ the second record with the same name gets the same because unique is false
39
+
40
+ Copyright (c) 2010 Dennis Meise [Digineo GmbH](http://www.digineo.de/ "Digineo GmbH") , released under the MIT license
41
+
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the has_friendly_name plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the has_friendly_name plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'HasFriendlyName'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Include hook code here
2
+ require 'string_extensions'
3
+ require 'has_friendly_name'
4
+ ActiveRecord::Base.send(:include, HasFriendlyName)
@@ -0,0 +1,38 @@
1
+ # HasFriendlyName
2
+ module HasFriendlyName
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ # adds friendly_name to your model. Options are:
10
+ # * unique => boolean - should the friendly_name be unique for this model? appends count if name has been taken
11
+ # * to_downcase - friendly_name will be downcased
12
+ # * titelize - friendly_name will be titelized
13
+ def has_friendly_name(options={})
14
+ self.cattr_accessor :has_friendly_name_options
15
+ self.has_friendly_name_options = {:unique => true, :titelize => false, :downcase => true, :from => :name}.merge(options)
16
+ before_create :generate_friendly_name
17
+ include HasFriendlyName::InstanceMethods
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+
23
+ def generate_friendly_name
24
+ self.friendly_name = send(self.has_friendly_name_options[:from].to_s.to_sym).to_friendly(self.has_friendly_name_options)
25
+
26
+ if self.has_friendly_name_options[:unique]
27
+ checkmodel=self.class.find_by_friendly_name(self.friendly_name)
28
+ if checkmodel and checkmodel!=self
29
+ self.friendly_name+='-'+(self.class.count+1).to_s
30
+ end
31
+ end
32
+
33
+ self.friendly_name
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,50 @@
1
+ class String
2
+
3
+ def to_friendly(given_options={})
4
+ options = {:seperator => "-", :downcase => true, :titleize => false}.merge(given_options)
5
+ res = self
6
+ res = res.titleize if options[:titleize]
7
+ res = res.gsub(/'/, "").gsub(/\W+/, " ").gsub(/\s\s*/, " ").strip.gsub(/\s/, options[:seperator])
8
+ res.gsub!(/ß/u, 'ss')
9
+ res.gsub!(/[ü]/u, 'ue')
10
+ res.gsub!(/«/, '')
11
+ res.gsub!(/»/, '')
12
+ res.gsub!(/[ä]/u, 'ae')
13
+ res.gsub!(/[ö]/u, 'oe')
14
+ res.gsub!(/ó/, 'o')
15
+ res.gsub!(/Ä/u, 'ae')
16
+ res.gsub!(/Ö/u, 'Oe')
17
+ res.gsub!(/Ü/u, 'Ue')
18
+
19
+ res.gsub!(/é/u, 'e')
20
+ res.gsub!(/è/u, 'e')
21
+ res.gsub!(/ê/u, 'e')
22
+ res.gsub!(/É/u, 'e')
23
+ res.gsub!(/È/u, 'e')
24
+ res.gsub!(/Ê/u, 'e')
25
+
26
+ res.gsub!(/á/u, 'a')
27
+ res.gsub!(/à/u, 'a')
28
+ res.gsub!(/â/u, 'a')
29
+ res.gsub!(/Á/u, 'a')
30
+ res.gsub!(/À/u, 'a')
31
+ res.gsub!(/Â/u, 'A')
32
+
33
+ res.gsub!(/ú/u, 'u')
34
+ res.gsub!(/ù/u, 'u')
35
+ res.gsub!(/û/u, 'u')
36
+ res.gsub!(/Ú/u, 'u')
37
+ res.gsub!(/Ù/u, 'u')
38
+ res.gsub!(/Û/u, 'u')
39
+
40
+ res.gsub!(/í/u, 'i')
41
+ res.gsub!(/ì/u, 'i')
42
+ res.gsub!(/î/u, 'i')
43
+ res.gsub!(/Ì/u, 'i')
44
+ res.gsub!(/Í/u, 'i')
45
+ res.gsub!(/Î/u, 'i')
46
+ res.gsub!(/[^a-z^0-9^-^ ]+/i, options[:seperator])
47
+ options[:downcase] ? res.downcase : res
48
+ end
49
+
50
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "..", "init")
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_friendly_name
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Meise
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-17 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: github@digineo.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - init.rb
26
+ - Rakefile
27
+ - README.md
28
+ - lib/has_friendly_name.rb
29
+ - lib/string_extensions.rb
30
+ - rails/init.rb
31
+ has_rdoc: true
32
+ homepage: http://www.digineo.de
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --inline-source
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: HasFriendlyName adds a search engine friendly name to your active record model.
60
+ test_files: []
61
+