activesupport-decorators 2.0.1 → 2.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: ab36f5e952455f62d9a6ba3297a3b9ccf0a4a882
4
- data.tar.gz: ec8df96f59cc48cefd330897491137a4b50fbbce
3
+ metadata.gz: 1eb929b86d8d93c87ea6b1b82fc674d71085d530
4
+ data.tar.gz: 06bdf75917b92a6a5f14932d6cff4085b57dc92a
5
5
  SHA512:
6
- metadata.gz: ea6ccf8428d1eceb951baf6df41f69b9de6ae94410b108d5e475ef0b5a69b2d4be7e2edbb51a678c00f7c9cac5f08b41861a14e43239a03233396972e590b223
7
- data.tar.gz: 64f9957ace96a5ac3186c06e66319d90b0e9190d564cc794c1279f38bb71c7bd72be0f45822643a8ac226f02ba1608a3acd886571c551f917c0e7abba23bd9cf
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 Rails engine defines a model called Pet (in my_engine/app/models/pet.rb):
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 Rails application now wants to adds the concept of pet owners. You extend the Pet model in the main application
33
- with the following model decorator (in app/models/pet_decorator.rb). Note that you could use 'Pet.class_eval do'
34
- instead of 'class Pet' if you want.
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
- Set your ActiveSupportDecorators paths similar to setting Rails autoload paths. This will load a decorator file if it
43
- matches the original file's name/path and ends with '_decorator.rb'. In other words when the engine's app/pet.rb is
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
- private
27
- def self.all_autoload_paths
28
- return [] unless defined?(Rails)
29
- all_modules = [::Rails.application] + ::Rails::Engine.subclasses.map(&:instance)
30
- all_modules.map { |mod| mod.send(:_all_autoload_paths) }.flatten
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.relative_search_path(file_name, const_path = nil)
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
- sanitized_file_name = file_name.sub(/\.rb$/, '')
40
- first_load_path_match = all_autoload_paths.find { |p| file_name.include?(p) }
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
- "#{file}#{pattern}.rb"
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.load_path_order(file_name, const_path = nil)
47
- order = [file_name]
50
+ def self.original_const_name(file_name)
51
+ first_match = expanded_paths.find { |path| file_name.include?(path) }
48
52
 
49
- expanded_paths.each do |path|
50
- candidate_file = File.join(path, relative_search_path(file_name, const_path))
51
- order << candidate_file if File.file?(candidate_file)
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
- order
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 :require_or_load_without_multiple, :require_or_load
5
+ alias_method :require_or_load_single, :require_or_load
6
6
 
7
7
  def require_or_load(file_name, const_path = nil)
8
- order = ActiveSupportDecorators.load_path_order(file_name, const_path)
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
- if ActiveSupportDecorators.debug && order.size > 1
11
- Rails.try(:logger).try(:debug, "ActiveSupportDecorators: Loading files in order #{order.join(', ')}.")
12
- end
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
- order.each do |path|
15
- require_or_load_without_multiple(path, const_path)
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
@@ -1,3 +1,3 @@
1
1
  module ActiveSupportDecorators
2
- VERSION = '2.0.1'
2
+ VERSION = '2.0.2'
3
3
  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 after loading the original without const_path' do
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 after loading the original with const_path' do
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.1
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-29 00:00:00.000000000 Z
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties