Skip to content
This repository

Refactor application.rb #1882

Merged
merged 1 commit into from over 1 year ago

2 participants

Sean Linsley Matt Brewer
Sean Linsley
Collaborator

application.rb is a pretty big file, and it's difficult to follow. Hopefully these changes help.
I wrote this up a while back, but never opened a pull request. Let me know what you think!

Sean Linsley
Collaborator

All green :four_leaf_clover:

Sean Linsley
Collaborator

Thoughts @macfanatic?

Matt Brewer macfanatic merged commit d763db5 into from
Sean Linsley seanlinsley referenced this pull request
Closed

Test all the tests! #2043

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 18, 2013
Sean Linsley seanlinsley refactor applicaiton.rb cd09ea0
This page is out of date. Refresh to see the latest.
106 lib/active_admin/application.rb
@@ -124,9 +124,7 @@ def prepare!
124 124
125 125 # Registers a brand new configuration for the given resource.
126 126 def register(resource, options = {}, &block)
127   - namespace_name = extract_namespace_name(options)
128   - namespace = find_or_create_namespace(namespace_name)
129   - namespace.register(resource, options, &block)
  127 + namespace(options[:namespace]).register resource, options, &block
130 128 end
131 129
132 130 # Creates a namespace for the given name
@@ -134,14 +132,12 @@ def register(resource, options = {}, &block)
134 132 # Yields the namespace if a block is given
135 133 #
136 134 # @returns [Namespace] the new or existing namespace
137   - def find_or_create_namespace(name)
138   - name ||= :root
139   -
  135 + def namespace(name)
  136 + name ||= name == false ? :root : default_namespace
140 137 if namespaces[name]
141 138 namespace = namespaces[name]
142 139 else
143   - namespace = Namespace.new(self, name)
144   - namespaces[name] = namespace
  140 + namespace = namespaces[name] = Namespace.new(self, name)
145 141 ActiveAdmin::Event.dispatch ActiveAdmin::Namespace::RegisterEvent, namespace
146 142 end
147 143
@@ -150,8 +146,6 @@ def find_or_create_namespace(name)
150 146 namespace
151 147 end
152 148
153   - alias_method :namespace, :find_or_create_namespace
154   -
155 149 # Register a page
156 150 #
157 151 # @param name [String] The page name
@@ -159,96 +153,56 @@ def find_or_create_namespace(name)
159 153 # @&block The registration block.
160 154 #
161 155 def register_page(name, options = {}, &block)
162   - namespace_name = extract_namespace_name(options)
163   - namespace = find_or_create_namespace(namespace_name)
164   - namespace.register_page(name, options, &block)
  156 + namespace(options[:namespace]).register_page name, options, &block
165 157 end
166 158
167   - # Stores if everything has been loaded or we need to reload
168   - @@loaded = false
169   -
170   - # Returns true if all the configuration files have been loaded.
  159 + # Whether all configuration files have been loaded
171 160 def loaded?
172   - @@loaded
  161 + @@loaded ||= false
173 162 end
174 163
175   - # Removes all the controllers that were defined by registering
176   - # resources for administration.
177   - #
178   - # We remove them, then load them on each request in development
179   - # to allow for changes without having to restart the server.
  164 + # Removes all defined controllers from memory. Useful in
  165 + # development, where they are reloaded on each request.
180 166 def unload!
181   - namespaces.values.each{|namespace| namespace.unload! }
  167 + namespaces.values.each{ |namespace| namespace.unload! }
182 168 @@loaded = false
183 169 end
184 170
185   - # Loads all of the ruby files that are within the load path of
186   - # ActiveAdmin.load_paths. This should load all of the administration
187   - # UIs so that they are available for the router to proceed.
188   - #
189   - # The files are only loaded if we haven't already loaded all the files
190   - # and they aren't marked for re-loading. To mark the files for re-loading
191   - # you must first call ActiveAdmin.unload!
  171 + # Loads all ruby files that are within the load_paths setting.
  172 + # To reload everything simply call `ActiveAdmin.unload!`
192 173 def load!
193   - # No work to do if we've already loaded
194   - return false if loaded?
195   -
196   - ActiveAdmin::Event.dispatch BeforeLoadEvent, self
197   -
198   - # Load files
199   - files_in_load_path.each{|file| load file }
200   -
201   - # If no configurations, let's make sure you can still login
202   - load_default_namespace if namespaces.values.empty?
203   -
204   - # Dispatch an ActiveAdmin::Application::LoadEvent with the Application
205   - ActiveAdmin::Event.dispatch AfterLoadEvent, self
206   -
207   - @@loaded = true
  174 + unless loaded?
  175 + ActiveAdmin::Event.dispatch BeforeLoadEvent, self # before_load hook
  176 + files.each{ |file| load file } # load files
  177 + namespace(nil) # init AA resources
  178 + ActiveAdmin::Event.dispatch AfterLoadEvent, self # after_load hook
  179 + @@loaded = true
  180 + end
