tramway 0.4.5.1 → 0.4.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef5809ca8f857fbc319595ce2ada4409120a4a89dc657bf2f7625e9b329d7d47
4
- data.tar.gz: da8094f83cc2f21d4470afef2311c790a079f9597aee99ebb448ea98cb83df61
3
+ metadata.gz: 57af927fcbb72fd669c6c8b7f480c37b3a83abe44cd84cf9fbabcb2cfa842786
4
+ data.tar.gz: ed23bd825f9bb892302f446366ad26420a84c0512af4627c5dd24fd239de7feb
5
5
  SHA512:
6
- metadata.gz: a8e9018040b8068c41e98f57682d12e3e08ce191ebef5c252185ff8ccacfbd71f75d6935a03e35c883e240ee5b402fd997c934779faf831e112cf46fe5d3c073
7
- data.tar.gz: e51b2512f9c078a2f094d4fb027c038c097ac7c1a4410da898563abc9eac2c94fa1e84f51e3438576ac9785b2e8ee6999b1b18b8505f1ab9c85be1095e1a7c3c
6
+ metadata.gz: 0ed0b00d4f70aa52fa11299c274467e04ecd4070d9cad5ef63befe2c380186e24d13a9c7545faee0178587085f129b0af3c5f1e7376f66dea2bccf3219382c6f
7
+ data.tar.gz: 8862017bcd151691898293a7c4756138601ac8fd89ae6f4509024c3448c2740451e956f0ebfe83a6f42890ea1fcbfbf61a55afb6d5101b8b17cbe7dd5acf8efa
data/README.md CHANGED
@@ -70,6 +70,8 @@ class UserDecorator < Tramway::BaseDecorator
70
70
  # delegates attributes to decorated object
71
71
  delegate_attributes :email, :first_name, :last_name
72
72
 
73
+ association :posts
74
+
73
75
  # you can provide your own methods with access to decorated object attributes with the method `object`
74
76
  def created_at
75
77
  I18n.l object.created_at
@@ -116,6 +118,17 @@ def show
116
118
  end
117
119
  ```
118
120
 
121
+ #### Decorate associations
122
+
123
+ ```ruby
124
+ class UserDecorator < Tramway::BaseDecorator
125
+ association :posts
126
+ end
127
+
128
+ user = tramway_decorate User.first
129
+ user.posts # => decorated collection of posts with PostDecorator
130
+ ```
131
+
119
132
  #### Decorate nil
120
133
 
121
134
  Tramway Decorator does not decorate nil objects
@@ -198,6 +211,8 @@ class UsersController < ApplicationController
198
211
  end
199
212
  ```
200
213
 
214
+ We also provide `submit!` as `save!` method that returns an exception in case of failed saving.
215
+
201
216
  #### Implement Form objects for any case
202
217
 
203
218
  *app/forms/user_updating_email_form.rb*
@@ -433,6 +448,18 @@ Pagination buttons looks like [this](https://play.tailwindcss.com/mqgDS5l9oY)
433
448
 
434
449
  **Tramway Decorator** and **Tramway Form** support `behave_as_ar` method. It allows to use `update` and `destroy` methods with decorated and form objects.
435
450
 
451
+ ### `object` method
452
+
453
+ **Tramway Decorator** and **Tramway Form** have public `object` method. It allows to access ActiveRecord object itself.
454
+
455
+ ```ruby
456
+ user_1 = tramway_decorate User.first
457
+ user_1.object #=> returns pure user object
458
+
459
+ user_2 = tramway_form User.first
460
+ user_2.object #=> returns pure user object
461
+ ```
462
+
436
463
  ## Contributing
437
464
 
438
465
  Install [lefthook](https://github.com/evilmartians/lefthook)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tramway/decorators/name_builder'
4
+ require 'tramway/decorators/association'
3
5
  require 'tramway/decorators/collection_decorator'
4
6
  require 'tramway/utils/render'
5
7
  require 'tramway/duck_typing'
@@ -38,6 +40,8 @@ module Tramway
38
40
  delegate attribute, to: :object
39
41
  end
40
42
  end
43
+
44
+ include Tramway::Decorators::AssociationClassMethods
41
45
  end
42
46
 
43
47
  def to_partial_path
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Decorators
5
+ # Module for defining association instance-level methods
6
+ module Association
7
+ private
8
+
9
+ # :reek:UtilityFunction { enabled: false }
10
+ def decorate_has_many_association(assoc)
11
+ AssocDecoratorHelper.decorate_has_many_association(assoc)
12
+ end
13
+ end
14
+
15
+ # Module for defining association class-level methods
16
+ #
17
+ module AssociationClassMethods
18
+ def associations(*associations)
19
+ associations.each do |assoc|
20
+ association assoc
21
+ end
22
+ end
23
+
24
+ # has_and_belongs_to_many is not supported for now
25
+ def association(association)
26
+ define_method(association) do
27
+ assoc = object.send(association)
28
+
29
+ if assoc.is_a?(ActiveRecord::Relation)
30
+ AssocDecoratorHelper.decorate_has_many_association assoc
31
+ elsif assoc.present?
32
+ AssocDecoratorHelper.decorate_associated_object(assoc)
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # Helper module for association decorators
39
+ module AssocDecoratorHelper
40
+ class << self
41
+ def decorate_has_many_association(assoc)
42
+ assoc.empty? ? [] : decorator(assoc.klass).decorate(assoc)
43
+ end
44
+
45
+ def decorate_associated_object(assoc)
46
+ decorator(assoc.class).decorate(assoc)
47
+ end
48
+
49
+ def decorator(class_name)
50
+ Tramway::Decorators::NameBuilder.default_decorator_class_name(class_name).constantize
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -16,7 +16,7 @@ module Tramway
16
16
  object_or_array.class
17
17
  end
18
18
 
19
- "#{klass}Decorator".constantize
19
+ Tramway::Decorators::NameBuilder.default_decorator_class_name(klass).constantize
20
20
  end
21
21
  end
22
22
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Decorators
5
+ # Implements name convention for decorators
6
+ #
7
+ module NameBuilder
8
+ module_function
9
+
10
+ def default_decorator_class_name(klass)
11
+ "#{klass}Decorator"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.4.5.1'
4
+ VERSION = '0.4.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5.1
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-05-06 00:00:00.000000000 Z
12
+ date: 2024-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-struct
@@ -124,8 +124,10 @@ files:
124
124
  - lib/tramway/config.rb
125
125
  - lib/tramway/configs/entities/route.rb
126
126
  - lib/tramway/configs/entity.rb
127
+ - lib/tramway/decorators/association.rb
127
128
  - lib/tramway/decorators/class_helper.rb
128
129
  - lib/tramway/decorators/collection_decorator.rb
130
+ - lib/tramway/decorators/name_builder.rb
129
131
  - lib/tramway/duck_typing.rb
130
132
  - lib/tramway/duck_typing/active_record_compatibility.rb
131
133
  - lib/tramway/engine.rb