orm_adapter_rails4_fix 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock.development +129 -0
- data/History.txt +58 -0
- data/LICENSE +20 -0
- data/README.rdoc +71 -0
- data/Rakefile +37 -0
- data/lib/orm_adapter.rb +15 -0
- data/lib/orm_adapter/adapters/active_record.rb +76 -0
- data/lib/orm_adapter/adapters/data_mapper.rb +57 -0
- data/lib/orm_adapter/adapters/mongo_mapper.rb +65 -0
- data/lib/orm_adapter/adapters/mongoid.rb +63 -0
- data/lib/orm_adapter/base.rb +127 -0
- data/lib/orm_adapter/to_adapter.rb +8 -0
- data/lib/orm_adapter/version.rb +3 -0
- data/orm_adapter.gemspec +35 -0
- data/spec/orm_adapter/adapters/active_record_spec.rb +62 -0
- data/spec/orm_adapter/adapters/data_mapper_spec.rb +47 -0
- data/spec/orm_adapter/adapters/mongo_mapper_spec.rb +41 -0
- data/spec/orm_adapter/adapters/mongoid_spec.rb +39 -0
- data/spec/orm_adapter/base_spec.rb +81 -0
- data/spec/orm_adapter/example_app_shared.rb +240 -0
- data/spec/orm_adapter_spec.rb +13 -0
- data/spec/spec_helper.rb +15 -0
- metadata +290 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
orm_adapter (0.4.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.1.3)
|
10
|
+
activesupport (= 3.1.3)
|
11
|
+
builder (~> 3.0.0)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
activerecord (3.1.3)
|
14
|
+
activemodel (= 3.1.3)
|
15
|
+
activesupport (= 3.1.3)
|
16
|
+
arel (~> 2.2.1)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.1.3)
|
19
|
+
multi_json (~> 1.0)
|
20
|
+
addressable (2.2.6)
|
21
|
+
arel (2.2.1)
|
22
|
+
bcrypt-ruby (3.0.1)
|
23
|
+
bson (1.5.2)
|
24
|
+
bson_ext (1.5.2)
|
25
|
+
bson (= 1.5.2)
|
26
|
+
builder (3.0.0)
|
27
|
+
data_objects (0.10.7)
|
28
|
+
addressable (~> 2.1)
|
29
|
+
datamapper (1.2.0)
|
30
|
+
dm-aggregates (~> 1.2.0)
|
31
|
+
dm-constraints (~> 1.2.0)
|
32
|
+
dm-core (~> 1.2.0)
|
33
|
+
dm-migrations (~> 1.2.0)
|
34
|
+
dm-serializer (~> 1.2.0)
|
35
|
+
dm-timestamps (~> 1.2.0)
|
36
|
+
dm-transactions (~> 1.2.0)
|
37
|
+
dm-types (~> 1.2.0)
|
38
|
+
dm-validations (~> 1.2.0)
|
39
|
+
diff-lcs (1.1.3)
|
40
|
+
dm-active_model (1.2.0)
|
41
|
+
activemodel (~> 3.1.0)
|
42
|
+
dm-core (~> 1.2.0)
|
43
|
+
dm-aggregates (1.2.0)
|
44
|
+
dm-core (~> 1.2.0)
|
45
|
+
dm-constraints (1.2.0)
|
46
|
+
dm-core (~> 1.2.0)
|
47
|
+
dm-core (1.2.0)
|
48
|
+
addressable (~> 2.2.6)
|
49
|
+
dm-do-adapter (1.2.0)
|
50
|
+
data_objects (~> 0.10.6)
|
51
|
+
dm-core (~> 1.2.0)
|
52
|
+
dm-migrations (1.2.0)
|
53
|
+
dm-core (~> 1.2.0)
|
54
|
+
dm-serializer (1.2.1)
|
55
|
+
dm-core (~> 1.2.0)
|
56
|
+
fastercsv (~> 1.5.4)
|
57
|
+
json (~> 1.6.1)
|
58
|
+
json_pure (~> 1.6.1)
|
59
|
+
multi_json (~> 1.0.3)
|
60
|
+
dm-sqlite-adapter (1.2.0)
|
61
|
+
dm-do-adapter (~> 1.2.0)
|
62
|
+
do_sqlite3 (~> 0.10.6)
|
63
|
+
dm-timestamps (1.2.0)
|
64
|
+
dm-core (~> 1.2.0)
|
65
|
+
dm-transactions (1.2.0)
|
66
|
+
dm-core (~> 1.2.0)
|
67
|
+
dm-types (1.2.1)
|
68
|
+
bcrypt-ruby (~> 3.0.0)
|
69
|
+
dm-core (~> 1.2.0)
|
70
|
+
fastercsv (~> 1.5.4)
|
71
|
+
json (~> 1.6.1)
|
72
|
+
multi_json (~> 1.0.3)
|
73
|
+
stringex (~> 1.3.0)
|
74
|
+
uuidtools (~> 2.1.2)
|
75
|
+
dm-validations (1.2.0)
|
76
|
+
dm-core (~> 1.2.0)
|
77
|
+
do_sqlite3 (0.10.7)
|
78
|
+
data_objects (= 0.10.7)
|
79
|
+
fastercsv (1.5.4)
|
80
|
+
git (1.2.5)
|
81
|
+
i18n (0.6.0)
|
82
|
+
json (1.6.4)
|
83
|
+
json_pure (1.6.4)
|
84
|
+
mongo (1.5.2)
|
85
|
+
bson (= 1.5.2)
|
86
|
+
mongo_mapper (0.10.1)
|
87
|
+
activemodel (~> 3.0)
|
88
|
+
activesupport (~> 3.0)
|
89
|
+
plucky (~> 0.4.0)
|
90
|
+
mongoid (2.4.0)
|
91
|
+
activemodel (~> 3.1)
|
92
|
+
mongo (~> 1.3)
|
93
|
+
tzinfo (~> 0.3.22)
|
94
|
+
multi_json (1.0.4)
|
95
|
+
plucky (0.4.3)
|
96
|
+
mongo (~> 1.3)
|
97
|
+
rake (0.9.2.2)
|
98
|
+
rspec (2.8.0)
|
99
|
+
rspec-core (~> 2.8.0)
|
100
|
+
rspec-expectations (~> 2.8.0)
|
101
|
+
rspec-mocks (~> 2.8.0)
|
102
|
+
rspec-core (2.8.0)
|
103
|
+
rspec-expectations (2.8.0)
|
104
|
+
diff-lcs (~> 1.1.2)
|
105
|
+
rspec-mocks (2.8.0)
|
106
|
+
sqlite3 (1.3.5)
|
107
|
+
stringex (1.3.0)
|
108
|
+
tzinfo (0.3.31)
|
109
|
+
uuidtools (2.1.2)
|
110
|
+
yard (0.7.4)
|
111
|
+
|
112
|
+
PLATFORMS
|
113
|
+
ruby
|
114
|
+
|
115
|
+
DEPENDENCIES
|
116
|
+
activerecord (>= 3.0.0)
|
117
|
+
bson_ext (>= 1.3.0)
|
118
|
+
bundler (>= 1.0.0)
|
119
|
+
datamapper (>= 1.0)
|
120
|
+
dm-active_model (>= 1.0)
|
121
|
+
dm-sqlite-adapter (>= 1.0)
|
122
|
+
git (>= 1.2.5)
|
123
|
+
mongo_mapper (>= 0.9.0)
|
124
|
+
mongoid (>= 2.0.0.beta.20)
|
125
|
+
orm_adapter!
|
126
|
+
rake (>= 0.8.7)
|
127
|
+
rspec (>= 2.4.0)
|
128
|
+
sqlite3 (>= 1.3.2)
|
129
|
+
yard (>= 0.6.0)
|
data/History.txt
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
== 0.4.0
|
2
|
+
|
3
|
+
* Adds :limit, and :offset options to #find_all [Fred Wu]
|
4
|
+
|
5
|
+
== 0.3.0
|
6
|
+
|
7
|
+
* Removes OrmAdapter::Base.model_classes and friends. This is a BC breaking change if you use .model_classes [Ian White]
|
8
|
+
* Add note about the scope of ORM Adapter re: model instance methods
|
9
|
+
|
10
|
+
== 0.2.0
|
11
|
+
|
12
|
+
* Normalise :id in find conditions [Tim Galeckas, Ian White]
|
13
|
+
* Allow find_first and find_all to take no arguments [Tim Galeckas, Ian White]
|
14
|
+
|
15
|
+
== 0.1.0
|
16
|
+
|
17
|
+
* Add #destroy(object) to the API [Fred Wu]
|
18
|
+
|
19
|
+
|
20
|
+
== 0.0.7
|
21
|
+
|
22
|
+
* Lazy load Active Record [José Valim]
|
23
|
+
|
24
|
+
|
25
|
+
== 0.0.6
|
26
|
+
|
27
|
+
* Active Record 3.1 association compatibility [Ian White]
|
28
|
+
* Compatibility with mongoid master wrt. OrmAdapter::Base#get [Frank Wöckener]. Closes #9 [Ian White]
|
29
|
+
|
30
|
+
|
31
|
+
== 0.0.5
|
32
|
+
|
33
|
+
* Adds Mongo Mapper adapter [Luke Cunningham]
|
34
|
+
|
35
|
+
|
36
|
+
== 0.0.4
|
37
|
+
|
38
|
+
* Adds ability to order results from #find_first and #find_all [Ian White]
|
39
|
+
* Rationalise development dependencies and release process [Ian White]
|
40
|
+
|
41
|
+
|
42
|
+
== 0.0.3
|
43
|
+
|
44
|
+
* ActiveRecord's OrmAdapter handles non standard foreign key names [Ian White]
|
45
|
+
* fix ruby 1.9.2 problems. [Ian White]
|
46
|
+
- removes "can't add a new key into hash during iteration" problem with ActiveRecord OrmAdapter
|
47
|
+
- removes problem with rspec 2 mocks interacting with ruby 1.9.2 #to_ary
|
48
|
+
|
49
|
+
|
50
|
+
== 0.0.2
|
51
|
+
|
52
|
+
* Add #get to the API. Ensure both #get and #get! complies with to_key requirements. [José Valim]
|
53
|
+
* Extract tests into shared example. Give instructions on how to write a new adapter. [Ian White]
|
54
|
+
|
55
|
+
|
56
|
+
== 0.0.1
|
57
|
+
|
58
|
+
* Initial release [Ian White, José Valim]
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2012 Ian White and José Valim
|
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,71 @@
|
|
1
|
+
= ORM Adapter {<img src="https://secure.travis-ci.org/ianwhite/orm_adapter.png?branch=master" alt="Build Status" />}[http://travis-ci.org/ianwhite/orm_adapter]
|
2
|
+
|
3
|
+
Provides a single point of entry for popular ruby ORMs. Its target audience is gem authors who want to support more than one ORM.
|
4
|
+
|
5
|
+
== Example of use
|
6
|
+
|
7
|
+
require 'orm_adapter'
|
8
|
+
|
9
|
+
User # is it an ActiveRecord, DM Resource, MongoMapper or MongoId Document?
|
10
|
+
|
11
|
+
User.to_adapter.find_first :name => 'Fred' # we don't care!
|
12
|
+
|
13
|
+
user_model = User.to_adapter
|
14
|
+
user_model.get!(1) # find a record by id
|
15
|
+
user_model.find_first(:name => 'fred') # find first fred
|
16
|
+
user_model.find_first(:level => 'awesome', :id => 23)
|
17
|
+
# find user 23, only if it's level is awesome
|
18
|
+
user_model.find_all # find all users
|
19
|
+
user_model.find_all(:name => 'fred') # find all freds
|
20
|
+
user_model.find_all(:order => :name) # find all freds, ordered by name
|
21
|
+
user_model.create!(:name => 'fred') # create a fred
|
22
|
+
user_model.destroy(object) # destroy the user object
|
23
|
+
|
24
|
+
|
25
|
+
@see OrmAdapter::Base for more details of the supported API
|
26
|
+
|
27
|
+
== Supported ORMs
|
28
|
+
|
29
|
+
Currently supported ORMs are *ActiveRecord*, *DataMapper*, *MongoMapper*, and *MongoId*.
|
30
|
+
|
31
|
+
We welcome you to write new adapters as gems. ORM Adapter will stay focused in having these major ORMs working.
|
32
|
+
|
33
|
+
To write an adapter look at <tt>lib/orm_adapter/adapters/active_record.rb</tt> for an example of implementation. To see how to test it, look at <tt>spec/orm_adapter/example_app_shared.rb</tt>, <tt>spec/orm_adapter/adapters/active_record_spec.rb</tt>. You'll need to require the target ORM in <tt>spec/spec_helper.rb</tt>
|
34
|
+
|
35
|
+
|
36
|
+
== Goals
|
37
|
+
|
38
|
+
ORM Adapter's goal is to support a minimum API used by most of the plugins that needs agnosticism beyond Active Model.
|
39
|
+
|
40
|
+
ORM Adapter will support only basic methods, as +get+, +find_first+, <tt>create!</tt> and so forth. It is not ORM Adapter's goal to support different query constructions, handle table joins, etc.
|
41
|
+
|
42
|
+
ORM adapter provides a consistent API for these basic class or 'factory' methods. It does not attempt to unify the behaviour of model instances returned by these methods. This means that unifying the behaviour of methods such as `model.save`, and `model.valid?` is beyond the scope of orm_adapter.
|
43
|
+
|
44
|
+
If you need complex queries, we recommend you to subclass ORM Adapters in your plugin and extend it expressing these query conditions as part of your domain logic.
|
45
|
+
|
46
|
+
== History
|
47
|
+
|
48
|
+
orm_adapter is an extraction from {pickle}[http://github.com/ianwhite/pickle] by {Ian White}[http://github.com/ianwhite]. Pickle's orm adapter included work by {Daniel Neighman}[http://github.com/hassox], {Josh Bassett}[http://github.com/nullobject], {Marc Lee}[http://github.com/maleko], and {Sebastian Zuchmanski}[http://github.com/sebcioz].
|
49
|
+
|
50
|
+
{José Valim}[http://github.com/josevalim] suggested the extraction, and worked on the first release with Ian.
|
51
|
+
|
52
|
+
{Luke Cunningham}[http://github.com/icaruswings] contributes the Mongo Mapper adapter.
|
53
|
+
|
54
|
+
{Fred Wu}[http://github.com/fredwu] contributes the #destroy methods, and :limit, :offset options for #find_all
|
55
|
+
|
56
|
+
{Tim Galeckas}[http://github.com/timgaleckas]
|
57
|
+
|
58
|
+
== Development
|
59
|
+
|
60
|
+
To run the specs, you can start from the last known good set of gem dependencies in Gemfile.lock.development:
|
61
|
+
|
62
|
+
git clone http://github.com/ianwhite/orm_adapter
|
63
|
+
cd orm_adapter
|
64
|
+
cp Gemfile.lock.development Gemfile.lock
|
65
|
+
bundle
|
66
|
+
bundle exec rake spec
|
67
|
+
|
68
|
+
|
69
|
+
== Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2010-2012 Ian White and José Valim. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rake'
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
$:.push File.expand_path("../lib", __FILE__)
|
11
|
+
require "orm_adapter/version"
|
12
|
+
|
13
|
+
task :default => :spec
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(:spec)
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'yard'
|
19
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
20
|
+
t.files = ['lib/**/*.rb', 'README.rdoc']
|
21
|
+
end
|
22
|
+
rescue LoadError
|
23
|
+
task :doc do
|
24
|
+
puts "install yard to generate the docs"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
task :release => :check_gemfile
|
31
|
+
|
32
|
+
task :check_gemfile do
|
33
|
+
if File.exists?("Gemfile.lock") && File.read("Gemfile.lock") != File.read("Gemfile.lock.development")
|
34
|
+
cp "Gemfile.lock", "Gemfile.lock.development"
|
35
|
+
raise "** Gemfile.lock.development has been updated, please commit these changes."
|
36
|
+
end
|
37
|
+
end
|
data/lib/orm_adapter.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'orm_adapter/base'
|
2
|
+
require 'orm_adapter/to_adapter'
|
3
|
+
require 'orm_adapter/version'
|
4
|
+
|
5
|
+
module OrmAdapter
|
6
|
+
# A collection of registered adapters
|
7
|
+
def self.adapters
|
8
|
+
@@adapters ||= []
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'orm_adapter/adapters/active_record' if defined?(ActiveRecord::Base)
|
13
|
+
require 'orm_adapter/adapters/data_mapper' if defined?(DataMapper::Resource)
|
14
|
+
require 'orm_adapter/adapters/mongoid' if defined?(Mongoid::Document)
|
15
|
+
require 'orm_adapter/adapters/mongo_mapper' if defined?(MongoMapper::Document)
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module OrmAdapter
|
4
|
+
class ActiveRecord < Base
|
5
|
+
# Return list of column/property names
|
6
|
+
def column_names
|
7
|
+
klass.column_names
|
8
|
+
end
|
9
|
+
|
10
|
+
# @see OrmAdapter::Base#get!
|
11
|
+
def get!(id)
|
12
|
+
klass.find(wrap_key(id))
|
13
|
+
end
|
14
|
+
|
15
|
+
# @see OrmAdapter::Base#get
|
16
|
+
def get(id)
|
17
|
+
klass.where(klass.primary_key => wrap_key(id)).first
|
18
|
+
end
|
19
|
+
|
20
|
+
# @see OrmAdapter::Base#find_first
|
21
|
+
def find_first(options = {})
|
22
|
+
conditions, order = extract_conditions!(options)
|
23
|
+
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
|
24
|
+
end
|
25
|
+
|
26
|
+
# @see OrmAdapter::Base#find_all
|
27
|
+
def find_all(options = {})
|
28
|
+
conditions, order, limit, offset = extract_conditions!(options)
|
29
|
+
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).offset(offset).all
|
30
|
+
end
|
31
|
+
|
32
|
+
# @see OrmAdapter::Base#create!
|
33
|
+
def create!(attributes = {})
|
34
|
+
klass.create!(attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @see OrmAdapter::Base#destroy
|
38
|
+
def destroy(object)
|
39
|
+
object.destroy && true if valid_object?(object)
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
# Introspects the klass to convert and objects in conditions into foreign key and type fields
|
45
|
+
def conditions_to_fields(conditions)
|
46
|
+
fields = {}
|
47
|
+
conditions.each do |key, value|
|
48
|
+
if value.is_a?(::ActiveRecord::Base) && (assoc = klass.reflect_on_association(key.to_sym)) && assoc.belongs_to?
|
49
|
+
|
50
|
+
if ::ActiveRecord::VERSION::STRING < "3.1"
|
51
|
+
fields[assoc.primary_key_name] = value.send(value.class.primary_key)
|
52
|
+
fields[assoc.options[:foreign_type]] = value.class.base_class.name.to_s if assoc.options[:polymorphic]
|
53
|
+
else # >= 3.1
|
54
|
+
fields[assoc.foreign_key] = value.send(value.class.primary_key)
|
55
|
+
fields[assoc.foreign_type] = value.class.base_class.name.to_s if assoc.options[:polymorphic]
|
56
|
+
end
|
57
|
+
|
58
|
+
else
|
59
|
+
fields[key] = value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
fields
|
63
|
+
end
|
64
|
+
|
65
|
+
def order_clause(order)
|
66
|
+
order.map {|pair| "#{pair[0]} #{pair[1]}"}.join(",")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
ActiveSupport.on_load(:active_record) do
|
72
|
+
included do
|
73
|
+
extend ::OrmAdapter::ToAdapter
|
74
|
+
self::OrmAdapter = ::OrmAdapter::ActiveRecord
|
75
|
+
end
|
76
|
+
end
|