ffmike-from_param 1.0 → 1.0.1

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 CHANGED
@@ -35,6 +35,7 @@ Example
35
35
  end
36
36
 
37
37
  # using a 'param' column, in this case 'slug'. Note the use of String.urlize to make the title URL-friendly.
38
+ # On Rails 2.2+, String.urlize is an alias for Rails built-in Inflector.parameterize.
38
39
 
39
40
  class Post < ActiveRecord::Base
40
41
  set_param_column "slug" # defaults to "param"
@@ -47,6 +48,8 @@ Example
47
48
  # GET /posts/2008-04-26-from-param-plugin-released
48
49
  @post = Post.from_param(params[:id]) # => <Post title="From Param: Plugin Released" created_at="2008-04-26">
49
50
  end
51
+
52
+ Note: You will need to have the rspec gem installed to run the tests.
50
53
 
51
54
  Resources
52
55
  =========
data/Rakefile CHANGED
@@ -1,9 +1,16 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
+ require 'spec'
5
+ require 'spec/rake/spectask'
4
6
 
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
+ desc 'Default: run rspec tests.'
8
+ task :default => :spec
9
+
10
+ desc "Run all specs in spec directory "
11
+ Spec::Rake::SpecTask.new(:spec) do |t|
12
+ t.spec_files = FileList['spec/**/*_spec.rb']
13
+ end
7
14
 
8
15
  desc 'Test the from_param plugin.'
9
16
  Rake::TestTask.new(:test) do |t|
data/lib/from_param.rb CHANGED
@@ -48,6 +48,26 @@ class ActiveRecord::Base
48
48
  # the param_column to the current to_param
49
49
  def set_param
50
50
  send "#{param_column}=", to_param if param_column?
51
+ if self.class.param_taken?(self)
52
+ i = 2
53
+ base_value = send "#{param_column}"
54
+ send "#{param_column}=", "#{base_value}-#{i}"
55
+ while(self.class.param_taken?(self))
56
+ base_value = send "#{param_column}"
57
+ send "#{param_column}=", base_value.chop + (i+=1).to_s
58
+ end
59
+ end
60
+ end
61
+
62
+ # check to see if this param is already in use
63
+ class << self
64
+ def param_taken?(instance)
65
+ return false if !param_column?
66
+ conditions = instance.new_record? ? nil : ["id <> ?", instance.id]
67
+ check_value = instance.send "#{param_column}"
68
+ return true if instance.class.send("find_by_#{param_column}", check_value, :conditions => conditions)
69
+ false
70
+ end
51
71
  end
52
72
 
53
73
  end
@@ -5,6 +5,10 @@ class String
5
5
  # * Converts to all lower-case
6
6
  # * Removes all characters except alphabetic and numeric
7
7
  def urlize
8
- self.gsub(" ","-").downcase.gsub(/[^a-z0-9-]/,"")
8
+ if ActiveSupport::Inflector.respond_to?(:parameterize)
9
+ self.parameterize
10
+ else
11
+ self.gsub(" ","-").downcase.gsub(/[^a-z0-9-]/,"")
12
+ end
9
13
  end
10
14
  end
@@ -34,4 +34,44 @@ describe ActiveRecord::Base, "with from_param" do
34
34
  @paramed.save
35
35
  SluggedModel.from_param(@paramed.to_param).should == @paramed
36
36
  end
37
+
38
+ it "#param_taken? should return true if param is already used" do
39
+ @paramed.name = "Angus"
40
+ @paramed.save
41
+ @paramed2 = ParameteredModel.new
42
+ @paramed2.param = "angus"
43
+ ParameteredModel.param_taken?(@paramed2).should be_true
44
+ end
45
+
46
+ it "#param_taken? should return false if updating own parameter" do
47
+ @paramed.name = "Ben"
48
+ @paramed.save
49
+ @paramed.param = "ben"
50
+ ParameteredModel.param_taken?(@paramed).should be_false
51
+ end
52
+
53
+ it "should append digit at the end if param is taken" do
54
+ @paramed.name = "Clyde"
55
+ @paramed.save
56
+ @paramed2 = ParameteredModel.new
57
+ @paramed2.name = "Clyde"
58
+ @paramed2.save
59
+ @paramed2.param.should == "clyde-2"
60
+ end
61
+
62
+ it "should serialize the name as necessary to avoid collisions" do
63
+ @paramed.name = "Dennis"
64
+ @paramed.save
65
+ @paramed2 = ParameteredModel.new
66
+ @paramed2.name = "Dennis"
67
+ @paramed2.save
68
+ @paramed3 = ParameteredModel.new
69
+ @paramed3.name = "Dennis"
70
+ @paramed3.save
71
+ @paramed4 = ParameteredModel.new
72
+ @paramed4.name = "Dennis"
73
+ @paramed4.save
74
+ @paramed4.param.should == "dennis-4"
75
+ end
76
+
37
77
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,35 @@
1
- require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_record'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/from_param')
5
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/string_extensions')
2
6
 
3
7
  plugin_spec_dir = File.dirname(__FILE__)
4
8
  ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
5
9
 
10
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
11
+
12
+ db_adapter = ENV['DB']
13
+
14
+ # no db passed, try one of these fine config-free DBs before bombing.
15
+ db_adapter ||= begin
16
+ require 'rubygems'
17
+ require 'sqlite'
18
+ 'sqlite'
19
+ rescue MissingSourceFile
20
+ begin
21
+ require 'sqlite3'
22
+ 'sqlite3'
23
+ rescue MissingSourceFile
24
+ end
25
+ end
26
+
27
+ if db_adapter.nil?
28
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
29
+ end
30
+
31
+ ActiveRecord::Base.establish_connection(config[db_adapter])
32
+
6
33
  load(File.dirname(__FILE__) + '/schema.rb')
7
34
 
8
35
  class ParameteredModel < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffmike-from_param
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.0"
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-29 00:00:00 -07:00
12
+ date: 2008-10-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15