zhaorong-mm-sluggable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +56 -0
  3. data/Rakefile +75 -0
  4. data/lib/mm-sluggable.rb +73 -0
  5. metadata +70 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Richard Livsey
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
@@ -0,0 +1,56 @@
1
+ = MongoMapper::Plugins::Sluggable
2
+
3
+ Tiny plugin for MongoMapper to cache a slugged version of a field
4
+
5
+ == Usage
6
+
7
+ Either load it into all models, or individual models:
8
+
9
+ # add to all models
10
+ MongoMapper::Document.append_inclusions(MongoMapper::Plugins::Sluggable)
11
+
12
+ # add to a specific model
13
+ plugin MongoMapper::Plugins::Sluggable
14
+
15
+ Then call sluggable to configure it
16
+
17
+ sluggable :title, :scope => :account_id
18
+
19
+ == Options
20
+
21
+ Available options are:
22
+
23
+ * :scope - scope to a specific field (default - nil)
24
+ * :key - what the slug key is called (default - :slug)
25
+ * :index - whether to add an index for the slug (default - true)
26
+ * :method - what method to call on the field to sluggify it (default - :parameterize)
27
+ * :callback - when to trigger the slugging (default - :before_validation_on_create)
28
+ * :force - whether to reset the slug key on the sluggable feild is changed (default - false)
29
+
30
+ Eg.
31
+
32
+ sluggable :title, :scope => :account_id, :key => :title_slug, :method => :to_url, :index => false
33
+
34
+ This will slug the title to the title_slug key, scoped to the account, will use String#to_url to slug it and won't add an index to the key
35
+
36
+ == Versioning
37
+
38
+ If an item with the same slug exists, it will add a version number to the slug.
39
+
40
+ IE assuming we already have an item with the slug of "dave", the slug will be generated as "dave-1"
41
+
42
+ == Note on Patches/Pull Requests
43
+
44
+ * Fork the project.
45
+ * Make your feature addition or bug fix.
46
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
47
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
48
+ * Send me a pull request. Bonus points for topic branches.
49
+
50
+ == Install
51
+
52
+ $ gem install mm-sluggable
53
+
54
+ == Copyright
55
+
56
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,75 @@
1
+ require "rubygems"
2
+ require "rake/gempackagetask"
3
+ require "rake/rdoctask"
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"]
14
+
15
+ # This builds the actual gem. For details of what all these options
16
+ # mean, and other ones you can add, check the documentation here:
17
+ #
18
+ # http://rubygems.org/read/chapter/20
19
+ #
20
+ spec = Gem::Specification.new do |s|
21
+
22
+ # Change these as appropriate
23
+ s.name = "zhaorong-mm-sluggable"
24
+ s.version = "0.0.1"
25
+ s.summary = "Tiny plugin for MongoMapper to cache a slugged version of a field"
26
+ s.author = "Richard Livsey"
27
+ s.email = "richard@livsey.org"
28
+ s.homepage = "http://github.com/zhaorong/mm-sluggable"
29
+
30
+ s.has_rdoc = true
31
+ s.extra_rdoc_files = %w(README.rdoc)
32
+ s.rdoc_options = %w(--main README.rdoc)
33
+
34
+ # Add any extra files to include in the gem
35
+ s.files = %w(LICENSE Rakefile README.rdoc) + Dir.glob("{spec,lib/**/*}")
36
+ s.require_paths = ["lib"]
37
+
38
+ # If you want to depend on other gems, add them here, along with any
39
+ # relevant versions
40
+ # s.add_dependency("some_other_gem", "~> 0.1.0")
41
+
42
+ # If your tests use any gems, include them here
43
+ s.add_development_dependency("rspec")
44
+ end
45
+
46
+ # This task actually builds the gem. We also regenerate a static
47
+ # .gemspec file, which is useful if something (i.e. GitHub) will
48
+ # be automatically building a gem for this project. If you're not
49
+ # using GitHub, edit as appropriate.
50
+ #
51
+ # To publish your gem online, install the 'gemcutter' gem; Read more
52
+ # about that here: http://gemcutter.org/pages/gem_docs
53
+ Rake::GemPackageTask.new(spec) do |pkg|
54
+ pkg.gem_spec = spec
55
+ end
56
+
57
+ desc "Build the gemspec file #{spec.name}.gemspec"
58
+ task :gemspec do
59
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
60
+ File.open(file, "w") {|f| f << spec.to_ruby }
61
+ end
62
+
63
+ task :package => :gemspec
64
+
65
+ # Generate documentation
66
+ Rake::RDocTask.new do |rd|
67
+ rd.main = "README.rdoc"
68
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
69
+ rd.rdoc_dir = "rdoc"
70
+ end
71
+
72
+ desc 'Clear out RDoc and generated packages'
73
+ task :clean => [:clobber_rdoc, :clobber_package] do
74
+ rm "#{spec.name}.gemspec"
75
+ end
@@ -0,0 +1,73 @@
1
+ require 'mongo_mapper'
2
+
3
+ module MongoMapper
4
+ module Plugins
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
17
+ end
18
+ end
19
+ end
20
+
21
+ module ClassMethods
22
+ def sluggable(to_slug = :title, options = {})
23
+ class_attribute :slug_options
24
+
25
+ self.slug_options = {
26
+ :to_slug => to_slug,
27
+ :key => :slug,
28
+ :index => true,
29
+ :method => :parameterize,
30
+ :scope => nil,
31
+ :callback => :before_validation_on_create,
32
+ :force => false
33
+ }.merge(options)
34
+
35
+ key slug_options[:key], String, :index => slug_options[:index]
36
+
37
+ self.send(slug_options[:callback], :set_slug)
38
+ end
39
+ end
40
+
41
+ module InstanceMethods
42
+ def set_slug
43
+ options = self.class.slug_options
44
+ need_set_slug = self.send(options[:key]).blank? || (options[:force] && self.send(:"#{options[:to_slug]}_changed?"))
45
+ return unless need_set_slug
46
+
47
+ to_slug = self[options[:to_slug]]
48
+ return if to_slug.blank?
49
+
50
+ the_slug = raw_slug = to_slug.send(options[:method]).to_s
51
+
52
+ conds = {}
53
+ conds[options[:key]] = the_slug
54
+ conds[options[:scope]] = self.send(options[:scope]) if options[:scope]
55
+
56
+ # todo - remove the loop and use regex instead so we can do it in one query
57
+ i = 0
58
+ while self.class.first(conds)
59
+ i += 1
60
+ conds[options[:key]] = the_slug = "#{raw_slug}-#{i}"
61
+ end
62
+
63
+ self.send(:"#{options[:key]}=", the_slug)
64
+ end
65
+
66
+ def to_param
67
+ options = self.class.slug_options
68
+ ( self.send(options[:key]) || self.id ).to_s
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zhaorong-mm-sluggable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Richard Livsey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-11-15 00:00:00 +08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description:
28
+ email: richard@livsey.org
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - README.rdoc
35
+ files:
36
+ - LICENSE
37
+ - Rakefile
38
+ - README.rdoc
39
+ - lib/mm-sluggable.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/zhaorong/mm-sluggable
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.6.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Tiny plugin for MongoMapper to cache a slugged version of a field
69
+ test_files: []
70
+