activesupport-decorators 2.0.1 → 2.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 +7 -7
- data/lib/active_support_decorators/active_support_decorators.rb +31 -17
- data/lib/active_support_decorators/dependencies_patch.rb +25 -7
- data/lib/active_support_decorators/version.rb +1 -1
- data/spec/lib/active_support_decorators/active_support_decorator_spec.rb +9 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1eb929b86d8d93c87ea6b1b82fc674d71085d530
|
4
|
+
data.tar.gz: 06bdf75917b92a6a5f14932d6cff4085b57dc92a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29989390b460c1be447c186f801c58a15258af962f950fe0eb02a20f0536d0b6f38f78136b4597f78b58d68b505a0dde925a3a3a0171fa0be6be055d36467a52
|
7
|
+
data.tar.gz: ea865922feebb36e0c4c1dc5729382bcbe371e307fea2cbc9873d6c4e94ad3c187c77d83e72ca33ad65f60fcc16a5445438585e40a2814c97dcc5dfca7b08ae1
|
data/README.md
CHANGED
@@ -22,16 +22,16 @@ gem 'activesupport-decorators', '~> 2.0'
|
|
22
22
|
|
23
23
|
#### Example 1 - Application extends engine (or any other) class.
|
24
24
|
|
25
|
-
Your
|
25
|
+
Your engine defines a model called Pet (in my_engine/app/models/pet.rb):
|
26
26
|
|
27
27
|
```Ruby
|
28
28
|
class Pet < ActiveRecord::Base
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
-
Your
|
33
|
-
|
34
|
-
|
32
|
+
Your application now wants to adds the concept of pet owners. You extend the Pet model in the application with the
|
33
|
+
following model decorator (in app/models/pet_decorator.rb). Note that you could use 'Pet.class_eval do' instead
|
34
|
+
of 'class Pet' if you want.
|
35
35
|
|
36
36
|
```Ruby
|
37
37
|
class Pet
|
@@ -39,9 +39,9 @@ class Pet
|
|
39
39
|
end
|
40
40
|
```
|
41
41
|
|
42
|
-
|
43
|
-
matches the original file's name/path and ends with '_decorator.rb'. In other words when the
|
44
|
-
loaded, it will load the main applications app/pet_decorator.rb.
|
42
|
+
Now tell ActiveSupportDecorators where to look for decorators, similar to setting Rails autoload paths. This will load
|
43
|
+
a decorator file if it matches the original file's name/path and ends with '_decorator.rb'. In other words when the
|
44
|
+
engine's app/pet.rb is loaded, it will load the main applications app/pet_decorator.rb.
|
45
45
|
|
46
46
|
```Ruby
|
47
47
|
ActiveSupportDecorators.paths << File.join(Rails.application.root, 'app/**')
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module ActiveSupportDecorators
|
2
|
+
|
2
3
|
def self.paths
|
3
4
|
@paths ||= []
|
4
5
|
end
|
@@ -23,34 +24,47 @@ module ActiveSupportDecorators
|
|
23
24
|
@debug = debugging_enabled
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
def self.log(message)
|
28
|
+
puts message if debug
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.is_decorator?(file_name)
|
32
|
+
sanitize(file_name).ends_with?(pattern)
|
31
33
|
end
|
32
34
|
|
33
|
-
def self.
|
34
|
-
file = file_name
|
35
|
+
def self.all(file_name, const_path = nil)
|
36
|
+
file = sanitize(file_name)
|
35
37
|
|
36
38
|
if const_path
|
37
39
|
file = const_path.underscore
|
38
40
|
else
|
39
|
-
|
40
|
-
|
41
|
-
file = sanitized_file_name.sub(first_load_path_match, '') if first_load_path_match
|
41
|
+
first_autoload_match = all_autoload_paths.find { |p| file.include?(p) }
|
42
|
+
file.sub!(first_autoload_match, '') if first_autoload_match
|
42
43
|
end
|
43
|
-
|
44
|
+
|
45
|
+
relative_target = "#{file}#{pattern}.rb"
|
46
|
+
|
47
|
+
expanded_paths.map { |path| File.join(path, relative_target) }.select { |candidate| File.file?(candidate) }
|
44
48
|
end
|
45
49
|
|
46
|
-
def self.
|
47
|
-
|
50
|
+
def self.original_const_name(file_name)
|
51
|
+
first_match = expanded_paths.find { |path| file_name.include?(path) }
|
48
52
|
|
49
|
-
|
50
|
-
|
51
|
-
|
53
|
+
if first_match
|
54
|
+
sanitize(file_name).sub("#{first_match}/", '').sub(pattern, '').camelize
|
55
|
+
else
|
56
|
+
nil
|
52
57
|
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def self.all_autoload_paths
|
62
|
+
return [] unless defined?(Rails)
|
63
|
+
all_modules = [::Rails.application] + ::Rails::Engine.subclasses.map(&:instance)
|
64
|
+
all_modules.map { |mod| mod.send(:_all_autoload_paths) }.flatten
|
65
|
+
end
|
53
66
|
|
54
|
-
|
67
|
+
def self.sanitize(file_name)
|
68
|
+
file_name.sub(/\.rb$/, '')
|
55
69
|
end
|
56
70
|
end
|
@@ -2,17 +2,35 @@ require 'active_support/dependencies'
|
|
2
2
|
|
3
3
|
module ActiveSupport
|
4
4
|
module Dependencies
|
5
|
-
alias_method :
|
5
|
+
alias_method :require_or_load_single, :require_or_load
|
6
6
|
|
7
7
|
def require_or_load(file_name, const_path = nil)
|
8
|
-
|
8
|
+
if ActiveSupportDecorators.is_decorator?(file_name)
|
9
|
+
# If an attempt is made to load the decorator file (such as eager loading), we
|
10
|
+
# need to load the original file and then the decorators.
|
11
|
+
original_const_name = ActiveSupportDecorators.original_const_name(file_name)
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
if original_const_name
|
14
|
+
ActiveSupportDecorators.log "Decorators: Expecting #{file_name} to decorate #{original_const_name}."
|
15
|
+
original_const_name.constantize
|
16
|
+
|
17
|
+
ActiveSupportDecorators.all(file_name, const_path).each do |d|
|
18
|
+
ActiveSupportDecorators.log "Decorators: Loading #{d} for #{file_name}."
|
19
|
+
require_or_load_single(d)
|
20
|
+
end
|
21
|
+
|
22
|
+
else
|
23
|
+
ActiveSupportDecorators.log "Decorators: Nothing found to load before: #{file_name}."
|
24
|
+
require_or_load_single(file_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
else
|
28
|
+
require_or_load_single(file_name, const_path)
|
13
29
|
|
14
|
-
|
15
|
-
|
30
|
+
ActiveSupportDecorators.all(file_name, const_path).each do |d|
|
31
|
+
ActiveSupportDecorators.log "Decorators: Loading #{d} for #{file_name}."
|
32
|
+
require_or_load_single(d)
|
33
|
+
end
|
16
34
|
end
|
17
35
|
end
|
18
36
|
end
|
@@ -19,19 +19,26 @@ describe ActiveSupportDecorators do
|
|
19
19
|
ActiveSupportDecorators.paths << File.join(File.dirname(__FILE__), 'support', 'decorators')
|
20
20
|
end
|
21
21
|
|
22
|
-
it 'it loads the decorator file
|
22
|
+
it 'it loads the decorator file when you load the original without const_path' do
|
23
23
|
path = File.join(File.dirname(__FILE__), 'support', 'originals', 'pet')
|
24
24
|
ActiveSupport::Dependencies.require_or_load(path)
|
25
25
|
|
26
26
|
Pet.new.owner.should eq('Mr. Robinson')
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'it loads the decorator file
|
29
|
+
it 'it loads the decorator file when you load the original with const_path' do
|
30
30
|
path = File.join(File.dirname(__FILE__), 'support', 'originals', 'pet')
|
31
31
|
ActiveSupport::Dependencies.require_or_load(path, 'Pet')
|
32
32
|
|
33
33
|
Pet.new.owner.should eq('Mr. Robinson')
|
34
34
|
end
|
35
|
+
|
36
|
+
#it 'it loads the original file when you load the decorator file' do
|
37
|
+
# path = File.join(File.dirname(__FILE__), 'support', 'decorators', 'pet_decorator')
|
38
|
+
# ActiveSupport::Dependencies.require_or_load(path)
|
39
|
+
|
40
|
+
# Pet.new.owner.should eq('Mr. Robinson')
|
41
|
+
#end
|
35
42
|
end
|
36
43
|
|
37
44
|
describe 'when not using activesupport-decorators' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activesupport-decorators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Pretorius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|