activesupport-decorators 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +42 -1
- data/lib/active_support_decorators/active_support_decorators.rb +22 -14
- data/lib/active_support_decorators/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fb383abd0973a741056c00121c60e933b56807f
|
4
|
+
data.tar.gz: 906175dbe61580b85a60e64a7f5ebd502e638a59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57d948191c62ccadd8d08e0bdd569e8426d2eacbf161bd0b8abb27136a39498c930e58bb39044a062dcfd676e786a32fece3e5f4742aa687cfe4afeb9a29760d
|
7
|
+
data.tar.gz: 02ebb658116b83151734bbdbdaa2a87afddb539fbd5220ed08871e6225393a1edfe3a7e31b0d62b26054aad95c4ea1ed66c2cbd07e204839c7496e9343f0ba37
|
data/README.md
CHANGED
@@ -1,4 +1,45 @@
|
|
1
1
|
ActiveSupportDecorators
|
2
2
|
=======================
|
3
3
|
|
4
|
-
|
4
|
+
The decorator pattern is particularly useful when extending constants in rails engines or vica versa. To implement
|
5
|
+
the decorator pattern, you need to load the decorator after the original file has been loaded. When you reference a
|
6
|
+
class in a Rails application, ActiveSupport will only load the first file it finds that matches the class name. This
|
7
|
+
means that you will need to manually load the additional (decorator) file. Usually you don't want to want to introduce
|
8
|
+
hard dependencies such as require statements. You also don't want to preload a bunch of classes in a Rails initializer.
|
9
|
+
This gem allows you to specify load dependencies without loading any of them when the application starts up.
|
10
|
+
|
11
|
+
Example
|
12
|
+
=======
|
13
|
+
|
14
|
+
Lets say your main rails application defines a model called Pet (in app/models/pet.rb):
|
15
|
+
|
16
|
+
```Ruby
|
17
|
+
class Pet < ActiveRecord::Base
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
Your rails engine adds the concept of pet owners to the application. You can extend the Pet model in the engine with
|
22
|
+
the following model decorator (in my_engine/app/models/pet.rb).
|
23
|
+
|
24
|
+
```Ruby
|
25
|
+
class Pet
|
26
|
+
belongs_to :owner
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
You can tell ActiveSupportDecorators to load any matching file in my_engine/app/models when a file is loaded from
|
31
|
+
app/models. A convenient place to do this is in a Rails initializer in the engine:
|
32
|
+
|
33
|
+
```Ruby
|
34
|
+
module MyEngine
|
35
|
+
module Rails
|
36
|
+
class Engine < ::Rails::Engine
|
37
|
+
initializer :append_auto_decorators do |app|
|
38
|
+
ActiveSupportDecorators.add_dependency("#{app.root}/app/models", "#{config.root}/app/models")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Note that you could specify the path as "/app" to decorate controllers, models, helpers, etc.
|
@@ -1,30 +1,38 @@
|
|
1
1
|
module ActiveSupportDecorators
|
2
|
-
def self.
|
3
|
-
@
|
2
|
+
def self.dependencies
|
3
|
+
@dependencies ||= {}
|
4
4
|
end
|
5
5
|
|
6
|
-
def self.
|
7
|
-
|
6
|
+
def self.add_dependency(path, decorator_path)
|
7
|
+
if dependencies.include?(path)
|
8
|
+
dependencies[path] << decorator_path
|
9
|
+
else
|
10
|
+
dependencies[path] = [decorator_path]
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
|
-
def self.
|
11
|
-
@
|
14
|
+
def self.debug
|
15
|
+
@debug ||= false
|
12
16
|
end
|
13
17
|
|
14
|
-
def self.
|
15
|
-
@
|
18
|
+
def self.debug=(debugging_enabled)
|
19
|
+
@debug = debugging_enabled
|
16
20
|
end
|
17
21
|
|
18
22
|
def self.load_path_order(file_name)
|
19
23
|
file_name_order = [file_name]
|
20
24
|
|
21
|
-
|
22
|
-
|
25
|
+
dependencies.each do |path, decorator_paths|
|
26
|
+
if file_name.starts_with?(path)
|
27
|
+
relative_name = file_name.gsub(path, '')
|
28
|
+
|
29
|
+
decorator_paths.each do |decorator_path|
|
30
|
+
decorator_file = "#{decorator_path}#{relative_name}"
|
23
31
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
if File.file?(decorator_file) || File.file?(decorator_file + '.rb')
|
33
|
+
Rails.logger.debug "ActiveSupportDecorators: Loading '#{decorator_file}' after '#{file_name}'." if debug
|
34
|
+
file_name_order << decorator_file
|
35
|
+
end
|
28
36
|
end
|
29
37
|
end
|
30
38
|
end
|