simple_slug 0.0.1 → 0.1.0

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: 38033c0b797c80a5f1ea79325d95605f9f35dae9
4
- data.tar.gz: 9bcc30728ffaaa925711bf374b94d359579a84aa
3
+ metadata.gz: d858815acb25626bd487c3a08d1550cdf39c3895
4
+ data.tar.gz: 18a66060a237cb85e5f901566305e6b5e6698dff
5
5
  SHA512:
6
- metadata.gz: 2a2135bcbc5a5aa951989a6a073c692598ff1bbb7b73195347be469a749c14a1f65a24e93f9cbc553f12632dcf39b7cf8223cacb64e6c3c275cab6db0e82ce8e
7
- data.tar.gz: 74200571bf0ca1c83747b034920c07fe432f24d9afb977caf89c61de3fe406bca3d5478ff4f208da967c668fb8de414c7fc6ee88fa7338e3d8e3ccd0648cd26e
6
+ metadata.gz: 8b2edc684abb85fa503c0b214488428a4ebd5684cd62a79424e703f486e52b805fe41aa0d44dfa9e7fdde2a215f56d95295d0bee822710b21410b5bb7f9761e1
7
+ data.tar.gz: 2258496ff4fb76de5a69cd08f5d9dd14036875817eb473f0ac9d84abb35efd146877a42ca5fa62270dfa5da521559ba827c51b2ff06eecb1fa16edc4f5607f10
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.0
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # SimpleSlug
2
2
 
