zhaorong-mm-sluggable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +1 -1
  2. data/Rakefile +7 -11
  3. data/lib/mm-sluggable.rb +19 -16
  4. metadata +14 -3
data/README.rdoc CHANGED
@@ -7,7 +7,7 @@ Tiny plugin for MongoMapper to cache a slugged version of a field
7
7
  Either load it into all models, or individual models:
8
8
 
9
9
  # add to all models
10
- MongoMapper::Document.append_inclusions(MongoMapper::Plugins::Sluggable)
10
+ MongoMapper::Document.plugin(MongoMapper::Plugins::Sluggable)
11
11
 
12
12
  # add to a specific model
13
13
  plugin MongoMapper::Plugins::Sluggable
data/Rakefile CHANGED
@@ -2,15 +2,9 @@ require "rubygems"
2
2
  require "rake/gempackagetask"
3
3
  require "rake/rdoctask"
4
4
 
5
- # require "spec"
6
- # require "spec/rake/spectask"
7
- # Spec::Rake::SpecTask.new do |t|
8
- # t.spec_opts = %w(--format specdoc --colour)
9
- # t.libs = ["spec"]
10
- # end
11
-
12
-
13
- # task :default => ["spec"]
5
+ # require 'rspec/core/rake_task'
6
+ # RSpec::Core::RakeTask.new(:spec)
7
+ # task :default => :spec
14
8
 
15
9
  # This builds the actual gem. For details of what all these options
16
10
  # mean, and other ones you can add, check the documentation here:
@@ -21,7 +15,7 @@ spec = Gem::Specification.new do |s|
21
15
 
22
16
  # Change these as appropriate
23
17
  s.name = "zhaorong-mm-sluggable"
24
- s.version = "0.0.1"
18
+ s.version = "0.0.2"
25
19
  s.summary = "Tiny plugin for MongoMapper to cache a slugged version of a field"
26
20
  s.author = "Richard Livsey"
27
21
  s.email = "richard@livsey.org"
@@ -39,6 +33,8 @@ spec = Gem::Specification.new do |s|
39
33
  # relevant versions
40
34
  # s.add_dependency("some_other_gem", "~> 0.1.0")
41
35
 
36
+ s.add_dependency("mongo_mapper", ">= 0.9.0")
37
+
42
38
  # If your tests use any gems, include them here
43
39
  s.add_development_dependency("rspec")
44
40
  end
@@ -48,7 +44,7 @@ end
48
44
  # be automatically building a gem for this project. If you're not
49
45
  # using GitHub, edit as appropriate.
50
46
  #
51
- # To publish your gem online, install the 'gemcutter' gem; Read more
47
+ # To publish your gem online, install the 'gemcutter' gem; Read more
52
48
  # about that here: http://gemcutter.org/pages/gem_docs
53
49
  Rake::GemPackageTask.new(spec) do |pkg|
54
50
  pkg.gem_spec = spec
data/lib/mm-sluggable.rb CHANGED
@@ -3,22 +3,20 @@ require 'mongo_mapper'
3
3
  module MongoMapper
4
4
  module Plugins
5
5
  module Sluggable
6
- def self.configure(model)
7
- class << model
8
- alias_method :origin_find, :find
9
- def find(*args)
10
- arg_f = args.first
11
- if (args.size == 1) && arg_f.is_a?(String) && ( arg_f !~ /^[0-9a-f]{24}$/i )
12
- options = slug_options
13
- first options[:key] => arg_f
14
- else
15
- origin_find *args
16
- end
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ alias_method :origin_find, :find
10
+ def find(*args)
11
+ arg_f = args.first
12
+ if (args.size == 1) && arg_f.is_a?(String) && ( arg_f !~ /^[0-9a-f]{24}$/i )
13
+ options = slug_options
14
+ first options[:key] => arg_f
15
+ else
16
+ origin_find *args
17
17
  end
18
18
  end
19
- end
20
19
 
21
- module ClassMethods
22
20
  def sluggable(to_slug = :title, options = {})
23
21
  class_attribute :slug_options
24
22
 
@@ -28,13 +26,18 @@ module MongoMapper
28
26
  :index => true,
29
27
  :method => :parameterize,
30
28
  :scope => nil,
31
- :callback => :before_validation_on_create,
29
+ :max_length => 256,
30
+ :callback => [:before_validation, {:on => :create}],
32
31
  :force => false
33
32
  }.merge(options)
34
33
 
35
34
  key slug_options[:key], String, :index => slug_options[:index]
36
35
 
37
- self.send(slug_options[:callback], :set_slug)
36
+ if slug_options[:callback].is_a?(Array)
37
+ self.send(slug_options[:callback][0], :set_slug, slug_options[:callback][1])
38
+ else
39
+ self.send(slug_options[:callback], :set_slug)
40
+ end
38
41
  end
39
42
  end
40
43
 
@@ -47,7 +50,7 @@ module MongoMapper
47
50
  to_slug = self[options[:to_slug]]
48
51
  return if to_slug.blank?
49
52
 
50
- the_slug = raw_slug = to_slug.send(options[:method]).to_s
53
+ the_slug = raw_slug = to_slug.send(options[:method]).to_s[0...options[:max_length]]
51
54
 
52
55
  conds = {}
53
56
  conds[options[:key]] = the_slug
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zhaorong-mm-sluggable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Richard Livsey
@@ -14,16 +14,27 @@ date: 2011-11-15 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: rspec
17
+ name: mongo_mapper
18
18
  prerelease: false
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.9.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
20
31
  none: false
21
32
  requirements:
22
33
  - - ">="
23
34
  - !ruby/object:Gem::Version
24
35
  version: "0"
25
36
  type: :development
26
- version_requirements: *id001
37
+ version_requirements: *id002
27
38
  description:
28
39
  email: richard@livsey.org
29
40
  executables: []