Module: Rails::Decorators::Decorator
- Defined in:
- lib/rails/decorators/decorator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #before_decorate(&block) ⇒ Object
- #class_methods(&class_methods_module_definition) ⇒ Object (also: #module_methods)
- #decorated(&block) ⇒ Object
- #decorates(klass) ⇒ Object
- #prepend_features(base) ⇒ Object
Class Method Details
.decorate(*targets, &module_definition) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rails/decorators/decorator.rb', line 14 def self.decorate(*targets, &module_definition) = targets. targets.each do |target| unless target.is_a?(Class) raise( InvalidDecorator, <<-eos.strip_heredoc Problem: You cannot decorate a Module Summary: Decoration only works with classes. Decorating modules requires managing load order, a problem that is very complicated and beyond the scope of this system. Resolution: Decorate multiple classes that include the module like so: `decorate Catalog::Product, Content::Page do` eos ) end if target.name.to_s.end_with?('Helper') raise( InvalidDecorator, <<-eos.strip_heredoc Problem: Rails::Decorators doesn't work with helpers. Summary: Rails does some magic with helpers which in certain cases causes decoration to not work. Resolution: Create a new helper and in a `to_prepare` block, use ActionPack's `helper` method to include the helper, e.g. `MyEngine::ApplicationController.helper(MyEngine::BlogsHelper)` eos ) end namespace = target.to_s.remove('::') decorator_name = "#{[:with].to_s.camelize}#{namespace}Decorator" if target.const_defined?(decorator_name) # We are probably reloading in Rails development env if this happens next if !Rails.application.config.cache_classes raise( InvalidDecorator, <<-eos.strip_heredoc Problem: #{decorator_name} is already defined in #{target.name}. Summary: When decorating a class, Rails::Decorators dynamically defines a module for prepending the decorations passed in the block. In this case, the name for the decoration module is already defined in the namespace, so decorating would redefine the constant. Resolution: Please specify a unique `with` option when decorating #{target.name}. eos ) end mod = Module.new do extend Rails::Decorators::Decorator module_eval(&module_definition) end target.const_set(decorator_name, mod) mod.decorates(target) end end |
.loader(roots) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/rails/decorators/decorator.rb', line 4 def self.loader(roots) Proc.new do roots.each do |root| decorators = Dir.glob("#{root}/app/**/*.#{Rails::Decorators.extension}") decorators.sort! decorators.each { |d| require_dependency(d) } end end end |
Instance Method Details
#before_decorate(&block) ⇒ Object
106 107 108 |
# File 'lib/rails/decorators/decorator.rb', line 106 def before_decorate(&block) instance_variable_set(:@_before_decorate_block, block) end |
#class_methods(&class_methods_module_definition) ⇒ Object Also known as: module_methods
114 115 116 117 |
# File 'lib/rails/decorators/decorator.rb', line 114 def class_methods(&class_methods_module_definition) mod = const_set(:ClassMethodsDecorator, Module.new) mod.module_eval(&class_methods_module_definition) end |
#decorated(&block) ⇒ Object
110 111 112 |
# File 'lib/rails/decorators/decorator.rb', line 110 def decorated(&block) instance_variable_set(:@_decorated_block, block) end |
#decorates(klass) ⇒ Object
120 121 122 |
# File 'lib/rails/decorators/decorator.rb', line 120 def decorates(klass) klass.send(:prepend, self) end |
#prepend_features(base) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rails/decorators/decorator.rb', line 88 def prepend_features(base) if instance_variable_defined?(:@_before_decorate_block) base.class_eval(&@_before_decorate_block) end super if const_defined?(:ClassMethodsDecorator) base .singleton_class .send(:prepend, const_get(:ClassMethodsDecorator)) end if instance_variable_defined?(:@_decorated_block) base.class_eval(&@_decorated_block) end end |