friendly_id 3.2.1.1 → 3.3.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -6,6 +6,11 @@ suggestions, ideas and improvements to FriendlyId.
6
6
  * Table of Contents
7
7
  {:toc}
8
8
 
9
+ ## 3.3.0 (NOT_RELEASED_YET)
10
+
11
+ * Compatibility with Active Record 3.1 ([Andrew White](https://github.com/pixeltrix))
12
+ * Gemspec compatibility with RubyGems 1.7 ([Philip Arndt](https://github.com/parndt))
13
+
9
14
  ## 3.2.1 (2011-02-03)
10
15
 
11
16
  * Fixed minor issue with ActiveRecord 3.0.4.rc1 (Bruno Michel)
data/Guide.md CHANGED
@@ -351,6 +351,22 @@ store a reserved value, FriendlyId raises a
351
351
  reserved words in {FriendlyId::Configuration::DEFAULTS} to set the value for any
352
352
  model using FriendlyId.
353
353
 
354
+ If you'd like to show a validation error when a word is reserved, you can add
355
+ an callback to your model that catches the error:
356
+
357
+ class Person < ActiveRecord::Base
358
+ has_friendly_id :name, :use_slug => true
359
+
360
+ after_validation :validate_reserved
361
+
362
+ def validate_reserved
363
+ slug_text
364
+ rescue FriendlyId::ReservedError
365
+ @errors[friendly_id_config.method] = "is reserved. Please choose something else"
366
+ return false
367
+ end
368
+ end
369
+
354
370
  ## Caching the FriendlyId Slug for Better Performance
355
371
 
356
372
  Checking the slugs table all the time has an impact on performance, so as of
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
1
  require "rubygems"
2
- require "bundler/setup"
3
2
  require "rake"
4
3
  require "rake/testtask"
5
4
  require "rake/gempackagetask"
6
5
  require "rake/clean"
6
+ require "bundler/setup"
7
7
 
8
8
  task :default => :test
9
9
 
@@ -62,7 +62,7 @@ module FriendlyId
62
62
  sluggable_ids = sluggable_ids_for([id])
63
63
 
64
64
  if sluggable_ids.size > 1 && fc.scope?
65
- return relation.where(relation.primary_key.in(sluggable_ids)).first
65
+ return relation.where(primary_key_column.in(sluggable_ids)).first
66
66
  end
67
67
 
68
68
  sluggable_id = sluggable_ids.first
@@ -83,7 +83,7 @@ module FriendlyId
83
83
  return find_some_using_slug(friendly_ids, unfriendly_ids) if use_slugs_table
84
84
  column = fc.cache_column || fc.column
85
85
  friendly = arel_table[column].in(friendly_ids)
86
- unfriendly = arel_table[relation.primary_key.name].in unfriendly_ids
86
+ unfriendly = primary_key_column.in unfriendly_ids
87
87
  if friendly_ids.present? && unfriendly_ids.present?
88
88
  where(friendly.or(unfriendly))
89
89
  else
@@ -93,7 +93,7 @@ module FriendlyId
93
93
 
94
94
  def find_some_using_slug(friendly_ids, unfriendly_ids)
95
95
  ids = [unfriendly_ids + sluggable_ids_for(friendly_ids)].flatten.uniq
96
- where(arel_table[relation.primary_key.name].in(ids))
96
+ where(primary_key_column.in(ids))
97
97
  end
98
98
 
99
99
  def sluggable_ids_for(ids)
@@ -131,6 +131,14 @@ module FriendlyId
131
131
  raise ActiveRecord::RecordNotFound, error
132
132
  end
133
133
  end
134
+
135
+ def primary_key_column
136
+ if relation.primary_key.is_a?(String)
137
+ arel_table[relation.primary_key]
138
+ else
139
+ arel_table[relation.primary_key.name]
140
+ end
141
+ end
134
142
  end
135
143
 
136
144
  def apply_finder_options(options)
@@ -45,6 +45,14 @@ class Slug < ::ActiveRecord::Base
45
45
  end
46
46
  end
47
47
 
48
+ def save(*args)
49
+ persisted? && !scope_changed? ? true : super
50
+ end
51
+
52
+ def save!(*args)
53
+ persisted? && !scope_changed? ? true : super
54
+ end
55
+
48
56
  private
49
57
 
50
58
  # If we're renaming back to a previously used friendly_id, delete the
@@ -5,7 +5,7 @@ module FriendlyId
5
5
  def self.included(base)
6
6
  base.class_eval do
7
7
  has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
8
- has_one :slug, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
8
+ has_one :slug, :order => 'id DESC', :as => :sluggable, :dependent => :nullify
9
9
  before_save :build_a_slug
10
10
  after_save :set_slug_cache
11
11
  after_update :update_scope
@@ -46,9 +46,8 @@ module FriendlyId
46
46
  # Build the new slug using the generated friendly id.
47
47
  def build_a_slug
48
48
  return unless new_slug_needed?
49
- @slug = slugs.build :name => slug_text.to_s, :scope => friendly_id_config.scope_for(self),
50
- :sluggable => self
51
- @new_friendly_id = @slug.to_friendly_id
49
+ self.slug = slugs.build(:name => slug_text.to_s, :scope => friendly_id_config.scope_for(self), :sluggable => self)
50
+ @new_friendly_id = slug.to_friendly_id
52
51
  end
53
52
 
54
53
  # Reset the cached friendly_id?
@@ -1,9 +1,9 @@
1
1
  module FriendlyId
2
2
  module Version
3
3
  MAJOR = 3
4
- MINOR = 2
5
- TINY = 1
6
- BUILD = 1
4
+ MINOR = 3
5
+ TINY = 0
6
+ BUILD = 'alpha1'
7
7
  STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
8
8
  end
9
9
  end
@@ -96,6 +96,13 @@ module FriendlyId
96
96
  assert_equal @resident2, @canada.residents.find("john-smith")
97
97
  end
98
98
 
99
+ test "scope records found as a relation member should be 'best'" do
100
+ assert_equal @resident, @usa.residents.find("john-smith")
101
+ assert @resident.friendly_id_status.best?
102
+ assert_equal @resident2, @canada.residents.find("john-smith")
103
+ assert @resident2.friendly_id_status.best?
104
+ end
105
+
99
106
  test "should find a single scoped record using slug conditions" do
100
107
  assert_equal @resident, Resident.find(@resident.friendly_id, :include => :slugs,
101
108
  :conditions => {:slugs => {:scope => @resident.country.to_param}})
@@ -116,4 +123,4 @@ module FriendlyId
116
123
  end
117
124
  end
118
125
  end
119
- end
126
+ end
@@ -0,0 +1,4 @@
1
+ adapter: mysql2
2
+ database: friendly_id_test
3
+ username: root
4
+ hostname: 127.0.0.1
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 3.2.1.1
4
+ prerelease: 6
5
+ version: 3.3.0.alpha1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Norman Clarke
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-03-23 00:00:00 -03:00
15
+ date: 2011-05-13 00:00:00 -03:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  requirements:
34
34
  - - ~>
35
35
  - !ruby/object:Gem::Version
36
- version: 3.0.0
36
+ version: "3.0"
37
37
  type: :development
38
38
  prerelease: false
39
39
  version_requirements: *id002
@@ -116,6 +116,7 @@ files:
116
116
  - test/active_record_adapter/sti_test.rb
117
117
  - test/active_record_adapter/support/database.jdbcsqlite3.yml
118
118
  - test/active_record_adapter/support/database.mysql.yml
119
+ - test/active_record_adapter/support/database.mysql2.yml
119
120
  - test/active_record_adapter/support/database.postgres.yml
120
121
  - test/active_record_adapter/support/database.sqlite3.yml
121
122
  - test/active_record_adapter/support/models.rb
@@ -143,16 +144,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
144
  requirements:
144
145
  - - ">="
145
146
  - !ruby/object:Gem::Version
146
- hash: 2028799673610630281
147
+ hash: 1894634640096086793
147
148
  segments:
148
149
  - 0
149
150
  version: "0"
150
151
  required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  none: false
152
153
  requirements:
153
- - - ">="
154
+ - - ">"
154
155
  - !ruby/object:Gem::Version
155
- version: "0"
156
+ version: 1.3.1
156
157
  requirements: []
157
158
 
158
159
  rubyforge_project: friendly-id