effective_slugs 1.2.2 → 1.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5aa5f0a3c6777c563c28d3924a9ffed0f382e807
4
- data.tar.gz: ba13e7ca75a21e6fe93699168c74ce8033e8b3c2
3
+ metadata.gz: d0875f47623bbaccf338ab9af234f1da14cb8cc1
4
+ data.tar.gz: f52f76f5543f0ba46e2608d141a4e92ab81d739e
5
5
  SHA512:
6
- metadata.gz: 053338eb7f19cc37a35036987b40c066ee486eec023530686e2c287606fe821f1654127b8bc6b2b06310835f4fe4a45eb5a2dcd0dfbede11689187a6d48f4551
7
- data.tar.gz: a40b50644123a979dbc2ad8578b4e0cb4e43c004597044376ecbc049cf0f4a6e39cb5f035976bb22262379e4c2322e00c6ada01980c141d4a843513a0d87bb9d
6
+ metadata.gz: 147999df32daee2087b25d029e5c8d1c1031521e0392e3a1cacf09db5d863577b8ec422ea20b5ba717165a57a0a8c1f90f9ee608c18ff6ca3523b72696c94b67
7
+ data.tar.gz: 4b7909bf46d75699f77545afdf99dc536454a4e4975fadc97b7862c7006000c623bf338a68315d4e960ac893d09c600d08fe7a6ce836d4028c1f3d6ee0328ddb
data/README.md CHANGED
@@ -47,7 +47,7 @@ As we're doing lookups on this column, a database index makes a lot of sense too
47
47
  rails generate migration add_slug_to_post slug:string:index
48
48
  ```
49
49
 
50
- which will create a migration something like
50
+ which will create a migration something like:
51
51
 
52
52
  ```ruby
53
53
  class AddSlugToPost < ActiveRecord::Migration
@@ -58,6 +58,22 @@ class AddSlugToPost < ActiveRecord::Migration
58
58
  end
