effective_resources 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 379383cd8b7fbf6f68822d2c23fe3a715c3374c7
4
- data.tar.gz: d7f66a8b0636d9d89c29e93a27ed1d9e145f53fa
3
+ metadata.gz: 7619108179839c52910a9c48bd64dc6cde187aab
4
+ data.tar.gz: c56da3425ba5cc69e42a619221525eaa24ec1907
5
5
  SHA512:
6
- metadata.gz: 3558655992943e761ffe3f3cafcc81ae3f90c2cb613837c90308ecc2f42e55d85c944eae190e7a68e67069d8cd154b8c1bc7f3356c1a52fd9f84915a045a8233
7
- data.tar.gz: 23a713c5aef042f0e45cae0e139aeea21c9e1e0f519f54b2960df8b6fd647ea13cdd54ce70023504b88407954c2a5bc0b534322434cb7f3c2e07338374518590
6
+ metadata.gz: 2005053efb36c11fb1d185477c1a7c7811e225454fb6363afd5e195b7bedf8077c22b2011c8b70435bff73068bd5ebcc3d744c6a5335bd643857b2fd0347fe8d
7
+ data.tar.gz: b9e96d197b49b499be1b0f0713e0fb888a5ca84677ded50bf0120a8f06bd551189ba2858c749a9c1f7ea10c0e3627103bddc6d227e9e40795abdf339c8b582c1
@@ -0,0 +1,67 @@
1
+ # ActsAsSlugged
2
+ #
3
+ # This module automatically generates slugs based on the :to_s field using a before_validation filter
4
+ #
5
+ # Mark your model with 'acts_as_sluggable' make sure you have a string field :slug
6
+
7
+ module ActsAsSlugged
8
+ extend ActiveSupport::Concern
9
+
10
+ module ActiveRecord
11
+ def acts_as_slugged(options = nil)
12
+ raise 'must respond to slug' unless new().respond_to?(:slug)
13
+
14
+ include ::ActsAsSlugged
15
+ end
16
+ end
17
+
18
+ included do
19
+ extend FinderMethods
20
+
21
+ before_validation { self.slug ||= build_slug }
22
+
23
+ validates :slug,
24
+ presence: true, uniqueness: true, exclusion: { in: excluded_slugs }, length: { maximum: 255 },
25
+ format: { with: /\A[a-zA-Z0-9_-]*\z/, message: 'only _ and - symbols allowed. no spaces either.' }
26
+ end
27
+
28
+ module ClassMethods
29
+ def relation
30
+ super.tap { |relation| relation.extend(FinderMethods) }
31
+ end
32
+
33
+ def excluded_slugs
34
+ ::ActiveRecord::Base.connection.tables.map { |x| x }.compact
35
+ end
36
+ end
37
+
38
+ module FinderMethods
39
+ def find(*args)
40
+ return super unless args.length == 1
41
+ return super if block_given?
42
+
43
+ where(slug: args.first).or(where(id: args.first)).first || raise(::ActiveRecord::RecordNotFound.new("Couldn't find #{name} with 'slug'=#{args.first}"))
44
+ end
45
+ end
46
+
47
+ # Instance Methods
48
+ def build_slug
49
+ slug = self.to_s.parameterize.downcase[0, 250]
50
+
51
+ if self.class.excluded_slugs.include?(slug)
52
+ slug = "#{slug}-#{self.class.name.demodulize.parameterize}"
53
+ end
54
+
55
+ if (count = self.class.where(slug: slug).count) > 0
56
+ slug = "#{slug}-#{count+1}"
57
+ end
58
+
59
+ slug
60
+ end
61
+
62
+ def to_param
63
+ slug
64
+ end
65
+
66
+ end
67
+
@@ -26,5 +26,4 @@ module EffectiveResources
26
26
  def self.authorize!(controller, action, resource)
27
27
  raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
28
28
  end
29
-
30
29
  end
@@ -20,6 +20,7 @@ module EffectiveResources
20
20
  initializer 'effective_resources.active_record' do |app|
21
21
  ActiveSupport.on_load :active_record do
22
22
  ActiveRecord::Base.extend(ActsAsTokened::ActiveRecord)
23
+ ActiveRecord::Base.extend(ActsAsSlugged::ActiveRecord)
23
24
  end
24
25
  end
25
26
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.8.15'.freeze
2
+ VERSION = '0.8.16'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.15
4
+ version: 0.8.16
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: 2018-05-21 00:00:00.000000000 Z
11
+ date: 2018-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,7 @@ files:
38
38
  - app/controllers/concerns/effective/crud_controller.rb
39
39
  - app/controllers/concerns/effective/flash_messages.rb
40
40
  - app/helpers/effective_resources_helper.rb
41
+ - app/models/concerns/acts_as_slugged.rb
41
42
  - app/models/concerns/acts_as_tokened.rb
42
43
  - app/models/effective/access_denied.rb
43
44
  - app/models/effective/attribute.rb