couchrest_model_slug 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +13 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +25 -0
- data/lib/couchrest/model/slug.rb +63 -0
- data/lib/couchrest/model/slug/version.rb +7 -0
- data/lib/couchrest_model_slug.rb +2 -0
- metadata +81 -0
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
group :development, :test do
|
4
|
+
gem "rails", "3.0.8"
|
5
|
+
gem "capybara", ">= 0.4.0"
|
6
|
+
gem "rspec-rails", "~> 2.6"
|
7
|
+
gem "simplecov", :require => false
|
8
|
+
gem "factory_girl_rails"
|
9
|
+
gem "ffaker"
|
10
|
+
gem "ZenTest"
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake'
|
10
|
+
require 'rake/rdoctask'
|
11
|
+
|
12
|
+
require 'rspec/core'
|
13
|
+
require 'rspec/core/rake_task'
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(:spec)
|
16
|
+
|
17
|
+
task :default => :spec
|
18
|
+
|
19
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'CouchrestModelSlug'
|
22
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
+
rdoc.rdoc_files.include('README.rdoc')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'stringex'
|
2
|
+
|
3
|
+
module CouchRest
|
4
|
+
module Model
|
5
|
+
module Slug
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
before_save :generate_slug
|
10
|
+
|
11
|
+
property :slug
|
12
|
+
view_by :slug
|
13
|
+
view_by :slug_without_count,
|
14
|
+
:map => "function(doc) {
|
15
|
+
if ((doc['#{self.model_type_key}'] == '#{self.name}') && (doc['slug'] != null)) {
|
16
|
+
var slug = doc['slug'].match(/^(\\S+?)(?:-\\d+)?$/);
|
17
|
+
emit(slug[1], null);
|
18
|
+
}
|
19
|
+
}",
|
20
|
+
:reduce => "_count"
|
21
|
+
|
22
|
+
cattr_accessor :slugged_props
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def to_param
|
27
|
+
result = self.send :slug
|
28
|
+
return super if result.blank?
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def generate_slug
|
34
|
+
if new_record? || slugged_props_changed?
|
35
|
+
slug = self.slugged_props.collect{ |prop| read_attribute(prop) }.join(", ").to_url
|
36
|
+
slug = self.class.generate_count_for_slug(slug)
|
37
|
+
self.slug = slug
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def slugged_props_changed?
|
42
|
+
self.slugged_props.any? { |p| self.send("#{p}_changed?") }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
def slug(*props)
|
48
|
+
self.slugged_props = props.map(&:to_s)
|
49
|
+
end
|
50
|
+
|
51
|
+
def find(value)
|
52
|
+
self.by_slug(:key => value).first || super(value)
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate_count_for_slug(slug_without_count)
|
56
|
+
hash = self.by_slug_without_count(:reduce => true, :group => true, :key => slug_without_count)["rows"].try(:first)
|
57
|
+
hash.nil? ? slug_without_count : "#{slug_without_count}-#{hash['value']}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: couchrest_model_slug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lucas Renan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: couchrest_model
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.0.rc1
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: stringex
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - "="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.2.1
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description:
|
38
|
+
email: contato@lucasrenan.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/couchrest/model/slug/version.rb
|
47
|
+
- lib/couchrest/model/slug.rb
|
48
|
+
- lib/couchrest_model_slug.rb
|
49
|
+
- MIT-LICENSE
|
50
|
+
- Rakefile
|
51
|
+
- Gemfile
|
52
|
+
- README.rdoc
|
53
|
+
homepage: https://github.com/lucasrenan/couchrest-model-slug
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.5
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A simple gem to generate slugs using couchrest model, based on mongoid_slug.
|
80
|
+
test_files: []
|
81
|
+
|