tramway 0.3.1.2 → 0.3.2

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: 0becd1c6b67984097bf36b6e6a2180792863ded76f5e28174ea213e3f5f85ddd
4
- data.tar.gz: a74d5284069e8fa7c162b5c1dce27ba3fc85bb3558c0c934527b2365ab37bf8d
3
+ metadata.gz: fc95797d7be71f61b7a8350b3ef71905d41c8281d3a134feefc4e8477b910b44
4
+ data.tar.gz: 9fc4aa0e0bbec968e96620532082142b3ad48ab701e97ab52c2bd05a8236c047
5
5
  SHA512:
6
- metadata.gz: 45a89fec8a66c5e33f3321d18dfb7971bb0fe52f2c1984b86958453364af2c7327d8fba8a80a36f83e3cb40aa93da5aae2cd2b319658dbf1ca21cea122375692
7
- data.tar.gz: e3087975aa2d174982434340b32964c39817c7a95baabbb0f912c54dbfda682e3c1270d6600896ac5ff9ad94c0bebbba59de4ea724933b96c2a1cb95efcb0861
6
+ metadata.gz: ef921bd83394ebde2404a8e5e1b7332af573713553ed73676850955ff8e32f9fc93cdddfa0124658ad40c096557fcd60ed420a55d252f5e5c3fb40ea34cae19e
7
+ data.tar.gz: d0a2b72e6e35ea9a297a9af346e794cf1833e8b5922c2e09d98a5f8f06a2d283db49b33753510535a0594365b072aff666712ee50a0b2c9d6036aa530ab52e38
data/README.md CHANGED
@@ -81,7 +81,7 @@ class UserDecorator < Tramway::BaseDecorator
81
81
  end
82
82
  ```
83
83
 
84
- #### Decorate single object
84
+ #### Decorate a single object
85
85
 
86
86
  You can use the same method to decorate a single object either
87
87
 
@@ -91,6 +91,20 @@ def show
91
91
  end
92
92
  ```
93
93
 
94
+ #### Decorate a collection of objects
95
+
96
+ ```ruby
97
+ def index
98
+ @users = tramway_decorate User.all
99
+ end
100
+ ```
101
+
102
+ ```ruby
103
+ def index
104
+ @posts = tramway_decorate user.posts
105
+ end
106
+ ```
107
+
94
108
  #### Decorate with a specific decorator
95
109
 
96
110
  You can implement a specific decorator and ask Tramway to decorate with it
@@ -334,6 +348,7 @@ Tramway provides `tramway_form_for` helper that renders Tailwind-styled forms by
334
348
  = tramway_form_for User.new do |f|
335
349
  = f.text_field :text
336
350
  = f.password_field :password
351
+ = f.select :role, [:admin, :user]
337
352
  = f.file_field :file
338
353
  = f.submit "Create User"
339
354
  ```
@@ -344,6 +359,7 @@ Available form helpers:
344
359
  * text_field
345
360
  * password_field
346
361
  * file_field
362
+ * select
347
363
  * submit
348
364
 
349
365
  ## Contributing
@@ -4,4 +4,12 @@ require 'view_component'
4
4
 
5
5
  # Base TailwindComponent. Contains base features for all tailwind components
6
6
  class TailwindComponent < ViewComponent::Base
7
+ extend Dry::Initializer[undefined: false]
8
+
9
+ option :template
10
+ option :attribute
11
+ option :object_name
12
+ option :options
13
+ option :label
14
+ option :for
7
15
  end
@@ -3,21 +3,22 @@
3
3
  module Tailwinds
4
4
  module Form
5
5
  # Provides Tailwind-styled forms
6
+ # :reek:InstanceVariableAssumption
6
7
  class Builder < Tramway::Views::FormBuilder
7
8
  def text_field(attribute, **options, &)
8
- input = super(attribute, **options.merge(class: text_input_class))
9
- render(Tailwinds::Form::TextFieldComponent.new(input, attribute, object_name:, **options), &)
9
+ render(Tailwinds::Form::TextFieldComponent.new(**default_options(attribute, options)), &)
10
10
  end
11
11
 
12
12
  def password_field(attribute, **options, &)
13
- input = super(attribute, **options.merge(class: text_input_class))
14
- render(Tailwinds::Form::TextFieldComponent.new(input, attribute, object_name:, **options), &)
13
+ render(Tailwinds::Form::TextFieldComponent.new(**default_options(attribute, options)), &)
15
14
  end
16
15
 
17
16
  def file_field(attribute, **options, &)
18
- input = super(attribute, **options.merge(class: :hidden))
17
+ render(Tailwinds::Form::FileFieldComponent.new(**default_options(attribute, options)), &)
18
+ end
19
19
 
20
- render(Tailwinds::Form::FileFieldComponent.new(input, attribute, object_name:, **options), &)
20
+ def select(attribute, collection, **options, &)
21
+ render(Tailwinds::Form::SelectComponent.new(**default_options(attribute, options).merge(collection:)), &)
21
22
  end
22
23
 
23
24
  def submit(action, **options, &)
@@ -26,8 +27,24 @@ module Tailwinds
26
27
 
27
28
  private
28
29
 
29
- def text_input_class
30
- 'w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:border-red-500'
30
+ def default_options(attribute, options)
31
+ {
32
+ template: @template,
33
+ attribute:,
34
+ object_name:,
35
+ label: label(attribute, options),
36
+ for: for_id(attribute),
37
+ options:
38
+ }
39
+ end
40
+
41
+ # :reek:UtilityFunction
42
+ def label(attribute, options)
43
+ options[:label] || attribute.to_s.humanize
44
+ end
45
+
46
+ def for_id(attribute)
47
+ "#{object_name}_#{attribute}"
31
48
  end
32
49
  end
33
50
  end
@@ -1,4 +1,4 @@
1
1
  .mb-4
2
2
  %label.inline-block.bg-blue-500.hover:bg-blue-700.text-white.font-bold.py-2.px-4.rounded.cursor-pointer.mt-4{ for: @for }
3
3
  = @label
4
- = @input
4
+ = @template.file_field @object_name, @attribute
@@ -4,11 +4,6 @@ module Tailwinds
4
4
  module Form
5
5
  # Tailwind-styled file_field input
6
6
  class FileFieldComponent < TailwindComponent
7
- def initialize(input, attribute, object_name: nil, **options)
8
- @label = options[:label] || attribute.to_s.capitalize
9
- @for = "#{object_name}_#{attribute}"
10
- @input = input
11
- end
12
7
  end
13
8
  end
14
9
  end
@@ -0,0 +1,4 @@
1
+ .mb-4
2
+ %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
3
+ = @label
4
+ = @template.select @object_name, @attribute, @collection, {}, @options.merge(class: 'bg-white border border-gray-300 text-gray-700 py-2 px-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent')
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailwinds
4
+ module Form
5
+ # Tailwind-styled text field
6
+ class SelectComponent < TailwindComponent
7
+ option :collection
8
+ end
9
+ end
10
+ end
@@ -1,4 +1,4 @@
1
1
  .mb-4
2
2
  %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
3
3
  = @label
4
- = @input
4
+ = @template.text_field @object_name, @attribute, **@options.merge(class: 'w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:border-red-500')
@@ -4,11 +4,6 @@ module Tailwinds
4
4
  module Form
5
5
  # Tailwind-styled text field
6
6
  class TextFieldComponent < TailwindComponent
7
- def initialize(input, attribute, object_name: nil, **options)
8
- @label = options[:label] || attribute.to_s.humanize
9
- @for = "#{object_name}_#{attribute}"
10
- @input = input
11
- end
12
7
  end
13
8
  end
14
9
  end
@@ -12,6 +12,8 @@ module Tramway
12
12
 
13
13
  def initialize(object)
14
14
  @object = object
15
+
16
+ self.class.delegate object.class.primary_key, to: :object
15
17
  end
16
18
 
17
19
  class << self
@@ -14,7 +14,7 @@ module Tramway
14
14
  end
15
15
 
16
16
  def collection?(object)
17
- object.class.name.in? ['ActiveRecord::Relation', 'Array']
17
+ object.is_a?(Array) || object.is_a?(ActiveRecord::Relation)
18
18
  end
19
19
  end
20
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.3.1.2'
4
+ VERSION = '0.3.2'
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.3.1.2
4
+ version: 0.3.2
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: 2023-12-23 00:00:00.000000000 Z
12
+ date: 2024-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-struct
@@ -96,6 +96,8 @@ files:
96
96
  - app/components/tailwinds/form/builder.rb
97
97
  - app/components/tailwinds/form/file_field_component.html.haml
98
98
  - app/components/tailwinds/form/file_field_component.rb
99
+ - app/components/tailwinds/form/select_component.html.haml
100
+ - app/components/tailwinds/form/select_component.rb
99
101
  - app/components/tailwinds/form/submit_button_component.html.haml
100
102
  - app/components/tailwinds/form/submit_button_component.rb
101
103
  - app/components/tailwinds/form/text_field_component.html.haml