tramway 0.4.3 → 0.4.5

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: cae2dee0a95959ec9a4d2edb886ffd6fbd2ec305770e0ba78f9dc09f04d641ce
4
- data.tar.gz: e3d287cd49a4b181279aa8574bec79364af8b9c0b240299cddb3f7ae18dfcd55
3
+ metadata.gz: 54d135e0a0a71941eecdbbbf042963727d6b0e13e7a024f1c86f22d099d76feb
4
+ data.tar.gz: fa8e45c0a89c065df62d580e368b1e73a43a4abd06c9e542d8b8a8706dba24c0
5
5
  SHA512:
6
- metadata.gz: a2cdabe494be288cc8c1d538b727e41cfa0ce2d8d0c49ca8be1561dbc1780e4ba5826be7ee9c9d7c62dff3749b0a94568f747d03d35405c74a22e62b24e210d3
7
- data.tar.gz: 36b601a82003ea9c623247d57a068b60fb98998265d7d5dc2a9f505c8e8accbf7ba4e7831bf36601fb05e8b952600bd16900c84db7e8fe4b421b408349914ca5
6
+ metadata.gz: 2b2b51bb24ca9c973ccd2cc3a925c102a2bedc1c1c1daa43c5be00d4563cc6491a521796a00f7cd80c8019718709e41d683c6f69819cd96172af0a5756f9ef29
7
+ data.tar.gz: 5db00c69d1d90c75d924d30a6989d087affbe2b2de546d774bfb58b732efce5b5a6615bd6801cdcfdd55d9f0c088c34fa2f7223793cd4d0e8c268dd10334a25e
data/README.md CHANGED
@@ -8,7 +8,7 @@ Unite Ruby on Rails brilliance. Streamline development with Tramway.
8
8
  * [Tramway Form](https://github.com/Purple-Magic/tramway#tramway-form)
9
9
  * [Tramway Navbar](https://github.com/Purple-Magic/tramway#tramway-navbar)
10
10
  * [Tailwind-styled forms](https://github.com/Purple-Magic/tramway#tailwind-styled-forms)
11
- * [Tailwind-styled pagination](https://github.com/Purple-Magic/tramway#tailwind-styled-pagination)
11
+ * [Tailwind-styled pagination](https://github.com/Purple-Magic/tramway?tab=readme-ov-file#tailwind-styled-pagination-for-kaminari)
12
12
 
13
13
  ## Installation
14
14
  Add this line to your application's Gemfile:
@@ -125,6 +125,10 @@ user = nil
125
125
  UserDecorator.decorate user # => nil
126
126
  ```
127
127
 
128
+ #### Update and Destroy
129
+
130
+ Read [behave_as_ar](https://github.com/Purple-Magic/tramway#behave_as_ar) section
131
+
128
132
  ### Tramway Form
129
133
 
130
134
  Tramway provides **convenient** form objects for Rails applications. List properties you want to change and the rules in Form classes. No controllers overloading.
@@ -303,6 +307,10 @@ class UserForm < Tramway::BaseForm
303
307
  end
304
308
  ```
305
309
 
310
+ #### Update and Destroy
311
+
312
+ Read [behave_as_ar](https://github.com/Purple-Magic/tramway#behave_as_ar) section
313
+
306
314
  ### Tramway Navbar
307
315
 
308
316
  Tramway provides DSL for rendering Tailwind Navgiation bar.
@@ -421,6 +429,10 @@ end
421
429
 
422
430
  Pagination buttons looks like [this](https://play.tailwindcss.com/mqgDS5l9oY)
423
431
 
432
+ ### `behave_as_ar`
433
+
434
+ **Tramway Decorator** and **Tramway Form** support `behave_as_ar` method. It allows to use `update` and `destroy` methods with decorated and form objects.
435
+
424
436
  ## Contributing
425
437
 
426
438
  Install [lefthook](https://github.com/evilmartians/lefthook)
@@ -30,7 +30,7 @@ module Tailwinds
30
30
  render(Tailwinds::Form::SelectComponent.new(
31
31
  input: input(:select),
32
32
  value: options[:selected] || object.public_send(attribute),
33
- collection:,
33
+ collection: explicitly_add_blank_option(collection, options),
34
34
  **default_options(attribute, options)
35
35
  ), &)
36
36
  end
@@ -53,20 +53,32 @@ module Tailwinds
53
53
  def default_options(attribute, options)
54
54
  {
55
55
  attribute:,
56
- label: label(attribute, options),
56
+ label: label_build(attribute, options),
57
57
  for: for_id(attribute),
58
58
  options:
59
59
  }
60
60
  end
61
61
 
62
62
  # :reek:UtilityFunction
63
- def label(attribute, options)
64
- options[:label] || attribute.to_s.humanize
63
+ def label_build(attribute, options)
64
+ options[:label].presence || attribute.to_s.humanize
65
65
  end
66
66
 
67
67
  def for_id(attribute)
68
68
  "#{object_name}_#{attribute}"
69
69
  end
70
+
71
+ # REMOVE IT. WE MUST UNDERSTAND WHY INCLUDE_BLANK DOES NOT WORK
72
+ # :reek:UtilityFunction
73
+ def explicitly_add_blank_option(collection, options)
74
+ if options[:include_blank]
75
+ collection = collection.to_a if collection.is_a? Hash
76
+
77
+ collection.unshift([nil, ''])
78
+ else
79
+ collection
80
+ end
81
+ end
70
82
  end
71
83
  end
72
84
  end
@@ -1,4 +1,5 @@
1
1
  .mb-4
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
- = @label
2
+ - if @label
3
+ %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 }
4
+ = @label
4
5
  = @input
@@ -1,4 +1,5 @@
1
1
  .mb-4
2
- %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
3
- = @label
2
+ - if @label
3
+ %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
4
+ = @label
4
5
  = @input.call(@attribute, @collection, { selected: @value }, @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'))
@@ -1,4 +1,5 @@
1
1
  .mb-4
2
- %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
3
- = @label
2
+ - if @label
3
+ %label.block.text-gray-700.text-sm.font-bold.mb-2{ for: @for }
4
+ = @label
4
5
  = @input.call @attribute, **@options.merge(class: 'w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:border-red-500'), value: @value
@@ -0,0 +1,3 @@
1
+ %li
2
+ = button_to @href, method: @method, class: @style, **@options do
3
+ = content
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rules/turbo_html_attributes_rules'
4
+
5
+ module Tailwinds
6
+ module Nav
7
+ module Item
8
+ # Render button styled with Tailwind using button_to methods
9
+ #
10
+ class ButtonComponent < TailwindComponent
11
+ def initialize(**options)
12
+ @href = options[:href]
13
+ @method = options[:method]
14
+ @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
15
+ @options = options.except(:href, :method)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rules/turbo_html_attributes_rules'
4
+
5
+ module Tailwinds
6
+ module Nav
7
+ module Item
8
+ # Render button styled with Tailwind using link_to methods
9
+ #
10
+ class LinkComponent < TailwindComponent
11
+ def initialize(**options)
12
+ @href = options[:href]
13
+ @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
14
+ @options = Rules::TurboHtmlAttributesRules.prepare_turbo_html_attributes(options:)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # desc "Explaining what the task does"
3
4
  # task :tramway do
4
5
  # # Task goes here
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'tramway/decorators/collection_decorator'
4
4
  require 'tramway/utils/render'
5
+ require 'tramway/duck_typing'
5
6
 
6
7
  module Tramway
7
8
  # Provides decorate function for Tramway projects
@@ -9,6 +10,7 @@ module Tramway
9
10
  class BaseDecorator
10
11
  include Tramway::Decorators::CollectionDecorators
11
12
  include Tramway::Utils::Render
13
+ include Tramway::DuckTyping::ActiveRecordCompatibility
12
14
 
13
15
  attr_reader :object
14
16
 
@@ -38,16 +40,10 @@ module Tramway
38
40
  end
39
41
  end
40
42
 
41
- delegate_attributes :id
42
-
43
43
  def to_partial_path
44
44
  underscored_class_name = object.class.name.underscore
45
45
 
46
46
  "#{underscored_class_name.pluralize}/#{underscored_class_name}"
47
47
  end
48
-
49
- def to_param
50
- id.to_s
51
- end
52
48
  end
53
49
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'tramway/forms/properties'
4
4
  require 'tramway/forms/normalizations'
5
+ require 'tramway/duck_typing'
5
6
 
6
7
  module Tramway
7
8
  # Provides form object for Tramway
@@ -9,13 +10,10 @@ module Tramway
9
10
  class BaseForm
10
11
  include Tramway::Forms::Properties
11
12
  include Tramway::Forms::Normalizations
13
+ include Tramway::DuckTyping::ActiveRecordCompatibility
12
14
 
13
15
  attr_reader :object
14
16
 
15
- %i[model_name to_key to_model errors attributes update destroy].each do |method_name|
16
- delegate method_name, to: :object
17
- end
18
-
19
17
  def initialize(object)
20
18
  @object = object
21
19
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module DuckTyping
5
+ # This module is used to make duck object compatible with ActiveRecord objects
6
+ module ActiveRecordCompatibility
7
+ # Contains behave_as_ar method
8
+ module ClassMethods
9
+ def behave_as_ar
10
+ %i[update update! destroy].each do |method_name|
11
+ delegate method_name, to: :object
12
+ end
13
+ end
14
+ end
15
+
16
+ %i[id model_name to_key errors to_param attributes].each do |method_name|
17
+ delegate method_name, to: :object
18
+ end
19
+
20
+ def self.included(base)
21
+ base.extend ClassMethods
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tramway/duck_typing/active_record_compatibility'
4
+
5
+ module Tramway
6
+ # This module is used to make duck object compatible
7
+ module DuckTyping
8
+ end
9
+ end
@@ -37,9 +37,9 @@ module Tramway
37
37
  raise 'You cannot provide an argument and a code block at the same time' if provided_url_and_block?(url, &block)
38
38
 
39
39
  rendered_item = if url.present?
40
- render_ignoring_block(text_or_url, url, options)
40
+ render_ignoring_block(text_or_url, url, **options)
41
41
  else
42
- render_using_block(text_or_url, options, &block)
42
+ render_using_block(text_or_url, **options, &block)
43
43
  end
44
44
 
45
45
  @items[@filling] << rendered_item
@@ -69,16 +69,24 @@ module Tramway
69
69
  @filling = nil
70
70
  end
71
71
 
72
- def render_ignoring_block(text_or_url, url, options)
72
+ def render_ignoring_block(text_or_url, url, method: nil, **options)
73
73
  options.merge!(href: url)
74
74
 
75
- context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
75
+ if method.present? && method.to_sym != :get
76
+ context.render(Tailwinds::Nav::Item::ButtonComponent.new(method:, **options)) { text_or_url }
77
+ else
78
+ context.render(Tailwinds::Nav::Item::LinkComponent.new(method:, **options)) { text_or_url }
79
+ end
76
80
  end
77
81
 
78
- def render_using_block(text_or_url, options, &block)
82
+ def render_using_block(text_or_url, method: nil, **options, &block)
79
83
  options.merge!(href: text_or_url)
80
84
 
81
- context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
85
+ if method.present? && method.to_sym != :get
86
+ context.render(Tailwinds::Nav::Item::ButtonComponent.new(method:, **options), &block)
87
+ else
88
+ context.render(Tailwinds::Nav::Item::LinkComponent.new(method:, **options), &block)
89
+ end
82
90
  end
83
91
  end
84
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.4.3'
4
+ VERSION = '0.4.5'
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.3
4
+ version: 0.4.5
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-03-05 00:00:00.000000000 Z
12
+ date: 2024-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-struct
@@ -102,8 +102,10 @@ files:
102
102
  - app/components/tailwinds/form/submit_button_component.rb
103
103
  - app/components/tailwinds/form/text_field_component.html.haml
104
104
  - app/components/tailwinds/form/text_field_component.rb
105
- - app/components/tailwinds/nav/item_component.html.haml
106
- - app/components/tailwinds/nav/item_component.rb
105
+ - app/components/tailwinds/nav/item/button_component.html.haml
106
+ - app/components/tailwinds/nav/item/button_component.rb
107
+ - app/components/tailwinds/nav/item/link_component.html.haml
108
+ - app/components/tailwinds/nav/item/link_component.rb
107
109
  - app/components/tailwinds/navbar_component.html.haml
108
110
  - app/components/tailwinds/navbar_component.rb
109
111
  - app/views/kaminari/_first_page.html.haml
@@ -124,6 +126,8 @@ files:
124
126
  - lib/tramway/configs/entity.rb
125
127
  - lib/tramway/decorators/class_helper.rb
126
128
  - lib/tramway/decorators/collection_decorator.rb
129
+ - lib/tramway/duck_typing.rb
130
+ - lib/tramway/duck_typing/active_record_compatibility.rb
127
131
  - lib/tramway/engine.rb
128
132
  - lib/tramway/forms/class_helper.rb
129
133
  - lib/tramway/forms/normalizations.rb
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rules/turbo_html_attributes_rules'
4
-
5
- module Tailwinds
6
- module Nav
7
- # Render button styled with Tailwind using button_to or link_to methods
8
- #
9
- class ItemComponent < TailwindComponent
10
- def initialize(**options)
11
- @href = options[:href]
12
- @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
13
- @options = Rules::TurboHtmlAttributesRules.prepare_turbo_html_attributes(options:)
14
- end
15
- end
16
- end
17
- end