tramway 0.4.5.1 → 0.4.7

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: ef5809ca8f857fbc319595ce2ada4409120a4a89dc657bf2f7625e9b329d7d47
4
- data.tar.gz: da8094f83cc2f21d4470afef2311c790a079f9597aee99ebb448ea98cb83df61
3
+ metadata.gz: 69d16412bfd7e8fcb33e3ddc27a8be714350cd7fe97a56451a47dced641546c7
4
+ data.tar.gz: 60d32317870e16a0e4aadc618a8a2170939fda2e7d06d7202935a40d1ee945f7
5
5
  SHA512:
6
- metadata.gz: a8e9018040b8068c41e98f57682d12e3e08ce191ebef5c252185ff8ccacfbd71f75d6935a03e35c883e240ee5b402fd997c934779faf831e112cf46fe5d3c073
7
- data.tar.gz: e51b2512f9c078a2f094d4fb027c038c097ac7c1a4410da898563abc9eac2c94fa1e84f51e3438576ac9785b2e8ee6999b1b18b8505f1ab9c85be1095e1a7c3c
6
+ metadata.gz: a31a8b31885e7bf84aed5896850e7e98930fcf7bdf5dcc74eaf006172b7d6f305458f79c659618ac8da82d2050d18b818a891ac93ef1ff4bf01a5fe20cf8dabc
7
+ data.tar.gz: f21ecdb0d87afb50afae3c3b5a01d953f3736778fad9f0e47e2b16e8a1d35b33002ef4bf96b1624af20e2aaa619d90c7195b1ed225bad68279e211dbf795f6f7
data/README.md CHANGED
@@ -18,6 +18,12 @@ gem "tramway"
18
18
  gem "view_component"
19
19
  ```
20
20
 
21
+ OR
22
+
23
+ ```shell
24
+ bundle add tramway view_component
25
+ ```
26
+
21
27
  ## Usage
22
28
 
23
29
  ### Tramway Entities
@@ -70,6 +76,8 @@ class UserDecorator < Tramway::BaseDecorator
70
76
  # delegates attributes to decorated object
71
77
  delegate_attributes :email, :first_name, :last_name
72
78
 
79
+ association :posts
80
+
73
81
  # you can provide your own methods with access to decorated object attributes with the method `object`
74
82
  def created_at
75
83
  I18n.l object.created_at
@@ -116,6 +124,17 @@ def show
116
124
  end
117
125
  ```
118
126
 
127
+ #### Decorate associations
128
+
129
+ ```ruby
130
+ class UserDecorator < Tramway::BaseDecorator
131
+ association :posts
132
+ end
133
+
134
+ user = tramway_decorate User.first
135
+ user.posts # => decorated collection of posts with PostDecorator
136
+ ```
137
+
119
138
  #### Decorate nil
120
139
 
121
140
  Tramway Decorator does not decorate nil objects
@@ -198,6 +217,8 @@ class UsersController < ApplicationController
198
217
  end
199
218
  ```
200
219
 
220
+ We also provide `submit!` as `save!` method that returns an exception in case of failed saving.
221
+
201
222
  #### Implement Form objects for any case
202
223
 
203
224
  *app/forms/user_updating_email_form.rb*
@@ -307,7 +328,21 @@ class UserForm < Tramway::BaseForm
307
328
  end
308
329
  ```
309
330
 
310
- #### Update and Destroy
331
+ ### Assign values
332
+
333
+ Tramway Form provides `assign` method that allows to assign values without saving
334
+
335
+ ```ruby
336
+ class UsersController < ApplicationController
337
+ def update
338
+ @user = tramway_form User.new
339
+ @user.assign params[:user] # assigns values to the form object
340
+ @user.reload # restores previous values
341
+ end
342
+ end
343
+ ```
344
+
345
+ ### Update and Destroy
311
346
 
312
347
  Read [behave_as_ar](https://github.com/Purple-Magic/tramway#behave_as_ar) section
313
348
 
@@ -433,6 +468,18 @@ Pagination buttons looks like [this](https://play.tailwindcss.com/mqgDS5l9oY)
433
468
 
434
469
  **Tramway Decorator** and **Tramway Form** support `behave_as_ar` method. It allows to use `update` and `destroy` methods with decorated and form objects.
435
470
 
471
+ ### `object` method
472
+
473
+ **Tramway Decorator** and **Tramway Form** have public `object` method. It allows to access ActiveRecord object itself.
474
+
475
+ ```ruby
476
+ user_1 = tramway_decorate User.first
477
+ user_1.object #=> returns pure user object
478
+
479
+ user_2 = tramway_form User.first
480
+ user_2.object #=> returns pure user object
481
+ ```
482
+
436
483
  ## Contributing
437
484
 
438
485
  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
@@ -45,6 +45,10 @@ module Tramway
45
45
  end
46
46
  end
47
47
 
48
+ def assign(params)
49
+ __submit params
50
+ end
51
+
48
52
  def method_missing(method_name, *args)
49
53
  if method_name.to_s.end_with?('=') && args.count == 1
50
54
  object.public_send(method_name, args.first)
@@ -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.7'
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.7
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-07-04 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