speaking_id 0.1.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/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ 0.1.0 (Jan 15, 2010)
2
+
3
+ * initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Martin Jagusch
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.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = SpeakingId
2
+
3
+ Creates simple and secure slugs from a given string or random slugs to hide a model ID.
4
+
5
+ == Installation
6
+
7
+ You can set it up as a gem in your environment.rb file.
8
+
9
+ config.gem "speaking_id"
10
+
11
+ And then install the gem.
12
+
13
+ sudo rake gems:install
14
+
15
+ Alternatively you can install it as a Rails plugin.
16
+
17
+ script/plugin install git://github.com/venlix/speaking_id.git
18
+
19
+ == Instructions
20
+
21
+ In your models:
22
+
23
+ class User < ActiveRecord::Base
24
+ has_slug :name
25
+ end
26
+
27
+ class Project < ActiveRecord::Base
28
+ has_random_slug
29
+ end
30
+
31
+ In your migrations:
32
+
33
+ class AddSpeakingIdToUserAndProject < ActiveRecord::Migration
34
+ def self.up
35
+ add_column :users, :slug, :string
36
+ add_column :project, :slug, :string
37
+
38
+ add_index :users, :slug, :unique => true
39
+ add_index :project, :slug, :unique => true
40
+ end
41
+
42
+ def self.down
43
+ remove_column :users, :slug
44
+ remove_column :project, :slug
45
+ end
46
+ end
47
+
48
+ By default, SpeakingId uses a column called "slug". If you want to use a different column name, just pass the +:column+ option:
49
+
50
+ has_slug :title, :column => :column_name
51
+
52
+ This works also with the +has_random_slug+ method.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'speaking_id'
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) + '/speaking_id/speaking_id'
2
+
3
+ if defined?(ActiveRecord)
4
+ ActiveRecord::Base.instance_eval {extend SpeakingId::ClassMethods}
5
+ end
@@ -0,0 +1,54 @@
1
+ module SpeakingId
2
+ module ClassMethods
3
+ def has_slug source, args = {}
4
+ include InstanceMethods
5
+
6
+ class_inheritable_accessor :slug_source, :slug_column
7
+
8
+ self.slug_source = source
9
+ self.slug_column = args.has_key?(:column) ? args[:column] : :slug
10
+
11
+ before_save :create_slug
12
+ end
13
+
14
+ def has_random_slug args = {}
15
+ include InstanceMethods
16
+
17
+ class_inheritable_accessor :slug_column
18
+
19
+ self.slug_column = args.has_key?(:column) ? args[:column] : :slug
20
+
21
+ before_save :create_random_slug
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def create_slug
27
+ begin
28
+ self[self.slug_column] = self[self.slug_source].to_s.parameterize("_")
29
+ self[self.slug_column] << ((counter ||= 1) == 1 ? nil : counter).to_s
30
+ counter += 1
31
+ end while slug_taken?
32
+ end
33
+
34
+ def create_random_slug
35
+ begin
36
+ self[self.slug_column] = create_random_token
37
+ end while slug_taken?
38
+ end
39
+
40
+ def to_param
41
+ self[self.slug_column]
42
+ end
43
+
44
+ private
45
+
46
+ def create_random_token limit = 6
47
+ ActiveSupport::SecureRandom.hex(limit).upcase
48
+ end
49
+
50
+ def slug_taken?
51
+ self.class.first :conditions => {self.slug_column.to_sym => self[self.slug_column]}
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SpeakingIdTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: speaking_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Jagusch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-15 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Creates simple and secure slugs from a given string or random slugs to hide a model ID.
17
+ email: m@venlix.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - CHANGELOG.rdoc
25
+ - LICENSE
26
+ files:
27
+ - lib/speaking_id/speaking_id.rb
28
+ - lib/speaking_id.rb
29
+ - test/speaking_id_test.rb
30
+ - test/test_helper.rb
31
+ - LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - CHANGELOG.rdoc
35
+ - init.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/venlix/speaking_id
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --line-numbers
43
+ - --inline-source
44
+ - --charset=UTF-8
45
+ - --title
46
+ - SpeakingId
47
+ - --main
48
+ - README.rdoc
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "1.2"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Simple slugs for ActiveRecord models.
70
+ test_files: []
71
+