activesupport-decorators 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a22f99e79b704ddf3d114c4e995ec75790a96a79
4
- data.tar.gz: da8b53bd8f8ed6ce86d96e71cfb0ea06fcd81e36
3
+ metadata.gz: 3fb383abd0973a741056c00121c60e933b56807f
4
+ data.tar.gz: 906175dbe61580b85a60e64a7f5ebd502e638a59
5
5
  SHA512:
6
- metadata.gz: ef3fbcc96854c5a9505edd6bcd3636765693450193d2a993fedfc9ac89f438b2f6d1d775d51fdcf1dbbdd3eb3f37ed37bbe68ac6df29cc6e41a9c72d8a574808
7
- data.tar.gz: 1924715f6dd6bdfb13a69b146b38095fb62fef5f022c8e421b8005555414a110a1352dba79660c519698d87b634d1e365e2f75134b16c29c9b805627557339f0
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
- Adds the decorator pattern to activesupport class loading.
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.auto_decorate_paths=(path_array)
3
- @auto_decorate_paths = path_array
2
+ def self.dependencies
3
+ @dependencies ||= {}
4
4
  end
5
5
 
6
- def self.auto_decorate_paths
7
- @auto_decorate_paths ||= []
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.auto_decorate_provider_paths=(path_array)
11
- @auto_decorate_provider_paths = path_array
14
+ def self.debug
15
+ @debug ||= false
12
16
  end
13
17
 
14
- def self.auto_decorate_provider_paths
15
- @auto_decorate_provider_paths ||= []
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
- if auto_decorate_paths.any? { |path| file_name.starts_with?(path) }
22
- relative_name = file_name.gsub(Rails.root.to_s, '')
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
- auto_decorate_provider_paths.each do |path|
25
- decorator_file = "#{path}#{relative_name}"
26
- if File.file?(decorator_file) || File.file?(decorator_file + '.rb')
27
- file_name_order << decorator_file
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
@@ -1,3 +1,3 @@
1
1
  module ActiveSupportDecorators
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesupport-decorators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Pretorius