mongoid_param 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,106 @@
1
+ = Mongoid::Param
2
+
3
+ Addon to Mongoid that allows you to easily generate and query against human readable (parameterized) id's!
4
+
5
+ == Installation
6
+
7
+ Add to Gemfile:
8
+
9
+ gem 'mongoid_param'
10
+
11
+ == Getting Started
12
+
13
+ To parameterize a model, you only need to include the module in your model and then call param with the field(s) you want to parameterize on. Here's an example:
14
+
15
+ class User
16
+ include Mongoid::Document
17
+ include Mongoid::Param
18
+
19
+ field :username
20
+ field :dob, :type => Date
21
+
22
+ param :username
23
+ end
24
+
25
+ == Parameterizing on Multiple Fields
26
+
27
+ You can pass multiple symbols to the *param* method to parameterize on multiple fields:
28
+
29
+ class Post
30
+ include Mongoid::Document
31
+ include Mongoid::Param
32
+
33
+ field :year
34
+ field :month
35
+ field :day
36
+ field :title
37
+
38
+ param :year, :month, :day, :title
39
+ end
40
+
41
+ == Customizing your Param values
42
+
43
+ You can easily customize your param value by passing a method to param:
44
+
45
+ class Message
46
+ include Mongoid::Document
47
+ include Mongoid::Timestamps
48
+ include Mongoid::Param
49
+
50
+ field :title
51
+ param :generate_param
52
+
53
+ private
54
+
55
+ def generate_param
56
+ created_at ||= Time.now # necessary because we're called before validation
57
+ "#{created_at.year} #{created_at.month} #{created_at.day} #{title}"
58
+ end
59
+ end
60
+
61
+ == Behind the Scenes
62
+
63
+ >> u = User.new :username => 'Bob', :dob => Date.today
64
+
65
+ >> u.to_param
66
+ => "bob"
67
+
68
+ >> u.update_attributes :username => 'Johnny Walker'
69
+
70
+ >> u.to_param
71
+ => "johnny_walker"
72
+
73
+ >> p = Post.new :title => 'Big Announcement', :year => 2010, :month => 2, :day => 13
74
+
75
+ >> p.to_param
76
+ => "2010_2_13_big_announcement"
77
+
78
+ >> p.update_attributes :month => 10
79
+
80
+ >> p.to_param
81
+ => "2010_10_13_big_announcement"
82
+
83
+ == Usage in Rails
84
+
85
+ Mongoid::Param was designed to easily work with Rails.
86
+
87
+ In your controller:
88
+
89
+ class UserController < ApplicationController
90
+ ...
91
+ def show
92
+ @user = User.find_by_param(params[:id])
93
+ end
94
+ ...
95
+ end
96
+
97
+ In your view:
98
+
99
+ link_to @user.username, @user # => /users/bob
100
+
101
+ == Finding by Parameters
102
+
103
+ Included is a simple helper method that allows you to easily fetch a record by its _param field:
104
+
105
+ @user = User.find_by_param('bob')
106
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,43 @@
1
+ # Generates and allows you to query against a human-friendly name for your records.
2
+ module Mongoid
3
+
4
+ module Param
5
+
6
+ def self.included(base)
7
+ base.send :extend, ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def param(*on_fields)
13
+ cattr_accessor :param_fields
14
+ self.param_fields = on_fields
15
+ field :_param, :type => String
16
+ index :_param, :unique => true
17
+ validates :_param, :presence => true, :uniqueness => true,
18
+ :format => { :with => /[-a-z0-9_]+/ },
19
+ :exclusion => { :in => %w[index show new create edit update destroy delete] }
20
+ before_validation :make_param
21
+
22
+ send :include, InstanceMethods
23
+ end
24
+
25
+ def find_by_param(value)
26
+ where(:_param => value).first
27
+ end
28
+
29
+ end
30
+
31
+ module InstanceMethods
32
+ def make_param
33
+ p = (self.param_fields.collect { |f| self.send(f) }.join(" ") || self._id).gsub(/('|")/, '')
34
+ self._param = p.parameterize('_')
35
+ end
36
+
37
+ def to_param
38
+ _param
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include Mongoid::Param
4
+
5
+ field :year
6
+ field :month
7
+ field :day
8
+ field :title
9
+
10
+ param :year, :month, :day, :title
11
+ end
@@ -0,0 +1,9 @@
1
+ class User
2
+ include Mongoid::Document
3
+ include Mongoid::Param
4
+
5
+ field :username
6
+ field :dob, :type => Date
7
+
8
+ param :username
9
+ end
@@ -0,0 +1,80 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Param do
4
+
5
+ context "user" do
6
+
7
+ before(:each) do
8
+ @bob = User.create(:username => 'Bob', :dob => Date.today)
9
+ @nancy = User.create(:username => 'Nancy', :dob => Date.today)
10
+ end
11
+
12
+ it "generates param" do
13
+ @bob.to_param.should eql 'bob'
14
+ end
15
+
16
+ it "updates the param" do
17
+ @bob.update_attributes :username => 'Bobby'
18
+ @bob.to_param.should eql 'bobby'
19
+ end
20
+
21
+ it "requires a unique param" do
22
+ @bob.update_attributes :username => 'Nancy'
23
+ @bob.errors['_param'].include?('is already taken').should eql true
24
+ end
25
+
26
+ it "finds by param" do
27
+ u = User.find_by_param('bob')
28
+ u.blank?.should eql false
29
+ end
30
+
31
+ it "does not find nonexistent things" do
32
+ u = User.find_by_param('someone')
33
+ u.blank?.should eql true
34
+ end
35
+
36
+ it "junks bad characters" do
37
+ u = User.create(:username => '(Alien) Cyborg Warfare #3')
38
+ u.to_param.should eql 'alien_cyborg_warfare_3'
39
+ end
40
+
41
+ end
42
+
43
+ context "post" do
44
+
45
+ before(:each) do
46
+ @big = Post.create(:year => 2010, :month => 06, :day => 21, :title => 'Big Announcement')
47
+ @big_two = Post.create(:year => 2011, :month => 06, :day => 21, :title => 'Big Announcement')
48
+ @little = Post.create(:year => 2009, :month => 02, :day => 04, :title => 'Little Announcement')
49
+ end
50
+
51
+ it "has the proper entires" do
52
+ Post.count.should eql 3
53
+ end
54
+
55
+ it "has globbed params" do
56
+ @big.to_param.should eql "2010_6_21_big_announcement"
57
+ @big_two.to_param.should eql "2011_6_21_big_announcement"
58
+ @little.to_param.should eql "2009_2_4_little_announcement"
59
+ end
60
+
61
+ it "requires a unique param" do
62
+ @big_two.update_attributes :year => 2010
63
+ @big_two.errors['_param'].include?('is already taken').should eql true
64
+ end
65
+
66
+ it "finds by param" do
67
+ p = Post.find_by_param('2010_6_21_big_announcement')
68
+ p.blank?.should eql false
69
+ end
70
+
71
+ it "does not find nonexistent things" do
72
+ p = Post.find_by_param('2010_5_3_some_post')
73
+ p.blank?.should eql true
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+
@@ -0,0 +1,25 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+
4
+ Bundler.require(:default)
5
+
6
+ Mongoid.config.master = Mongo::Connection.new.db("mongoid_param_test")
7
+
8
+ require File.expand_path("../../lib/mongoid_param", __FILE__)
9
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
10
+
11
+ DatabaseCleaner.orm = "mongoid"
12
+
13
+ Rspec.configure do |config|
14
+ config.before(:all) do
15
+ DatabaseCleaner.strategy = :truncation
16
+ end
17
+
18
+ config.before(:each) do
19
+ DatabaseCleaner.start
20
+ end
21
+
22
+ config.after(:each) do
23
+ DatabaseCleaner.clean
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_param
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Coene
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-09 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongoid
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: -1848230040
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ - beta4
35
+ version: 2.0.0.beta4
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: Addon to Mongoid that allows you to easily generate and query against human readable (parameterized) id's!
39
+ email: jcoene@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.rdoc
46
+ files:
47
+ - README.rdoc
48
+ - VERSION
49
+ - lib/mongoid_param.rb
50
+ - spec/models/post.rb
51
+ - spec/models/user.rb
52
+ - spec/mongoid_param_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/jcoene/mongoid_param
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Parameterize your models in one line
88
+ test_files:
89
+ - spec/models/post.rb
90
+ - spec/models/user.rb
91
+ - spec/mongoid_param_spec.rb
92
+ - spec/spec_helper.rb