1
+ require 'delegate'
1
2
require 'meta_search/searches/mongoid'
2
3
3
4
module ActiveAdmin ::Mongoid ::Document
4
5
extend ActiveSupport ::Concern
5
6
6
- included do
7
- unless respond_to? :primary_key
8
- class << self
9
- attr_accessor :primary_key
7
+
8
+
9
+
10
+ # INSTANCE METHODS
11
+
12
+ # Returns the column object for the named attribute.
13
+ def column_for_attribute ( name )
14
+ self . class . columns_hash [ name . to_s ]
15
+ end
16
+
17
+
18
+
19
+
20
+ # PROXY CLASSES
21
+
22
+ class ColumnWrapper < SimpleDelegator
23
+ def type
24
+ _super = super
25
+ case _super
26
+ when Object ; String
27
+ when Moped ::BSON ::ObjectId ; String
28
+ else _super
10
29
end
11
30
end
12
-
13
- self . primary_key ||= [ :_id ]
14
31
end
15
32
16
33
class Connection
@@ -23,23 +40,76 @@ def quote_column_name name
23
40
end
24
41
end
25
42
43
+
44
+
45
+
46
+ # CLASS METHODS
47
+
48
+ included do
49
+ include MetaSearch ::Searches ::Mongoid
50
+
51
+ unless respond_to? :primary_key
52
+ class << self
53
+ attr_accessor :primary_key
54
+ end
55
+ end
56
+
57
+ self . primary_key ||= [ :_id ]
58
+
59
+ end
60
+
26
61
module ClassMethods
27
- def content_columns
28
- @content_columns ||= fields . map ( &:second ) . select { |f | f . name !~ /(^_|^(created|updated)_at)/ }
62
+
63
+ # Metasearch
64
+
65
+ def joins_values *args
66
+ scoped
29
67
end
30
68
31
- def metasearch *args , & block
69
+ def group_by *args
32
70
scoped
33
71
end
34
72
73
+
74
+
75
+ # Cache
76
+
77
+ def [] name
78
+ raise name . inspect
79
+ cache [ name ]
80
+ end
81
+
82
+ def []= name , value
83
+ cache [ name ] = value
84
+ end
85
+
86
+ def cache
87
+ @cache ||= { }
88
+ end
89
+
90
+
91
+ # Columns
92
+
93
+ def content_columns
94
+ @content_columns ||= fields . map ( &:second ) . reject do |f |
95
+ f . name =~ /(^_|^(created|updated)_at)/ or Mongoid ::Fields ::ForeignKey === f
96
+ end
97
+ end
98
+
35
99
def columns
36
- @columns ||= fields . map ( &:second )
100
+ @columns ||= fields . map ( &:second ) . map { | c | ColumnWrapper . new ( c ) }
37
101
end
38
102
39
103
def column_names
40
104
@column_names ||= fields . map ( &:first )
41
105
end
42
106
107
+ def columns_hash
108
+ columns . index_by ( &:name )
109
+ end
110
+
111
+
112
+
43
113
def reorder *args
44
114
scoped
45
115
end
@@ -56,6 +126,10 @@ def quoted_table_name
56
126
collection_name . to_s . inspect
57
127
end
58
128
129
+
130
+ def reflections *a
131
+ relations *a
132
+ end
59
133
end
60
134
end
61
135
0 commit comments