tramway 0.4.5.1 → 0.4.7
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 +4 -4
- data/README.md +48 -1
- data/lib/tramway/base_decorator.rb +4 -0
- data/lib/tramway/base_form.rb +4 -0
- data/lib/tramway/decorators/association.rb +55 -0
- data/lib/tramway/decorators/class_helper.rb +1 -1
- data/lib/tramway/decorators/name_builder.rb +15 -0
- data/lib/tramway/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69d16412bfd7e8fcb33e3ddc27a8be714350cd7fe97a56451a47dced641546c7
|
4
|
+
data.tar.gz: 60d32317870e16a0e4aadc618a8a2170939fda2e7d06d7202935a40d1ee945f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/tramway/base_form.rb
CHANGED
@@ -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
|
@@ -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
|
data/lib/tramway/version.rb
CHANGED
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.
|
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-
|
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
|