From a440c85ed6698ccafbc61e651ce15b1b4519cdde Mon Sep 17 00:00:00 2001 From: jacobherrington Date: Sun, 20 Jan 2019 08:47:05 -0600 Subject: [PATCH 1/3] Extend Decorator documentation This change adds an example of using ActiveRecord inside a model decorator. Largely adapted from a gist: https://gist.github.com/kushniryb/101c161eab8d2d2e2e35e4a17b781d1b --- .../developers/extensions/decorators.html.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/guides/source/developers/extensions/decorators.html.md b/guides/source/developers/extensions/decorators.html.md index 0429d1ddca6..edcfaedc822 100644 --- a/guides/source/developers/extensions/decorators.html.md +++ b/guides/source/developers/extensions/decorators.html.md @@ -32,6 +32,34 @@ total by $10 (or whatever your currency is). [monkey-patch]: https://en.wikipedia.org/wiki/Monkey_patch +## Using ActiveRecord methods in decorators + +In order to access ActiveRecord methods, you'll need to define a special +method. + +```ruby +module MyStore::ProductDecorator + + # This is the place to define custom associations, delegations, scopes and + # other ActiveRecord stuff + def self.prepended(base) + base.has_many :comments, dependent: :destroy + base.scope :sellable, -> { base.where(...).order(...) } + base.delegate :something, to: :something + + base.singleton_class.prepend ClassMethods + end + + ... + + Spree::Product.prepend self +end +``` + +In this example, a decorator has been used to extend the functionality of +`Spree::Product`. The decorator includes an ActiveRecord association, scope, +and delegation. + ## Decorators and Solidus upgrades Decorators can complicate your Solidus upgrades. If you depend on decorators, From bab9355a0c8d47aba3e70518e52f556692fb3808 Mon Sep 17 00:00:00 2001 From: Jacob Herrington Date: Tue, 22 Jan 2019 10:29:15 -0600 Subject: [PATCH 2/3] Rephrase and remove unnecessary code from example --- guides/source/developers/extensions/decorators.html.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/guides/source/developers/extensions/decorators.html.md b/guides/source/developers/extensions/decorators.html.md index edcfaedc822..1f7cdd28329 100644 --- a/guides/source/developers/extensions/decorators.html.md +++ b/guides/source/developers/extensions/decorators.html.md @@ -34,7 +34,7 @@ total by $10 (or whatever your currency is). ## Using ActiveRecord methods in decorators -In order to access ActiveRecord methods, you'll need to define a special +In order to access some class-level methods, you'll need to define a special method. ```ruby @@ -46,8 +46,6 @@ module MyStore::ProductDecorator base.has_many :comments, dependent: :destroy base.scope :sellable, -> { base.where(...).order(...) } base.delegate :something, to: :something - - base.singleton_class.prepend ClassMethods end ... From 7222dd2229f66e4c20c56653070c351f15511335 Mon Sep 17 00:00:00 2001 From: Jacob Herrington Date: Tue, 22 Jan 2019 10:32:03 -0600 Subject: [PATCH 3/3] Change header from ActiveRecord to class-level --- guides/source/developers/extensions/decorators.html.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/developers/extensions/decorators.html.md b/guides/source/developers/extensions/decorators.html.md index 1f7cdd28329..677ac6120ea 100644 --- a/guides/source/developers/extensions/decorators.html.md +++ b/guides/source/developers/extensions/decorators.html.md @@ -32,7 +32,7 @@ total by $10 (or whatever your currency is). [monkey-patch]: https://en.wikipedia.org/wiki/Monkey_patch -## Using ActiveRecord methods in decorators +## Using class-level methods in decorators In order to access some class-level methods, you'll need to define a special method.