the_artist_formerly_known_as_mongoid_document 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 092bc3b05b2a76809bf1f724656e1902f04df519
4
+ data.tar.gz: 277758e9c228352ecf9978f93ca7d3f7b8ffd6c1
5
+ SHA512:
6
+ metadata.gz: 455f899dac192979767f1d3f50e3874fb538a0298f2b9e39ba8186179675598356d026911e437ba2e060b49b045f5f80d74ac61dac6174712bce8722bbb5e83e
7
+ data.tar.gz: 594f627e5636940d80ff7994fdf7ade0102f21bb1979c4ae1c7d718595159d54699adf8e9e4b011bf5b3650e6379121effa4f15a75a9ffe3816d0f83f8196103
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-the_artist_formerly_known_as_mongoid_document.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Steven Dunlap
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # TheArtistFormerlyKnownAsMongoidDocument
2
+
3
+ Moving away from Mongo/Mongoid in a Rails app? Still wish to honor legacy
4
+ mongo `ObjectId`s? Well do I have the solution for you!
5
+ `TheArtistFormerlyKnownAsMongoidDocument` is a simple approach that allows
6
+ `#find` methods to still look up by a mongo id.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'the_artist_formerly_known_as_mongoid_document'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install activerecord-the_artist_formerly_known_as_mongoid_document
21
+
22
+ ## Usage
23
+
24
+ As you migrate from a `Mongoid::Document` model to a new `ActiveRecord` model,
25
+ make sure to include an indexed field `mongo_id` which contains the old mongo
26
+ id. In your new model, extend `TheArtistFormerlyKnownAsMongoidDocument`.
27
+ For example:
28
+
29
+ ```ruby
30
+ require 'the_artist_formerly_known_as_mongoid_document'
31
+
32
+ class Artist < ActiveRecord::Base
33
+ extend 'TheArtistFormerlyKnownAsMongoidDocument'
34
+
35
+ # ...
36
+
37
+ end
38
+ ```
39
+
40
+ You can then use `#find` for both `mongo_id`s:
41
+
42
+ ```ruby
43
+ Artist.find('52829a3e73d8b08b00ad5d63')
44
+ ```
45
+
46
+ And of course you can still look for plain-old regular `id`s:
47
+
48
+ ```ruby
49
+ Artist.find(1)
50
+ ```
51
+
52
+ ### Note
53
+
54
+ If you are using something like [FriendlyId], it is wise to put `extend
55
+ TheArtistFormerlyKnownAsMongoidDocument` after `extend FriendlyId`.
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
65
+ [FriendlyId]: https://github.com/norman/friendly_id
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module TheArtistFormerlyKnownAsMongoidDocument
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "the_artist_formerly_known_as_mongoid_document/version"
2
+
3
+ module TheArtistFormerlyKnownAsMongoidDocument
4
+ # find by either the id proper or mongo_id
5
+ # @param *args either a single id or multiple ids
6
+ # @note currently searching by multiple mongo ids is unsupported
7
+ # @note this calls super (and therefore hits the database twice) if not found by the mongo id
8
+ # @return [TheArtistFormallyKnownAsMongoidDoument::Base] object representing the model found by either id or mongo_id
9
+ def find(*args)
10
+ id = args.first
11
+ if args.count == 1 && !id.is_a?(Numeric) && id.to_s.match( /^[0-9a-fA-F]{24}$/ )
12
+ found_by_mongo_id = where mongo_id: id
13
+ return super if found_by_mongo_id.count == 0
14
+ return found_by_mongo_id.first
15
+ else
16
+ return super
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'the_artist_formerly_known_as_mongoid_document'
3
+
4
+ describe TheArtistFormerlyKnownAsMongoidDocument do
5
+ let(:matched_mongo_id) { '507f1f77bcf86cd799439011' }
6
+ let(:unmatched_mongo_id) { '5282956f60f1e162f4742098' }
7
+ before(:each) do
8
+ build_model :artist do
9
+ string :name
10
+ string :mongo_id
11
+ end
12
+ Artist.extend(TheArtistFormerlyKnownAsMongoidDocument)
13
+ @prince = Artist.create(mongo_id: matched_mongo_id, name: 'Prince')
14
+ end
15
+
16
+ describe '.find' do
17
+ it 'should look up by mongo_id when a mongo id is supplied' do
18
+ #Artist.should_receive(:where).with(mongo_id: matched_mongo_id) { [ Artist.new( name: 'Prince', mongo_id: matched_mongo_id ) ] }
19
+ expect( Artist.find(matched_mongo_id) ).to eq(@prince)
20
+ end
21
+
22
+ it 'should look up by id when a numeric id is supplied' do
23
+ expect( Artist.find(@prince.id) ).to eq(@prince)
24
+ end
25
+
26
+ it 'should raise and error if no record exists by the mongoid id' do
27
+ expect{ Artist.find(unmatched_mongo_id) }.to raise_error( ActiveRecord::RecordNotFound )
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'acts_as_fu'
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ config.include ActsAsFu
20
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'the_artist_formerly_known_as_mongoid_document/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "the_artist_formerly_known_as_mongoid_document"
8
+ spec.version = TheArtistFormerlyKnownAsMongoidDocument::VERSION
9
+ spec.authors = ["Steven Dunlap"]
10
+ spec.email = ["steven@roadtrippers.com"]
11
+ spec.description = 'Make migrating away from Mongoid slightly less painful.'
12
+ spec.summary = 'Provide legacy support for old mongo ids in the finder methods at the model level after migrating away from Mongoid.'
13
+ spec.homepage = "http://github.com/roadtrippers/the_artist_formerly_known_as_mongoid_document"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "acts_as_fu"
25
+ spec.add_development_dependency "pry"
26
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_artist_formerly_known_as_mongoid_document
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Dunlap
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: acts_as_fu
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Make migrating away from Mongoid slightly less painful.
84
+ email:
85
+ - steven@roadtrippers.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/the_artist_formerly_known_as_mongoid_document.rb
97
+ - lib/the_artist_formerly_known_as_mongoid_document/version.rb
98
+ - spec/lib/the_artist_formally_known_as_mongoid_document_spec.rb
99
+ - spec/spec_helper.rb
100
+ - the_artist_formerly_known_as_mongoid_document.gemspec
101
+ homepage: http://github.com/roadtrippers/the_artist_formerly_known_as_mongoid_document
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Provide legacy support for old mongo ids in the finder methods at the model
125
+ level after migrating away from Mongoid.
126
+ test_files:
127
+ - spec/lib/the_artist_formally_known_as_mongoid_document_spec.rb
128
+ - spec/spec_helper.rb