public
Fork of caring/acts_as_url_param
Description: Pretty url support for Ruby on Rails applications
Homepage: http://www.caring.com
Clone URL: git://github.com/joshuabates/acts_as_url_param.git
Search Repo:
Add acts_as_url_param plugin
joshuabates (author)
Wed Dec 19 11:01:22 -0800 2007
commit  e5ccb81aa561c44362034ccaa3b4167e90550e0d
tree    706eecb3719965aca61c6ed3cc114f6935ad45f5
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
0
@@ -0,0 +1,65 @@
0
+ActsAsUrlParam
0
+==============
0
+
0
+This plugin allows restful resources to be automatically exposed with human readable urls
0
+based on another method on the resource.
0
+
0
+The URL parameter value that is computed will be unique within the model. If the model is
0
+using inheritance, it's recommended to define acts_as_url_param on the base class.
0
+
0
+Defines one public class methods on your model:
0
+MyResource.url_param_available?(candidate,id=nil)
0
+# => true
0
+# if the candidate value does not exist for the model
0
+
0
+Defines one public instance method on your model:
0
+my_resource.compute_url_param
0
+# => "unique-url-parameter-2"
0
+
0
+Contributors
0
+============
0
+"Chris Eppstein"<chris@eppsteins.net>
0
+"Joshua Bates"<joshuabates@gmail.com>
0
+
0
+Example
0
+=======
0
+
0
+# use the url_param column as the model's url. It will be set and/or modified whenever the default method changes.
0
+# The default methods, in order of precedence, are: :name, :label, :title
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param
0
+end
0
+
0
+# use the url_name column as the model's url. It will be set and/or modified whenever title changes
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param :url_name, :from => :title
0
+end
0
+
0
+# use the url_param column as the model's url. It will be set and/or modified whenever title changes
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param :from => :title
0
+end
0
+
0
+# use the url_param column as the model's url. It will be set and/or modified whenever the :on event occurs.
0
+# Allowed events are: :create, :save, :update. Default is :create so that your permalinks stay permanent.
0
+# May also be a Proc that accepts the model instance and returns true if url_param should change.
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param :from => :title, :on => :create
0
+end
0
+
0
+# use the url_name column as the model's url. It will be set and/or modified whenever the :on event(s) occur.
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param :url_name, :from => :title, :on => :create
0
+end
0
+
0
+# Passing a block allows you to approve candidate url parameters.
0
+# This enables definition of a url space across that spans several models. This is easiest if all
0
+# models are acts_as_url_param.
0
+class MyResource < ActiveRecord::Base
0
+ acts_as_url_param :url_name, :from => :title do |candidate|
0
+ MyResource.url_param_available?(candidate,self.id) &&
0
+ MyOtherResource.url_param_available?(candidate)
0
+ end
0
+end
0
+
0
+Copyright (c) 2007 Chris Eppstein, released under the MIT license
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -0,0 +1,22 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/rdoctask'
0
+
0
+desc 'Default: run unit tests.'
0
+task :default => :test
0
+
0
+desc 'Test the acts_as_url_param plugin.'
0
+Rake::TestTask.new(:test) do |t|
0
+ t.libs << 'lib'
0
+ t.pattern = 'test/**/*_test.rb'
0
+ t.verbose = true
0
+end
0
+
0
+desc 'Generate documentation for the acts_as_url_param plugin.'
0
+Rake::RDocTask.new(:rdoc) do |rdoc|
0
+ rdoc.rdoc_dir = 'rdoc'
0
+ rdoc.title = 'ActsAsUrlParam'
0
+ rdoc.options << '--line-numbers' << '--inline-source'
0
+ rdoc.rdoc_files.include('README')
0
+ rdoc.rdoc_files.include('lib/**/*.rb')
0
+end
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+# Include hook code here
0
+require "acts_as_url_param"
0
+require "url_utils"
0
+ActiveRecord::Base.send(:include, ActsAsUrlParam)
0
\ No newline at end of file
...
 
