Skip to content

Commit f8cacc9

Browse files
committed
Cleanup and fix some duplicate methods
1 parent 9655407 commit f8cacc9

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

lib/active_admin/mongoid/document.rb

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
require 'delegate'
12
require 'meta_search/searches/mongoid'
23

34
module ActiveAdmin::Mongoid::Document
45
extend ActiveSupport::Concern
56

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
1029
end
1130
end
12-
13-
self.primary_key ||= [:_id]
1431
end
1532

1633
class Connection
@@ -23,23 +40,76 @@ def quote_column_name name
2340
end
2441
end
2542

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+
2661
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
2967
end
3068

31-
def metasearch *args, &block
69+
def group_by *args
3270
scoped
3371
end
3472

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+
3599
def columns
36-
@columns ||= fields.map(&:second)
100+
@columns ||= fields.map(&:second).map{ |c| ColumnWrapper.new(c) }
37101
end
38102

39103
def column_names
40104
@column_names ||= fields.map(&:first)
41105
end
42106

107+
def columns_hash
108+
columns.index_by(&:name)
109+
end
110+
111+
112+
43113
def reorder *args
44114
scoped
45115
end
@@ -56,6 +126,10 @@ def quoted_table_name
56126
collection_name.to_s.inspect
57127
end
58128

129+
130+
def reflections *a
131+
relations *a
132+
end
59133
end
60134
end
61135

0 commit comments

Comments
 (0)