-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
base_dashboard.rb
113 lines (91 loc) · 2.59 KB
/
base_dashboard.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require "administrate/field/belongs_to"
require "administrate/field/boolean"
require "administrate/field/date_time"
require "administrate/field/email"
require "administrate/field/has_many"
require "administrate/field/has_one"
require "administrate/field/number"
require "administrate/field/polymorphic"
require "administrate/field/select"
require "administrate/field/string"
require "administrate/field/text"
require "administrate/field/time"
require "administrate/field/url"
require "administrate/field/password"
module Administrate
class BaseDashboard
include Administrate
class << self
def configuration
@configuration ||= Configuration.new
end
def configure
yield configuration
end
def display_in_navigation?
configuration.navigation == true
end
end
def attribute_types
self.class::ATTRIBUTE_TYPES
end
def attribute_type_for(attribute_name)
attribute_types.fetch(attribute_name) do
fail attribute_not_found_message(attribute_name)
end
end
def attribute_types_for(attribute_names)
attribute_names.each_with_object({}) do |name, attributes|
attributes[name] = attribute_type_for(name)
end
end
def all_attributes
attribute_types.keys
end
def form_attributes
self.class::FORM_ATTRIBUTES
end
def permitted_attributes
form_attributes.map do |attr|
attribute_types[attr].permitted_attribute(attr)
end.uniq
end
def show_page_attributes
self.class::SHOW_PAGE_ATTRIBUTES
end
def collection_attributes
self.class::COLLECTION_ATTRIBUTES
end
def display_resource(resource)
"#{resource.class} ##{resource.id}"
end
def collection_includes
attribute_includes(collection_attributes)
end
def item_includes
attribute_includes(show_page_attributes)
end
private
def attribute_not_found_message(attr)
"Attribute #{attr} could not be found in #{self.class}::ATTRIBUTE_TYPES"
end
def association_classes
@association_classes ||=
ObjectSpace.each_object(Class).
select { |klass| klass < Administrate::Field::Associative }
end
def attribute_includes(attributes)
attributes.map do |key|
field = self.class::ATTRIBUTE_TYPES[key]
next key if association_classes.include?(field)
key if association_classes.include?(field.try(:deferred_class))
end.compact
end
class Configuration
attr_accessor :navigation
def initialize
@navigation = true
end
end
end
end