effective_resources 0.8.15 → 0.8.16
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7619108179839c52910a9c48bd64dc6cde187aab
|
4
|
+
data.tar.gz: c56da3425ba5cc69e42a619221525eaa24ec1907
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+
|
data/lib/effective_resources.rb
CHANGED
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.
|
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-
|
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
|