...
1
0
@@ -0,0 +1 @@
0
+# Install hook code here
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
0
@@ -0,0 +1,117 @@
0
+module ActsAsUrlParam
0
+ def self.included(base)
0
+ base.extend ActMethods
0
+ end
0
+
0
+ module ActMethods
0
+
0
+ def acts_as_url_param(*args, &block)
0
+ class_inheritable_accessor :acts_as_url_options, :acts_as_url_param_base
0
+ # No extract options in rails 1.2.x
0
+ options = args.respond_to?(:extract_options!) ? args.extract_options! : extract_options_from_args!(args)
0
+ self.acts_as_url_options = options
0
+ options[:column] = args.first || 'url_name'
0
+ raise ArgumentError, "Column does not exist" unless column_names.include? options[:column].to_s
0
+ options[:from] ||= default_from_column
0
+ raise ArgumentError, "No columns found to use for setting the url_param" unless column_or_method_exists? options[:from]
0
+ options[:on] ||= :create
0
+ options[:block] = block if block_given?
0
+ callback = "before_validation"
0
+ callback += "_on_create" if options[:on] == :create
0
+ send callback, :set_url_param
0
+ extend ClassMethods
0
+ include InstanceMethods
0
+ include Caring::Utilities::UrlUtils
0
+ extend Caring::Utilities::UrlUtils
0
+ validates_presence_of options[:from], :if => :empty_param?
0
+ self.acts_as_url_options = options
0
+ self.class_eval do
0
+ define_method("#{options[:column]}=") do |value|
0
+ write_attribute(options[:column], url_safe(value))
0
+ end
0
+
0
+ alias_method_chain :validate, :unique_url unless method_defined? :validate_without_unique_url
0
+ end
0
+ klass = self
0
+ (class << self; self; end).module_eval do
0
+ define_method(:url_param_available_for_model?) do |*args|
0
+ candidate, id = *args
0
+ conditions = acts_as_url_options[:conditions] + ' AND ' if acts_as_url_options[:conditions]
0
+ conditions ||= ''
0
+ conditions += "#{acts_as_url_options[:column]} = ?"
0
+ conditions += " AND id != ?" if id
0
+ conditions = [conditions, candidate]
0
+ conditions << id if id
0
+ if descends_from_active_record? or self == klass
0
+ count(:conditions => conditions) == 0
0
+ else
0
+ base_class.count(:conditions => conditions) == 0
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
+ private
0
+ def default_from_column
0
+ %W(name label title).detect do |column_name|
0
+ column_or_method_exists?(column_name) and self.acts_as_url_options[:to].to_s != column_name
0
+ end
0
+ end
0
+
0
+ def column_or_method_exists?(name)
0
+ column_names.include? name.to_s or method_defined? name
0
+ end
0
+
0
+ module ClassMethods
0
+ def url_param_available?(candidate, id=nil)
0
+ if proc = acts_as_url_options[:block]
0
+ !(proc.arity == 1 ? proc.call(candidate) : proc.call(candidate, id))
0
+ else
0
+ url_param_available_for_model?(candidate, id)
0
+ end
0
+ end
0
+
0
+ def compute_url_param(candidate)
0
+ return if candidate.blank?
0
+ # raise ArgumentError, "The url canidate cannot be empty" if candidate.blank?
0
+ uniquify_proc = acts_as_url_options[:block] || Proc.new { |candidate| url_param_available? candidate }
0
+ uniquify(url_safe(candidate), &uniquify_proc)
0
+ end
0
+ end
0
+
0
+ module InstanceMethods
0
+ def compute_url_param
0
+ # raise ArgumentError, "The column used for generating the url_param is empty" unless url_from
0
+ self.class.compute_url_param(url_from)
0
+ end
0
+
0
+ def url_from
0
+ self.class.method_defined?(acts_as_url_options[:from]) ? send(acts_as_url_options[:from]) : read_attribute(acts_as_url_options[:from])
0
+ end
0
+
0
+ def to_param
0
+ read_attribute acts_as_url_options[:column]
0
+ end
0
+
0
+ def empty_param?
0
+ !to_param
0
+ end
0
+
0
+ private
0
+
0
+ def set_url_param
0
+ write_attribute(acts_as_url_options[:column], compute_url_param) unless !read_attribute(acts_as_url_options[:column]).blank? and acts_as_url_options[:on] == :create
0
+ @url_param_validated = true
0
+ end
0
+
0
+ def validate_with_unique_url
0
+ return true if @url_param_validated
0
+ avail_id = new_record? ? nil : id
0
+ unless self.class.url_param_available? to_param, avail_id
0
+ errors.add_to_base "The url is not unique"
0
+ end
0
+ validate_without_unique_url
0
+ end
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
0
@@ -0,0 +1,68 @@
0
+require 'generator'
0
+
0
+module Caring
0
+ module Utilities
0
+ module UrlUtils
0
+ # Makes a string safe for urls
0
+ # Options:
0
+ # :replacements - a Hash of a replacement string to a regex that should match for replacement
0
+ # :char - when :replacements is not provided, this is the string that will be used to replace unsafe characters. defaults to '-'
0
+ # :collapse - set to false if multiple, consecutive unsafe characters should not be replaced with only a single instance of :char.
0
+ # defaults to true. only matters if you don't provide :replacements. If you do, you'll need to write your regex such
0
+ # that it matches multiple characters.
0
+ # :strip_endings - remove replacement characters from the beginning and end of the safe string. Defaults to true.
0
+ # :downcase - make the input string lower case. Defautls to true.
0
+ def url_safe(s, options = {})
0
+ s = s.downcase if options.fetch(:downcase, true)
0
+ collapse = options.fetch(:collapse, true)
0
+ default_regex = /[^'a-zA-Z0-9-]#{"+" if collapse}/
0
+ replacements = options[:replacements] || { options.fetch(:char,"-") => default_regex, "" => /'#{"+" if collapse}/}
0
+ replacements.each do |replacement, regex|
0
+ s = s.gsub(regex,replacement)
0
+ end
0
+ if options.fetch(:strip_endings,true)
0
+ replacement_strings = replacements.keys.map{|k| "(#{Regexp.escape(k)})" unless k.blank?}.compact.join("|")
0
+ s = s.gsub(/(^#{replacement_strings})|(#{replacement_strings}$)/,"")
0
+ end
0
+ return s
0
+ end
0
+
0
+ # Generate integers
0
+ # Options:
0
+ # :start => 1, The integer to start with
0
+ # :end => nil, the last integer to generate, when nil this becomes an infinite sequence
0
+ # :increment => 1, the amount to add for each iteration
0
+ def int_generator(options = {})
0
+ start = options.fetch(:start,1)
0
+ last = options[:end]
0
+ increment = options.fetch(:increment, 1)
0
+ raise ArgumentError if increment == 0
0
+ raise ArgumentError if last && (increment > 0 && start > last) || (increment < 0 && start < last)
0
+ Generator.new do |g|
0
+ i = start
0
+ loop do
0
+ g.yield i
0
+ return if !last.nil? && (increment > 0 && i >= last) || (increment < 0 && i <= last)
0
+ i = i + increment
0
+ end
0
+ end
0
+ end
0
+
0
+ # accepts a block that will be passed a candidate string and should return true if it is unique.
0
+ # Options:
0
+ # => :separator => "-", a string that will be injected between the base string and the uniqifier
0
+ # => :endings => generator, a Generator that provides endings to be placed at the end of the base.
0
+ # defaults to the set of positive integers.
0
+ def uniquify(base, options = {})
0
+ sep = options.fetch(:separator, "-")
0
+ endings = options[:endings] || int_generator
0
+ return base if yield base
0
+ while endings.next? do
0
+ candidate = base+sep+endings.next.to_s
0
+ return candidate if yield candidate
0
+ end
0
+ raise ArgumentError.new("No unique construction found for \"#{base}\"")
0
+ end
0
+ end
0
+ end
0
+end
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+# desc "Explaining what the task does"
0
+# task :acts_as_url_param do
0
+# # Task goes here
0
+# end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
0
@@ -0,0 +1,121 @@
0
+require File.expand_path(File.dirname(__FILE__) + "/test_helper")
0
+require "mocha"
0
+
0
+require "acts_as_url_param_base"
0
+require "author"
0
+require "blog_post"
0
+require "item"
0
+require "book"
0
+require "magazine"
0
+require "newspaper"
0
+require "user"
0
+require "story"
0
+
0
+class ActsAsUrlParamTest < Test::Unit::TestCase
0
+
0
+ def test_should_set_url_name_on_create
0
+ assert !ActsAsUrlParam::Item.create(:name => 'test a url param').url_name.blank?
0
+ end
0
+
0
+ def test_should_not_set_url_name_if_already_set
0
+ item = ActsAsUrlParam::Item.create(:name => 'test a url param', :url_name => 'test-this')
0
+ assert_equal('test-this', item.to_param)
0
+ end
0
+
0
+ def test_should_make_custom_urls_safe
0
+ ActsAsUrlParam::Item.any_instance.expects(:url_safe)
0
+ ActsAsUrlParam::Item.new(:url_name => 'make me safe')
0
+ end
0
+
0
+ def test_should_use_correct_column_to_create_url_name
0
+ assert ActsAsUrlParam::User.create(:name => 'john doe', :login => 'jdog').url_name =~ /jdog/
0
+ end
0
+
0
+ def test_should_be_invalid_without_content_to_create_url
0
+ user = ActsAsUrlParam::User.create(:name => 'john doe')
0
+ assert !user.valid?
0
+ end
0
+
0
+ def test_should_raise_error_without_from_column
0
+ assert_raises ArgumentError do
0
+ acts_as_url_name_model(nil, :from => :no_name)
0
+ end
0
+ end
0
+
0
+ def test_should_raise_error_without_url_name_column
0
+ assert_raises ArgumentError do
0
+ acts_as_url_name_model(:no_url_name)
0
+ end
0
+ end
0
+
0
+ def test_should_check_if_url_param_available
0
+ ActsAsUrlParam::User.create(:login => 'tester')
0
+ assert !ActsAsUrlParam::User.url_param_available?('tester')
0
+ assert ActsAsUrlParam::User.url_param_available?('goodman')
0
+ end
0
+
0
+ def test_should_use_block_to_check_if_url_param_available
0
+ ActsAsUrlParam::Story.create(:title => 'new post')
0
+ assert !ActsAsUrlParam::BlogPost.url_param_available?('new post')
0
+ end
0
+
0
+ def test_should_use_block_to_compute_url_name
0
+ post = ActsAsUrlParam::BlogPost.new(:title => 'post')
0
+ assert_equal 'post', post.compute_url_param
0
+ story = ActsAsUrlParam::Story.create(:title => 'new post')
0
+ new_post = ActsAsUrlParam::BlogPost.new(:title => 'new post')
0
+ assert_not_equal new_post.compute_url_param, story.to_param
0
+ end
0
+
0
+ def test_should_compute_url_name
0
+ name = 'this is a url param'
0
+ item = ActsAsUrlParam::Item.new(:name => name)
0
+ assert !ActsAsUrlParam::Item.compute_url_param(name).blank?
0
+ assert_equal(item.compute_url_param, ActsAsUrlParam::Item.compute_url_param(name))
0
+ end
0
+
0
+ def test_should_update_url_name_on_custom_callback
0
+ author = ActsAsUrlParam::Author.create(:label => 'name of author')
0
+ author_url = author.to_param
0
+ author.update_attributes(:label => 'a new author')
0
+ assert_not_equal(author_url, author.to_param)
0
+ end
0
+
0
+ def test_should_not_update_url_name_by_default
0
+ item = ActsAsUrlParam::Item.create(:name => 'this is a url param')
0
+ item_url = item.to_param
0
+ item.update_attributes(:name => 'not updated')
0
+ assert_equal(item_url, item.to_param)
0
+ end
0
+
0
+ def test_should_work_with_sti
0
+ item = ActsAsUrlParam::Item.create(:name => 'an item')
0
+ book = ActsAsUrlParam::Book.create(:name => 'an item')
0
+ newspaper = ActsAsUrlParam::Newspaper.create(:name => 'an item')
0
+ assert_not_equal(item.to_param, book.to_param)
0
+ assert_equal(item.to_param, newspaper.to_param)
0
+
0
+ newspaper = ActsAsUrlParam::Newspaper.create(:name => 'another item')
0
+ book = ActsAsUrlParam::Book.create(:name => 'another item')
0
+ assert_equal(book.to_param, newspaper.to_param)
0
+ end
0
+
0
+ def test_should_use_method_for_url_from_if_exists
0
+ magazine = ActsAsUrlParam::Magazine.create()
0
+ assert !magazine.to_param.blank?
0
+ end
0
+
0
+ private
0
+ def acts_as_url_name_model(column = nil, options = {})
0
+ m = Class.new(ActiveRecord::Base)
0
+ m.class_eval do
0
+ set_table_name :items
0
+ if column
0
+ acts_as_url_param column, options
0
+ else
0
+ acts_as_url_param options
0
+ end
0
+ end
0
+ m
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+sqlite3:
0
+ :adapter: sqlite3
0
+ :dbfile: ":memory:"
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
0
@@ -0,0 +1,8 @@
0
+# Namespace models
0
+module ActsAsUrlParam
0
+end
0
+
0
+class ActsAsUrlParamBase < ActiveRecord::Base
0
+ self.abstract_class = true
0
+ self.connection = ACTS_AS_URL_PARAM_TEST_DB
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class ActsAsUrlParam::Author < ActsAsUrlParamBase
0
+ acts_as_url_param :on => :update
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,6 @@
0
+class ActsAsUrlParam::BlogPost < ActsAsUrlParamBase
0
+ acts_as_url_param do |candidate|
0
+ url_param_available_for_model?(candidate) &&
0
+ Story.url_param_available?(candidate)
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
0
...
1
2
3
0
@@ -0,0 +1,2 @@
0
+class ActsAsUrlParam::Book < ActsAsUrlParam::Item
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class ActsAsUrlParam::Item < ActsAsUrlParamBase
0
+ acts_as_url_param :conditions => "items.type != 'Newspaper'"
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
0
...
1
2
3
4
5
6
0
@@ -0,0 +1,5 @@
0
+class ActsAsUrlParam::Magazine < ActsAsUrlParam::Item
0
+ def name
0
+ "The magazine"
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class ActsAsUrlParam::Newspaper < ActsAsUrlParam::Item
0
+ acts_as_url_param
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class ActsAsUrlParam::Story < ActsAsUrlParamBase
0
+ acts_as_url_param :story_url
0
+end
0
\ No newline at end of file
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class ActsAsUrlParam::User < ActsAsUrlParamBase
0
+ acts_as_url_param :from => :login
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -0,0 +1,28 @@
0
+ActiveRecord::Schema.define(:version => 1) do
0
+ create_table :authors do |t|
0
+ t.column :label, :string
0
+ t.column :url_name, :string
0
+ end
0
+
0
+ create_table :blog_posts do |t|
0
+ t.column :title, :string
0
+ t.column :url_name, :string
0
+ end
0
+
0
+ create_table :items do |t|
0
+ t.column :name, :string
0
+ t.column :type, :string, :default => 'Item', :null => false
0
+ t.column :url_name, :string
0
+ end
0
+
0
+ create_table :stories do |t|
0
+ t.column :title, :string
0
+ t.column :story_url, :string
0
+ end
0
+
0
+ create_table :users do |t|
0
+ t.column :name, :string
0
+ t.column :login, :string
0
+ t.column :url_name, :string
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
0
@@ -0,0 +1,54 @@
0
+# Include this file in your test by copying the following line to your test:
0
+# require File.expand_path(File.dirname(__FILE__) + "/test_helper")
0
+
0
+require 'test/unit'
0
+rails_env = File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
+if File.exist? rails_env
0
+ require rails_env
0
+ require 'active_record/fixtures'
0
+else
0
+ require 'rubygems'
0
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
0
+ RAILS_ROOT = File.dirname(__FILE__)
0
+ require 'active_record'
0
+ require 'active_record/fixtures'
0
+ require "#{File.dirname(__FILE__)}/../init"
0
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
0
+end
0
+
0
+Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
0
+$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
0
+
0
+load_schema = Proc.new do
0
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
0
+ schema = File.dirname(__FILE__) + "/schema.rb"
0
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
0
+ load(schema) if File.exist?(schema)
0
+ ACTS_AS_URL_PARAM_TEST_DB = ActiveRecord::Base.connection
0
+end
0
+
0
+keep_connection_and_load_schema = Proc.new do
0
+ old_connection = ActiveRecord::Base.connection
0
+ load_schema.call
0
+ ActiveRecord::Base.connection = old_connection
0
+end
0
+
0
+ActiveRecord::Base.connected? ? keep_connection_and_load_schema.call : load_schema.call
0
+
0
+class Test::Unit::TestCase #:nodoc:
0
+ def create_fixtures(*table_names)
0
+ if block_given?
0
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
0
+ else
0
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
0
+ end
0
+ end
0
+
0
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
0
+ self.use_transactional_fixtures = true
0
+
0
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
0
+ self.use_instantiated_fixtures = false
0
+
0
+ # Add more helper methods to be used by all tests here...
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,23 @@
0
+require File.expand_path(File.dirname(__FILE__) + "/test_helper")
0
+
0
+class UrlUtilsTest < Test::Unit::TestCase
0
+ include Caring::Utilities::UrlUtils
0
+
0
+ def test_url_safe
0
+ assert_equal "a1-b-z", url_safe("a1 b@ _Z!")
0
+ assert_equal 'a1#b#_Z', url_safe('a1 b@ _Z', :replacements => { '#', /[^a-zA-Z0-9_-]+/}, :downcase => false)
0
+ assert_equal 'a1-b2---z9', url_safe('a1 b2 z9', :collapse => false)
0
+ assert_equal 'a1_b2___z9', url_safe('a1 b2 z9', :char => '_', :collapse => false)
0
+ end
0
+
0
+ def test_uniquify
0
+ assert_equal "asdf", uniquify("asdf") { |candidate| true }
0
+ i = 0
0
+ assert_equal "asdf-1", uniquify("asdf") { |candidate| (i = i+1) == 2 }
0
+ i = 0
0
+ assert_equal "asdf-2", uniquify("asdf") { |candidate| (i = i+1) == 3 }
0
+ assert_raises ArgumentError do
0
+ uniquify("asdf", :endings => Generator.new(%w(a b c))) {|c| false}
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
 
...
1
0
@@ -0,0 +1 @@
0
+# Uninstall hook code here

Comments

    No one has commented yet.