From 81147fda5e933a82fe094635cc88002f72509fbd Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Mon, 6 Nov 2023 14:28:11 +0100 Subject: [PATCH] refactor: rename generated method by .decorates_association (#48) --- CHANGELOG.md | 3 ++- README.md | 6 +++--- lib/dekorator.rb | 2 +- spec/lib/dekorator_base_spec.rb | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb100a6..6112f74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -Nothing yet +### Changed +- **Breaking** `.decorates_association` now generate `#decorated_[ASSOCIATION_NAME]` instead `#[ASSOCIATION_NAME]` ([#48](https://github.com/komposable/dekorator/pull/48)) ## [1.3.0] - 2023-11-06 ### Added diff --git a/README.md b/README.md index 0d7b70b..ee71668 100644 --- a/README.md +++ b/README.md @@ -108,12 +108,12 @@ class PostDecorator < ApplicationDecorator end ``` -In this example, `UserDecorator#posts` will be decorated. +In this example, `UserDecorator#posts` will be decorated as `#decorated_posts`. ```ruby decorated_user = decorate(User.first) decorated_user # => UserDecorator -decorated_user.posts.first # => PostDecorator +decorated_user.decorated_posts.first # => PostDecorator ``` ### Custom decorator @@ -148,7 +148,7 @@ end decorated_user = decorate(User.first) decorated_user # => UserDecorator -decorated_user.posts.first # => ArticleDecorator +decorated_user.decorated_posts.first # => ArticleDecorator ``` ## Compatibility diff --git a/lib/dekorator.rb b/lib/dekorator.rb index caf24a3..53d898a 100644 --- a/lib/dekorator.rb +++ b/lib/dekorator.rb @@ -54,7 +54,7 @@ def decorate(object_or_enumerable, with: nil) def decorates_association(relation_name, with: :__guess__) relation_name = relation_name.to_sym - define_method(relation_name) do + define_method(:"decorated_#{relation_name}") do @_decorated_associations[relation_name] ||= decorate(__getobj__.public_send(relation_name), with: with) end end diff --git a/spec/lib/dekorator_base_spec.rb b/spec/lib/dekorator_base_spec.rb index 5060758..19f44ff 100644 --- a/spec/lib/dekorator_base_spec.rb +++ b/spec/lib/dekorator_base_spec.rb @@ -49,7 +49,7 @@ end describe "self#decorates_association" do - it { expect(decorated_post_with_comments.comments).to all(be_a(CommentDecorator)) } + it { expect(decorated_post_with_comments.decorated_comments).to all(be_a(CommentDecorator)) } end describe "self#base_class" do