3
- This is not a "bulldozer", this is just friendly id generator for ActiveRecord.
3
+ [![Build Status](https://travis-ci.org/leschenko/simple_slug.png?branch=master)](https://travis-ci.org/leschenko/simple_slug)
4
+ [![Dependency Status](https://gemnasium.com/leschenko/simple_slug.png)](https://gemnasium.com/leschenko/simple_slug)
5
+
6
+ Simple friendly url generator for ActiveRecord models with history.
4
7
 
5
8
  ## Installation
6
9
 
@@ -34,9 +37,21 @@ class User < ActiveRecord::Base
34
37
  end
35
38
  ```
36
39
 
40
+ If you want to control when slug is generated, define `should_generate_new_slug?` method:
41
+
42
+ ```ruby
43
+ class User < ActiveRecord::Base
44
+ simple_slug :full_name
45
+
46
+ def should_generate_new_slug?
47
+ true
48
+ end
49
+ end
50
+ ```
51
+
37
52
  ## Contributing
38
53
 
39
- 1. Fork it ( http://github.com/<my-github-username>/simple_slug/fork )
54
+ 1. Fork it ( http://github.com/leschenko/simple_slug/fork )
40
55
  2. Create your feature branch (`git checkout -b my-new-feature`)
41
56
  3. Commit your changes (`git commit -am 'Add some feature'`)
42
57
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -19,7 +19,7 @@ module SimpleSlug
19
19
  exclusion: {in: SimpleSlug.excludes},
20
20
  format: {without: SimpleSlug.exclude_regexp}
21
21
  if simple_slug_options[:history]
22
- after_save :simple_slug_create_history_slug
22
+ after_save :simple_slug_reset_unsaved_slug, :simple_slug_create_history_slug
23
23
  after_destroy :simple_slug_cleanup_history
24
24
  include InstanceHistoryMethods
25
25
  end
@@ -42,16 +42,16 @@ module SimpleSlug
42
42
 
43
43
  module InstanceMethods
44
44
  def to_param
45
- send(simple_slug_options[:slug_column]).presence || super
45
+ simple_slug_stored_slug.presence || super
46
46
  end
47
47
 
48
48
  def should_generate_new_slug?
49
- send(simple_slug_options[:slug_column]).blank? || simple_slug_options[:history]
49
+ simple_slug_get.blank? || simple_slug_options[:history]
50
50
  end
51
51
 
52
- def simple_slug_generate
52
+ def simple_slug_generate(force=false)
53
53
  simple_slug = simple_slug_normalize(simple_slug_base)
54
- return true if simple_slug == send(simple_slug_options[:slug_column]).to_s.sub(/--\d+\z/, '')
54
+ return true if !force && simple_slug == send(simple_slug_options[:slug_column]).to_s.sub(/--\d+\z/, '')
55
55
  resolved_simple_slug = simple_slug_resolve(simple_slug)
56
56
  send "#{simple_slug_options[:slug_column]}=", resolved_simple_slug
57
57
  end
@@ -96,16 +96,33 @@ module SimpleSlug
96
96
  base_scope = base_scope.where('sluggable_id != ?', id) if persisted?
97
97
  base_scope.exists?
98
98
  end
99
+
100
+ def simple_slug_set(value)
101
+ send "#{simple_slug_options[:slug_column]}=", value
102
+ end
103
+
104
+ def simple_slug_get
105
+ send simple_slug_options[:slug_column]
106
+ end
107
+
108
+ def simple_slug_stored_slug
109
+ send("#{simple_slug_options[:slug_column]}_was")
110
+ end
99
111
  end
100
112
 
101
113
  module InstanceHistoryMethods
114
+ def simple_slug_reset_unsaved_slug
115
+ return true if errors.blank?
116
+ simple_slug_set simple_slug_stored_slug
117
+ end
118
+
102
119
  def simple_slug_cleanup_history
103
120
  ::SimpleSlug::HistorySlug.where(sluggable_type: self.class.name, sluggable_id: id).delete_all
104
121
  end
105
122
 
106
123
  def simple_slug_create_history_slug
107
124
  return true unless slug_changed?
108
- ::SimpleSlug::HistorySlug.where(sluggable_type: self.class.name, sluggable_id: id, slug: send(simple_slug_options[:slug_column])).first_or_create
125
+ ::SimpleSlug::HistorySlug.where(sluggable_type: self.class.name, sluggable_id: id, slug: simple_slug_get).first_or_create
109
126
  end
110
127
  end
111
128
 
@@ -1,3 +1,3 @@
1
1
  module SimpleSlug
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
data/simple_slug.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SimpleSlug::VERSION
9
9
  spec.authors = ['Alex Leschenko']
10
10
  spec.email = ['leschenko.al@gmail.com']
11
- spec.summary = %q{Simple slug generator with history.}
12
- spec.description = %q{This is not a "bulldozer. This is just friendly id generator fo ActiveRecord."}
11
+ spec.summary = %q{Friendly url generator with history.}
12
+ spec.description = %q{Simple friendly url generator for ActiveRecord with history."}
13
13
  spec.homepage = 'https://github.com/leschenko/simple_slug'
14
14
  spec.license = 'MIT'
15
15
 
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'activerecord', '~> 4.0.0'
21
+ spec.add_dependency 'activerecord', '>= 4.0.0', '< 5.0'
22
22
  spec.add_dependency 'i18n', '~> 0.6.0'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.5'
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,7 @@ class RspecActiveModelBase
16
16
  define_model_callbacks :validation, :save, :destroy
17
17
 
18
18
  attr_accessor :id, :slug, :name, :created_at
19
+ alias_method :slug_was, :slug
19
20
 
20
21
  def self.create(attributes, *)
21
22
  record = new(attributes)
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Leschenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-13 00:00:00.000000000 Z
11
+ date: 2014-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 4.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: i18n
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +86,7 @@ dependencies:
80
86
  - - ">="
81
87
  - !ruby/object:Gem::Version
82
88
  version: '0'
83
- description: This is not a "bulldozer. This is just friendly id generator fo ActiveRecord."
89
+ description: Simple friendly url generator for ActiveRecord with history."
84
90
  email:
85
91
  - leschenko.al@gmail.com
86
92
  executables: []
@@ -88,6 +94,7 @@ extensions: []
88
94
  extra_rdoc_files: []
89
95
  files:
90
96
  - ".gitignore"
97
+ - ".travis.yml"
91
98
  - Gemfile
92
99
  - LICENSE.txt
93
100
  - README.md
@@ -123,10 +130,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
130
  version: '0'
124
131
  requirements: []
125
132
  rubyforge_project:
126
- rubygems_version: 2.2.0.rc.1
133
+ rubygems_version: 2.2.2
127
134
  signing_key:
128
135
  specification_version: 4
129
- summary: Simple slug generator with history.
136
+ summary: Friendly url generator with history.
130
137
  test_files:
131
138
  - spec/simple_slug/base_spec.rb
132
139
  - spec/simple_slug/history_spec.rb