tramway 0.4.5 → 0.4.6

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
  SHA256:
3
- metadata.gz: 54d135e0a0a71941eecdbbbf042963727d6b0e13e7a024f1c86f22d099d76feb
4
- data.tar.gz: fa8e45c0a89c065df62d580e368b1e73a43a4abd06c9e542d8b8a8706dba24c0
3
+ metadata.gz: 57af927fcbb72fd669c6c8b7f480c37b3a83abe44cd84cf9fbabcb2cfa842786
4
+ data.tar.gz: ed23bd825f9bb892302f446366ad26420a84c0512af4627c5dd24fd239de7feb
5
5
  SHA512:
6
- metadata.gz: 2b2b51bb24ca9c973ccd2cc3a925c102a2bedc1c1c1daa43c5be00d4563cc6491a521796a00f7cd80c8019718709e41d683c6f69819cd96172af0a5756f9ef29
7
- data.tar.gz: 5db00c69d1d90c75d924d30a6989d087affbe2b2de546d774bfb58b732efce5b5a6615bd6801cdcfdd55d9f0c088c34fa2f7223793cd4d0e8c268dd10334a25e
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)
@@ -60,8 +60,11 @@ module Tailwinds
60
60
  end
61
61
 
62
62
  # :reek:UtilityFunction
63
+ # :reek:NilCheck
63
64
  def label_build(attribute, options)
64
- options[:label].presence || attribute.to_s.humanize
65
+ label_option = options[:label]
66
+
67
+ label_option.nil? ? attribute.to_s.humanize : label_option
65
68
  end
66
69
 
67
70
  def for_id(attribute)
@@ -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'
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
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