59
59
  ```
60
60
 
61
+ Then collect the slug field with your object's form. The below example will not be displayed on a #new but it will on #edit or if the slug is in error.
62
+
63
+ ```haml
64
+ - if f.object.persisted? || f.object.errors.include?(:slug)
65
+ - current_url = (post_path(f.object) rescue nil)
66
+ = f.input :slug, hint: "The slug controls this post's internet address. Be careful, changing the slug will break links that other websites may have to the old address.<br>#{('This post is currently reachable via ' + link_to(current_url.gsub(f.object.slug, '<strong>' + f.object.slug + '</strong>').html_safe, current_url)) if current_url }".html_safe
67
+ ```
68
+
69
+ and include the permitted param within your controller:
70
+
71
+ ```ruby
72
+ def permitted_params
73
+ params.require(:post).permit(:slug)
74
+ end
75
+ ```
76
+
61
77
  ## Behavior
62
78
 
63
79
  ### Slug Generation
@@ -46,7 +46,7 @@ module ActsAsSluggable
46
46
  module ActiveRecord
47
47
  def acts_as_sluggable(options = {})
48
48
  if self.methods.include?(:is_site_specific) # ActsAsSiteSpecific
49
- @acts_as_sluggable_opts = {:validation_scope => :site_id}.merge(options)
49
+ @acts_as_sluggable_opts = { validation_scope: :site_id }.merge(options)
50
50
  else
51
51
  @acts_as_sluggable_opts = {}.merge(options)
52
52
  end
@@ -56,18 +56,14 @@ module ActsAsSluggable
56
56
  end
57
57
 
58
58
  included do
59
- before_validation :set_slug, :if => proc { should_generate_new_slug? }
59
+ before_validation :set_slug, if: proc { should_generate_new_slug? }
60
60
 
61
- validates_presence_of :slug
62
- validates_exclusion_of :slug, :in => EffectiveSlugs.all_excluded_slugs
63
- validates_length_of :slug, :maximum => 255
64
- validates_format_of :slug, :with => /\A[a-zA-Z0-9_-]*\z/, :message => 'only _ and - symbols allowed. no spaces either.'
65
-
66
- if @acts_as_sluggable_opts[:validation_scope]
67
- validates_uniqueness_of :slug, :scope => @acts_as_sluggable_opts[:validation_scope]
68
- else
69
- validates_uniqueness_of :slug
70
- end
61
+ validates :slug,
62
+ exclusion: { in: EffectiveSlugs.all_excluded_slugs },
63
+ format: { with: /\A[a-zA-Z0-9_-]*\z/, message: 'only _ and - symbols allowed. no spaces either.' },
64
+ length: { maximum: 255 },
65
+ presence: true,
66
+ uniqueness: @acts_as_sluggable_opts[:validation_scope] ? { scope: @acts_as_sluggable_opts[:validation_scope] } : true
71
67
 
72
68
  if ::ActiveRecord::VERSION::MAJOR == 4 && ::ActiveRecord::VERSION::MINOR == 2
73
69
  extend FinderMethods
@@ -75,17 +71,17 @@ module ActsAsSluggable
75
71
  end
76
72
 
77
73
  def set_slug
78
- raise StandardError, "ActsAsSluggable expected a table column :slug to exist" unless self.respond_to?(:slug)
74
+ raise StandardError, "ActsAsSluggable expected a table column :slug to exist" unless respond_to?(:slug)
79
75
 
80
76
  new_slug = slug_source.to_s.try(:parameterize)
81
77
 
82
78
  if new_slug.present?
83
79
  while EffectiveSlugs.excluded_slugs.include?(new_slug) do
84
- new_slug << "-" << self.class.name.demodulize.parameterize
80
+ new_slug << '-'.freeze << self.class.name.demodulize.parameterize
85
81
  end
86
82
 
87
83
  # TODO: Could make this a bit smarter about conflicts
88
- num_slugs = self.class.name.constantize.where(:slug => new_slug).count
84
+ num_slugs = self.class.name.constantize.where(slug: new_slug).count
89
85
  num_slugs = self.class.name.constantize.where('slug LIKE ?', "#{new_slug}%").count if num_slugs > 0
90
86
 
91
87
  num_slugs == 0 ? self.slug = new_slug : self.slug = "#{new_slug}-#{num_slugs}"
@@ -95,8 +91,8 @@ module ActsAsSluggable
95
91
  end
96
92
 
97
93
  def slug_source
98
- return title if self.respond_to?(:title)
99
- return name if self.respond_to?(:name)
94
+ return title if respond_to?(:title)
95
+ return name if respond_to?(:name)
100
96
  to_s
101
97
  end
102
98
 
@@ -116,23 +112,32 @@ module ActsAsSluggable
116
112
 
117
113
  module FinderMethods
118
114
  def find(*args)
119
- if regular_find?(args)
120
- super
115
+ first = args.first || ''.freeze
116
+ return super if first.kind_of?(Array) || first.kind_of?(Integer)
117
+
118
+ slug = first.to_s
119
+
120
+ if (slug.delete('^0-9'.freeze).length == slug.length) # The slug could be '400'
121
+ find_by_slug(args) || find_by_id!(args)
121
122
  else
122
- find_by_slug(args) or raise ::ActiveRecord::RecordNotFound
123
+ find_by_slug!(args)
123
124
  end
125
+
124
126
  end
125
127
 
126
128
  def exists?(*args)
127
- regular_find?(args) ? super : (find_by_slug(args).present? rescue false)
128
- end
129
+ first = args.first || ''.freeze
130
+ return super if first.kind_of?(Array) || first.kind_of?(Integer)
129
131
 
130
- private
132
+ slug = first.to_s
131
133
 
132
- def regular_find?(args)
133
- first = args.first || ''.freeze
134
- first.kind_of?(Array) || first.kind_of?(Integer) || (first.delete('^0-9').length == first.length)
134
+ if (slug.delete('^0-9'.freeze).length == slug.length) # The slug could be '400'
135
+ (where(arel_table[:slug].eq(slug).or(arel_table[:id].eq(slug))).present? rescue false)
136
+ else
137
+ (find_by_slug(args).present? rescue false)
138
+ end
135
139
  end
140
+
136
141
  end
137
142
  end
138
143
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveSlugs
2
- VERSION = '1.2.2'.freeze
2
+ VERSION = '1.2.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_slugs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-12 00:00:00.000000000 Z
11
+ date: 2016-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails