tramway 2.2.3.3 → 2.2.4

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: a4ac0f8b16c48697499fff448ea5c1afd8f10a7edb23f3202c7b2e27270ad5d5
4
- data.tar.gz: 409afb51bb0244d2bbcf609d0ce0b4a8a3b408b1daf1b1e0adc109f827a2ca49
3
+ metadata.gz: 16dea01fb21b124c9230e27238eb4d7f4f872d3e4348b9b16ca79e48e4ad1ac8
4
+ data.tar.gz: 8dfbda56fba27bb45e93e8048823f8b32adce01a5f8e8412608e13c694022005
5
5
  SHA512:
6
- metadata.gz: 521e251f67fe5e7157ff3256c77da7372174578b936b742055ad6cc7e7337a297df74487076bd189b24a5f1634775e481c4067b62fba2bde9213f654c7143173
7
- data.tar.gz: 4812134f889e1dd2b943cbd317ce3a1b097b78cf5460a50d86dc9d4dca082844913e8b37732fc572c2982058bb419c447dc43c673bb20c14768800bb22ef5e69
6
+ metadata.gz: 64e58a748628c72a579964e6c604695a29711b28a673eae560922dc2dd5c179116767a2026e15a9d898dc773ac62ee92b3afc4c2b94c921933c1e8b1bac57260
7
+ data.tar.gz: 83b5e8825a9ef8592ea244ab883e363136b9b77281b3c42ff5d256818e33a9330a683fb3074b1c902c27cf014a1f852a5f3a788f152d0af8a133648b1c4681fe
data/README.md CHANGED
@@ -272,13 +272,17 @@ method name and the remaining keys are passed as named arguments.
272
272
 
273
273
  ```ruby
274
274
  class UserForm < Tramway::BaseForm
275
- properties :email, :about_me
275
+ properties :email, :about_me, :user_type
276
276
 
277
277
  fields email: :email,
278
278
  name: :text,
279
279
  about_me: {
280
280
  type: :text_area,
281
281
  rows: 5
282
+ },
283
+ user_type: {
284
+ type: :select,
285
+ collection: ['regular', 'user']
282
286
  }
283
287
  end
284
288
  ```
@@ -9,23 +9,6 @@ module Tailwinds
9
9
  classic: 'div-table-cell md:block first:block hidden px-6 py-4 font-medium text-gray-100 text-base'
10
10
  )
11
11
  end
12
-
13
- def around_render
14
- ensure_view_context_accessor
15
- previous_flag = view_context.tramway_inside_cell
16
- view_context.tramway_inside_cell = true
17
- yield
18
- ensure
19
- view_context.tramway_inside_cell = previous_flag
20
- end
21
-
22
- private
23
-
24
- def ensure_view_context_accessor
25
- return if view_context.respond_to?(:tramway_inside_cell=)
26
-
27
- view_context.singleton_class.attr_accessor :tramway_inside_cell
28
- end
29
12
  end
30
13
  end
31
14
  end
@@ -44,6 +44,24 @@ module Tailwinds
44
44
  classic: 'div-table-cell px-6 py-4 font-medium text-gray-100 text-xs sm:text-base'
45
45
  )
46
46
  end
47
+
48
+ def around_render
49
+ ensure_view_context_accessor
50
+ previous_flag = view_context.tramway_inside_cell
51
+ view_context.tramway_inside_cell = href.present?
52
+
53
+ yield
54
+ ensure
55
+ view_context.tramway_inside_cell = previous_flag
56
+ end
57
+
58
+ private
59
+
60
+ def ensure_view_context_accessor
61
+ return if view_context.respond_to?(:tramway_inside_cell=)
62
+
63
+ view_context.singleton_class.attr_accessor :tramway_inside_cell
64
+ end
47
65
  end
48
66
  end
49
67
  end
data/docs/AGENTS.md CHANGED
@@ -358,6 +358,70 @@ In case you implementing API, use `api` namespaces for forms and decorators.
358
358
  ### Rule 25
359
359
  DO NOT use `#{model_name}_params` method with `permit` method inside controllers. When you use `tramway_form`, it's unnecessary.
360
360
 