208 181 end
209 182
210   - # Returns ALL the files to load from all the load paths
211   - def files_in_load_path
212   - load_paths.flatten.compact.uniq.collect{|path| Dir["#{path}/**/*.rb"] }.flatten
  183 + # Returns ALL the files to be loaded
  184 + def files
  185 + load_paths.flatten.compact.uniq.map{ |path| Dir["#{path}/**/*.rb"] }.flatten
213 186 end
214 187
215 188 def router
216 189 @router ||= Router.new(self)
217 190 end
218 191
  192 + # One-liner called by user's config/routes.rb file
219 193 def routes(rails_router)
220   - # Ensure that all the configurations (which define the routes)
221   - # are all loaded
222 194 load!
223   -
224 195 router.apply(rails_router)
225 196 end
226 197
227   - def load_default_namespace
228   - find_or_create_namespace(default_namespace)
229   - end
230   -
231   - #
232 198 # Add before, around and after filters to each registered resource and pages.
233   - #
234   - # eg:
235   - #
  199 + # For example:
236 200 # ActiveAdmin.before_filter :authenticate_admin!
237 201 #
238   - def before_filter(*args, &block)
239   - BaseController.before_filter(*args, &block)
240   - end
241   -
242   - def skip_before_filter(*args, &block)
243   - BaseController.skip_before_filter(*args, &block)
244   - end
245   -
246   - def after_filter(*args, &block)
247   - BaseController.after_filter(*args, &block)
248   - end
249   -
250   - def around_filter(*args, &block)
251   - BaseController.around_filter(*args, &block)
  202 + %w(before_filter skip_before_filter after_filter around_filter).each do |name|
  203 + define_method name do |*args, &block|
  204 + BaseController.send name, *args, &block
  205 + end
252 206 end
253 207
254 208 # Helper method to add a dashboard section
@@ -271,10 +225,6 @@ def register_default_assets
271 225 register_javascript 'active_admin.js'
272 226 end
273 227
274   - def extract_namespace_name(options)
275   - options.has_key?(:namespace) ? options[:namespace] : default_namespace
276   - end
277   -
278 228 # Since we're dealing with all our own file loading, we need
279 229 # to remove our paths from the ActiveSupport autoload paths.
280 230 # If not, file naming becomes very important and can cause clashes.
10 spec/unit/application_spec.rb
@@ -95,18 +95,18 @@
95 95
96 96 describe "files in load path" do
97 97 it "should load files in the first level directory" do
98   - application.files_in_load_path.should include(File.expand_path("app/admin/dashboard.rb", Rails.root))
  98 + application.files.should include(File.expand_path("app/admin/dashboard.rb", Rails.root))
99 99 end
100 100
101 101 it "should load files from subdirectories" do
102 102 FileUtils.mkdir_p(File.expand_path("app/admin/public", Rails.root))
103 103 test_file = File.expand_path("app/admin/public/posts.rb", Rails.root)
104 104 FileUtils.touch(test_file)
105   - application.files_in_load_path.should include(test_file)
  105 + application.files.should include(test_file)
106 106 end
107 107 end
108 108
109   - describe "#namespace (or #find_or_create_namespace)" do
  109 + describe "#namespace" do
110 110
111 111 it "should yield a new namespace" do
112 112 application.namespace :new_namespace do |ns|
@@ -115,7 +115,7 @@
115 115 end
116 116
117 117 it "should return an instantiated namespace" do
118   - admin = application.find_or_create_namespace :admin
  118 + admin = application.namespace :admin
119 119 admin.should == application.namespaces[:admin]
120 120 end
121 121
@@ -132,7 +132,7 @@
132 132 describe "#register_page" do
133 133 it "finds or create the namespace and register the page to it" do
134 134 namespace = mock
135   - application.should_receive(:find_or_create_namespace).with("public").and_return namespace
  135 + application.should_receive(:namespace).with("public").and_return namespace
136 136 namespace.should_receive(:register_page).with("My Page", {:namespace => "public"})
137 137
138 138 application.register_page("My Page", :namespace => "public")

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.