forked from mongomapper/mongomapper
/
rails.rb
60 lines (49 loc) · 1.42 KB
/
rails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# encoding: UTF-8
module MongoMapper
module Plugins
module Rails
autoload :ActiveRecordAssociationAdapter, "mongo_mapper/plugins/rails/active_record_association_adapter"
extend ActiveSupport::Concern
module InstanceMethods
def to_param
id.to_s if persisted?
end
def to_model
self
end
def to_key
[id] if persisted?
end
def new_record?
new?
end
def read_attribute(name)
self[name]
end
def read_attribute_before_type_cast(name)
read_key_before_type_cast(name)
end
def write_attribute(name, value)
self[name] = value
end
end
module ClassMethods
def has_one(*args)
one(*args)
end
def has_many(*args, &extension)
many(*args, &extension)
end
def column_names
keys.keys
end
# Returns returns an ActiveRecordAssociationAdapter for an association. This adapter has an API that is a
# subset of ActiveRecord::Reflection::AssociationReflection. This allows MongoMapper to be used with the
# association helpers in gems like simple_form and formtastic.
def reflect_on_association(name)
ActiveRecordAssociationAdapter.for_association(associations[name]) if associations[name]
end
end
end
end
end