361
+ ### Rule 26
362
+ DO NOT create new private methods in the controller for business logic stuff. Use service objects instead.
363
+ Create `app/services/base_service.rb` if it does not exist.
364
+
365
+ ```ruby
366
+ class BaseService
367
+ extend Dry::Initializer[undefined: false]
368
+ include Dry::Monads[:do, :result]
369
+
370
+ def self.call(...)
371
+ new(...).call
372
+ end
373
+ end
374
+ ```
375
+
376
+ And instead of this in a controller
377
+
378
+ ```ruby
379
+ def create
380
+ user = tramway_form User.new
381
+
382
+ if user.submit params[:user]
383
+ notify_admin user
384
+
385
+ # other stuff
386
+ else
387
+ end
388
+ end
389
+
390
+ private
391
+
392
+ def notify_admin(user)
393
+ # stuff
394
+ end
395
+ ```
396
+
397
+ Make this
398
+
399
+ *app/services/notify_admin.rb*
400
+ ```ruby
401
+ class NotifyAdmin < BaseService
402
+ option :user
403
+
404
+ def call
405
+ # stuff
406
+ end
407
+ end
408
+ ```
409
+
410
+ and in a controller
411
+
412
+ ```
413
+ def create
414
+ user = tramway_form User.new
415
+
416
+ if user.submit params[:user]
417
+ NotifyAdmin.call user:
418
+
419
+ # other stuff
420
+ else
421
+ end
422
+ end
423
+ ```
424
+
361
425
  ## Controller Patterns
362
426
 
363
427
  - Keep actions short and explicit with guard clauses.
@@ -7,7 +7,25 @@ module Tramway
7
7
  # Class methods for defining fields
8
8
  module ClassMethods
9
9
  def fields(**attributes)
10
- @fields.merge! attributes
10
+ attributes.any? ? __set_fields(attributes) : __fields
11
+ end
12
+
13
+ def __set_fields(attributes)
14
+ attributes.each do |(attribute, field_data)|
15
+ @fields.merge! attribute => field_data
16
+ end
17
+ end
18
+
19
+ def __fields
20
+ @fields.merge(__ancestor_fields)
21
+ end
22
+
23
+ def __ancestor_fields(klass = superclass)
24
+ superklass = klass.superclass
25
+
26
+ return {} unless superklass.respond_to?(:fields)
27
+
28
+ klass.fields.merge(__ancestor_fields(superklass))
11
29
  end
12
30
 
13
31
  def __initialize_fields(subclass)
@@ -4,27 +4,47 @@ module Tramway
4
4
  module Utils
5
5
  # Provides dynamic field rendering
6
6
  module Field
7
- def tramway_field(field_type, attribute, **, &)
8
- if field_type.is_a?(Hash)
9
- name = field_name(field_type[:type])
10
- value = field_type[:value]&.call
7
+ def tramway_field(field_data, attribute, **options, &)
8
+ input_type = field_type(field_data)
9
+ input_name = field_name input_type
10
+ input_options = field_options(field_data).merge(options)
11
11
 
12
- hash = { value:, **field_type.except(:type, :value) }.compact
12
+ case input_type.to_sym
13
+ when :select, :multiselect
14
+ collection = input_options.delete(:collection)
13
15
 
14
- public_send(name, attribute, **hash, **, &)
16
+ public_send(input_name, attribute, collection, **input_options, &)
15
17
  else
16
- public_send(field_name(field_type), attribute, **, &)
18
+ public_send(input_name, attribute, **input_options, &)
17
19
  end
18
20
  end
19
21
 
20
22
  private
21
23
 
22
- def field_name(field_type)
23
- case field_type.to_sym
24
+ def field_name(field_data)
25
+ case field_data.to_sym
24
26
  when :text_area, :select, :multiselect
25
- field_type
27
+ field_data
26
28
  else
27
- "#{field_type}_field"
29
+ "#{field_data}_field"
30
+ end
31
+ end
32
+
33
+ def field_type(field_data)
34
+ if field_data.is_a?(Hash)
35
+ field_data[:type]
36
+ else
37
+ field_data
38
+ end
39
+ end
40
+
41
+ def field_options(field_data)
42
+ if field_data.is_a?(Hash)
43
+ value = field_data[:value]&.call
44
+
45
+ field_data.merge(value:).except(:type)
46
+ else
47
+ {}
28
48
  end
29
49
  end
30
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '2.2.3.3'
4
+ VERSION = '2.2.4'
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: 2.2.3.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme