sluggable-rails 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: f6642fdec2fa4e1446c0f420a4b74de6cfe81a695b8e5ea0b0d8ef636aaca04f
4
- data.tar.gz: d3f8d5215cd75b92d5d1f42eb706a19c97ecd142bb6ba1054d1937b299216701
3
+ metadata.gz: c38b59bf2edd9acb8300d9e37832e4120939b864f2f65d1f9702a8f49c12aabe
4
+ data.tar.gz: 277eb4281f2b13f19120873d0eab09bb1075c618d5ab7488f5a11f097f24aab4
5
5
  SHA512:
6
- metadata.gz: 14d3e1ed04fd8301d854f181cfd38a604fd238a455f617bb49ed7db34eca450db171c52269379dd1b863c61a8032e78037fb0ad663ea2661325f18fd6cf5946e
7
- data.tar.gz: e4a4d15517bbccecb8aae456fd189dce2418c4b9209f646426cb6a31dc284584031d9db6f00e0a470081ecc130a63b6859014a13b2be2b91e70042d3e4e8f211
6
+ metadata.gz: e7bea5adb515c009394a298ffbf4853aa5f46e555f3f40bd8a2eefbaec110e9bf75d077b216a6320b354937aeab735b3ce8304ddea7cd99dec1812a10b3ef437
7
+ data.tar.gz: 771331ee4fcb571f7ee2b161e4f2aa55a956e1cf0eddcbe52692f89a98dd1093170126e0906a4ad59bc4bd820af713e379878f09ab1f60664991dcdb19b0a332
@@ -1,24 +1,3 @@
1
- ActiveRecord::Base.instance_eval do
2
- before_save :update_slugs
3
-
4
- def has_slug(attribute = :slug, by:, separator: '-', scope: nil)
5
- slug_definitions.add attribute, origin: by, separator: separator, scope: scope
6
- validates attribute, presence: true, uniqueness: scope.present? ? { scope: scope } : true
7
- end
8
-
9
- def slug_definitions
10
- @slug_definitions ||= Sluggable::Rails::Definitions.new
11
- end
12
- end
13
-
14
1
  ActiveRecord::Base.class_eval do
15
- def slugs
16
- self.class.slug_definitions.slugs from: self
17
- end
18
-
19
- protected
20
-
21
- def update_slugs
22
- slugs.each { |slug| slug.update if slug.needs_an_update? }
23
- end
2
+ include Sluggable::Rails::Concern
24
3
  end
@@ -4,6 +4,7 @@ module Sluggable
4
4
  require 'sluggable/rails/definitions'
5
5
  require 'sluggable/rails/slug'
6
6
  require 'sluggable/rails/slugs'
7
+ require 'sluggable/rails/concern'
7
8
  require 'sluggable/core_ext'
8
9
  end
9
10
  end
@@ -0,0 +1,28 @@
1
+ module Sluggable::Rails::Concern
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_validation :update_slugs
6
+ end
7
+
8
+ def slugs
9
+ self.class.slug_definitions.slugs from: self
10
+ end
11
+
12
+ module ClassMethods
13
+ def has_slug(attribute = :slug, by:, separator: '-', scope: nil)
14
+ slug_definitions.add attribute, origin: by, separator: separator, scope: scope
15
+ validates attribute, presence: true, uniqueness: scope.present? ? { scope: scope } : true
16
+ end
17
+
18
+ def slug_definitions
19
+ @slug_definitions ||= Sluggable::Rails::Definitions.new
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def update_slugs
26
+ slugs.each { |slug| slug.update if slug.needs_an_update? }
27
+ end
28
+ end
@@ -1,4 +1,6 @@
1
1
  class Sluggable::Rails::Definitions
2
+ include Enumerable
3
+
2
4
  def initialize
3
5
  @definitions = {}
4
6
  end
@@ -16,4 +18,8 @@ class Sluggable::Rails::Definitions
16
18
  @definitions.each { |_, definition| slugs.add definition, from }
17
19
  slugs
18
20
  end
21
+
22
+ def each(&block)
23
+ @definitions.each &block
24
+ end
19
25
  end
@@ -51,7 +51,7 @@ class Sluggable::Rails::Slug < String
51
51
  end
52
52
 
53
53
  def scope
54
- definition.scope.map{ |attribute| [atribute, record.send(attribute)] }.to_h
54
+ definition.scope.map{ |attribute| [attribute, record.send(attribute)] }.to_h
55
55
  end
56
56
 
57
57
  protected
@@ -1,4 +1,6 @@
1
1
  class Sluggable::Rails::Slugs
2
+ include Enumerable
3
+
2
4
  def initialize
3
5
  @slugs = []
4
6
  end
@@ -7,7 +9,7 @@ class Sluggable::Rails::Slugs
7
9
  @slugs << Sluggable::Rails::Slug.new(definition, record)
8
10
  end
9
11
 
10
- def each &block
12
+ def each(&block)
11
13
  @slugs.each &block
12
14
  end
13
15
  end
@@ -1,5 +1,5 @@
1
1
  module Sluggable
2
2
  module Rails
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sluggable-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Dargelos
@@ -51,6 +51,7 @@ files:
51
51
  - Rakefile
52
52
  - lib/sluggable/core_ext.rb
53
53
  - lib/sluggable/rails.rb
54
+ - lib/sluggable/rails/concern.rb
54
55
  - lib/sluggable/rails/definition.rb
55
56
  - lib/sluggable/rails/definitions.rb
56
57
  - lib/sluggable/rails/slug.rb