simple_slug 0.3.0 → 0.3.1

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: 42fa741cbe466a58681cb3cefadc7b6d0b51b482
4
- data.tar.gz: 14481b78f88550c22b0d2de99fc57cca767d18f3
3
+ metadata.gz: caac6febd23cca44010d4b67fe7cb412328e8be3
4
+ data.tar.gz: f37d62b115a1d829bab4a1b819594015259352e4
5
5
  SHA512:
6
- metadata.gz: decf2bb240b092b883d6de908ac75840d04e51bc60ffa198eea175e1e380c511f1d990b2ba200a14e84acc8ba38126f4284a1b87eea610027e6367c1a4c18d59
7
- data.tar.gz: fed2de665fbd48727cb9e1f3ff1c5ae233b5bcd12c60d980a6364f03c0e30f3762f3a5e46af7a8f277917b27f550f5f7e4c92ad8ed493b8fecf275d0328aac4e
6
+ metadata.gz: b2c6fbd51d8eea2145dad462bab7f4891d550e50a868af06d58c8e8d6cbe3d60e9ed9ff1ac40eedf121cd4485df10a30665a8afc92de016db2b968d311c38fc3
7
+ data.tar.gz: 149e04495109161a9536394f6cf6de304da9edb3e8bb0c0654d8981b6f719f04d8f1d73a7da3eda4aa2c4c593a6af3594d7268857d4479972493a6833fc0126f
@@ -8,16 +8,26 @@ module SimpleSlug
8
8
  def simple_slug(*args)
9
9
  class_attribute :simple_slug_options, instance_writer: false
10
10
  options = args.extract_options!
11
- self.simple_slug_options = options.reverse_merge(slug_column: SimpleSlug.slug_column, slug_method: args, max_length: SimpleSlug.max_length)
11
+ self.simple_slug_options = options.reverse_merge(
12
+ slug_column: SimpleSlug.slug_column,
13
+ slug_method: args,
14
+ max_length: SimpleSlug.max_length,
15
+ callback_type: SimpleSlug.callback_type,
16
+ add_validation: SimpleSlug.add_validation
17
+ )
12
18
 
13
19
  include InstanceMethods
14
20
  extend ClassMethods
15
21
 
16
- before_validation :simple_slug_generate, if: :should_generate_new_slug?
17
- validates simple_slug_options[:slug_column],
18
- presence: true,
19
- exclusion: {in: SimpleSlug.excludes},
20
- format: {without: SimpleSlug.exclude_regexp}
22
+ send(simple_slug_options[:callback_type], :simple_slug_generate, if: :should_generate_new_slug?) if simple_slug_options[:callback_type]
23
+
24
+ if simple_slug_options[:add_validation]
25
+ validates simple_slug_options[:slug_column],
26
+ presence: true,
27
+ exclusion: {in: SimpleSlug.excludes},
28
+ format: {without: SimpleSlug.exclude_regexp}
29
+ end
30
+
21
31
  if simple_slug_options[:history]
22
32
  after_save :simple_slug_reset_unsaved_slug, :simple_slug_create_history_slug
23
33
  after_destroy :simple_slug_cleanup_history
@@ -1,3 +1,3 @@
1
1
  module SimpleSlug
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/simple_slug.rb CHANGED
@@ -18,10 +18,15 @@ module SimpleSlug
18
18
  mattr_accessor :max_length
19
19
  @@max_length = 240
20
20
 
21
+ mattr_accessor :callback_type
22
+ @@callback_type = :before_validation
23
+
24
+ mattr_accessor :add_validation
25
+ @@add_validation = true
26
+
21
27
  STARTS_WITH_NUMBER_REGEXP =/\A\d+/
22
28
 
23
29
  def self.setup
24
30
  yield self
25
31
  end
26
-
27
32
  end
@@ -4,6 +4,14 @@ class SlugGenerationRspecModel < RspecActiveModelBase
4
4
  simple_slug :name
5
5
  end
6
6
 
7
+ class SlugGenerationRspecModelWithoutValidation < RspecActiveModelBase
8
+ simple_slug :name, add_validation: false
9
+ end
10
+
11
+ class SlugGenerationRspecModelWithoutCallback < RspecActiveModelBase
12
+ simple_slug :name, callback_type: nil
13
+ end
14
+
7
15
  describe SimpleSlug::ModelAddition do
8
16
  describe 'slug generation' do
9
17
  before do
@@ -99,4 +107,16 @@ describe SimpleSlug::ModelAddition do
99
107
  expect(SlugGenerationRspecModel.new(name: 'Hello' * 100).simple_slug_generate.length).to eq 500
100
108
  end
101
109
  end
110
+
111
+ describe 'add_validation' do
112
+ it 'skip validation' do
113
+ expect(SlugGenerationRspecModelWithoutValidation.validators_on(:slug)).to be_blank
114
+ end
115
+ end
116
+
117
+ describe 'callback_type' do
118
+ it 'skip callback' do
119
+ expect(SlugGenerationRspecModelWithoutCallback.new).not_to receive(:should_generate_new_slug?)
120
+ end
121
+ end
102
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Leschenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-12 00:00:00.000000000 Z
11
+ date: 2016-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord