/
application.rb
291 lines (230 loc) · 9.4 KB
/
application.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require 'active_admin/router'
require 'active_admin/helpers/settings'
module ActiveAdmin
class Application
include Settings
# Adds settings to both the Application and the Namespace instance
# so that they can be configured independantly.
def self.inheritable_setting(name, default)
Namespace.setting name, nil
setting name, default
end
# The default namespace to put controllers and routes inside. Set this
# in config/initializers/active_admin.rb using:
#
# config.default_namespace = :super_admin
#
setting :default_namespace, :admin
# A hash of all the registered namespaces
setting :namespaces, {}
# Load paths for admin configurations. Add folders to this load path
# to load up other resources for administration. External gems can
# include their paths in this load path to provide active_admin UIs
setting :load_paths, [File.expand_path('app/admin', Rails.root)]
# The default number of resources to display on index pages
inheritable_setting :default_per_page, 30
# The title which gets displayed in the main layout
inheritable_setting :site_title, ""
# Set the site title link href (defaults to AA dashboard)
inheritable_setting :site_title_link, ""
# Set the site title image displayed in the main layout (has precendence over :site_title)
inheritable_setting :site_title_image, ""
# The view factory to use to generate all the view classes. Take
# a look at ActiveAdmin::ViewFactory
inheritable_setting :view_factory, ActiveAdmin::ViewFactory.new
# The method to call in controllers to get the current user
inheritable_setting :current_user_method, false
# The method to call in the controllers to ensure that there
# is a currently authenticated admin user
inheritable_setting :authentication_method, false
# The path to log user's out with. If set to a symbol, we assume
# that it's a method to call which returns the path
inheritable_setting :logout_link_path, :destroy_admin_user_session_path
# The method to use when generating the link for user logout
inheritable_setting :logout_link_method, :get
# Whether the batch actions are enabled or not
inheritable_setting :batch_actions, false
# Whether filters are enabled
inheritable_setting :filters, true
# The namespace root.
inheritable_setting :root_to, 'dashboard#index'
# Default CSV separator
inheritable_setting :csv_column_separator, ','
# Default CSV options
inheritable_setting :csv_options, {}
# Active Admin makes educated guesses when displaying objects, this is
# the list of methods it tries calling in order
setting :display_name_methods, [ :display_name,
:full_name,
:name,
:username,
:login,
:title,
:email,
:to_s ]
# == Deprecated Settings
# @deprecated The default sort order for index pages
deprecated_setting :default_sort_order, 'id_desc'
# DEPRECATED: This option is deprecated and will be removed. Use
# the #allow_comments_in option instead
attr_accessor :admin_notes
include AssetRegistration
# Event that gets triggered on load of Active Admin
BeforeLoadEvent = 'active_admin.application.before_load'.freeze
AfterLoadEvent = 'active_admin.application.after_load'.freeze
def setup!
register_default_assets
end
def prepare!
remove_active_admin_load_paths_from_rails_autoload_and_eager_load
attach_reloader
generate_stylesheets
end
# Registers a brand new configuration for the given resource.
def register(resource, options = {}, &block)
namespace_name = extract_namespace_name(options)
namespace = find_or_create_namespace(namespace_name)
namespace.register(resource, options, &block)
end
# Creates a namespace for the given name
#
# Yields the namespace if a block is given
#
# @returns [Namespace] the new or existing namespace
def find_or_create_namespace(name)
name ||= :root
if namespaces[name]
namespace = namespaces[name]
else
namespace = Namespace.new(self, name)
namespaces[name] = namespace
ActiveAdmin::Event.dispatch ActiveAdmin::Namespace::RegisterEvent, namespace
end
yield(namespace) if block_given?
namespace
end
alias_method :namespace, :find_or_create_namespace
# Register a page
#
# @param name [String] The page name
# @options [Hash] Accepts option :namespace.
# @&block The registration block.
#
def register_page(name, options = {}, &block)
namespace_name = extract_namespace_name(options)
namespace = find_or_create_namespace(namespace_name)
namespace.register_page(name, options, &block)
end
# Stores if everything has been loaded or we need to reload
@@loaded = false
# Returns true if all the configuration files have been loaded.
def loaded?
@@loaded
end
# Removes all the controllers that were defined by registering
# resources for administration.
#
# We remove them, then load them on each request in development
# to allow for changes without having to restart the server.
def unload!
namespaces.values.each{|namespace| namespace.unload! }
@@loaded = false
end
# Loads all of the ruby files that are within the load path of
# ActiveAdmin.load_paths. This should load all of the administration
# UIs so that they are available for the router to proceed.
#
# The files are only loaded if we haven't already loaded all the files
# and they aren't marked for re-loading. To mark the files for re-loading
# you must first call ActiveAdmin.unload!
def load!
# No work to do if we've already loaded
return false if loaded?
ActiveAdmin::Event.dispatch BeforeLoadEvent, self
# Load files
files_in_load_path.each{|file| load file }
# If no configurations, let's make sure you can still login
load_default_namespace if namespaces.values.empty?
# Dispatch an ActiveAdmin::Application::LoadEvent with the Application
ActiveAdmin::Event.dispatch AfterLoadEvent, self
@@loaded = true
end
# Returns ALL the files to load from all the load paths
def files_in_load_path
load_paths.flatten.compact.uniq.collect{|path| Dir["#{path}/**/*.rb"] }.flatten
end
def router
@router ||= Router.new(self)
end
def routes(rails_router)
# Ensure that all the configurations (which define the routes)
# are all loaded
load!
router.apply(rails_router)
end
def load_default_namespace
find_or_create_namespace(default_namespace)
end
#
# Add before, around and after filters to each registered resource and pages.
#
# eg:
#
# ActiveAdmin.before_filter :authenticate_admin!
#
def before_filter(*args, &block)
BaseController.before_filter(*args, &block)
end
def skip_before_filter(*args, &block)
BaseController.skip_before_filter(*args, &block)
end
def after_filter(*args, &block)
BaseController.after_filter(*args, &block)
end
def around_filter(*args, &block)
BaseController.around_filter(*args, &block)
end
# Helper method to add a dashboard section
def dashboard_section(name, options = {}, &block)
ActiveAdmin::Dashboards.add_section(name, options, &block)
end
private
def register_default_assets
register_stylesheet 'active_admin.css', :media => 'screen'
register_stylesheet 'active_admin/print.css', :media => 'print'
unless ActiveAdmin.use_asset_pipeline?
register_javascript 'jquery.min.js'
register_javascript 'jquery-ui.min.js'
register_javascript 'jquery_ujs.js'
end
register_javascript 'active_admin.js'
end
def extract_namespace_name(options)
options.has_key?(:namespace) ? options[:namespace] : default_namespace
end
# Since we're dealing with all our own file loading, we need
# to remove our paths from the ActiveSupport autoload paths.
# If not, file naming becomes very important and can cause clashes.
def remove_active_admin_load_paths_from_rails_autoload_and_eager_load
ActiveSupport::Dependencies.autoload_paths.reject!{|path| load_paths.include?(path) }
# Don't eagerload our configs, we'll deal with them ourselves
Rails.application.config.eager_load_paths = Rails.application.config.eager_load_paths.reject do |path|
load_paths.include?(path)
end
end
def attach_reloader
ActiveAdmin::Reloader.build(Rails.application, self, Rails.version).attach!
end
def generate_stylesheets
# Create our own asset pipeline in Rails 3.0
if ActiveAdmin.use_asset_pipeline?
# Add our mixins to the load path for SASS
::Sass::Engine::DEFAULT_OPTIONS[:load_paths] << File.expand_path("../../../app/assets/stylesheets", __FILE__)
else
require 'active_admin/sass/css_loader'
::Sass::Plugin.add_template_location(File.expand_path("../../../app/assets/stylesheets", __FILE__))
::Sass::Plugin.add_template_location(File.expand_path("../sass", __FILE__))
end
end
end
end