tramway 0.2.2 → 0.2.3

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: 12b9bb73f91f5c694ee64efaa36d66a5ee68c6b41066a5e775b42e4ba81678d4
4
- data.tar.gz: 79c240ff539d4762fb8a2c4b3fe29ff05805c2699e12225d53038fafb53c5a1d
3
+ metadata.gz: eee7e8b57c93fd646e96a26e07e9dab81b6deee977c50c2ab34b1df93f48e96d
4
+ data.tar.gz: 48d411dcce7d057eb867862f2b8410b5320bbf2dfdc47f5a0e06508f34049139
5
5
  SHA512:
6
- metadata.gz: 5ccd8cdeb8bb8b9df08d4885ff473e68ef7a29f5980460050e52db4d7b7b758da170cabece248f78589dbc102d47f1ae4c3ee1410d50d8103cbb4ffc549d0a2e
7
- data.tar.gz: 58c752011aa51d3c0afd9e074380411657a390b0a09568226de9d4c38b033d1b9b23f172b27af2a3ae137bc342185fad3736085412025d66cac4dab68e4ff994
6
+ metadata.gz: dc78481a707c0688c774469549b8c1dc319adfe51f67cdd871b4b4751f03367206c674298c764a54110aa9c9b634082c6c844e1b97df0e9d2b2411fb536fa527
7
+ data.tar.gz: 2bdf9eca337acd11afacb0fca442e6047b1b600d5b6a499bad31ad7522777edf0c2079c247491334a32eb3bb8682d54c78e596256b120c14a6c4a62593592150
data/README.md CHANGED
@@ -26,6 +26,23 @@ end
26
26
 
27
27
  By default, links to the Tramway Entities index page are rendered in [Tramway Navbar](https://github.com/Purple-Magic/tramway#tramway-navbar).
28
28
 
29
+ #### Define entities with options
30
+
31
+ Tramway Entity supports several options that are used in different features.
32
+
33
+ **route**
34
+
35
+ *config/initializers/tramway.rb*
36
+ ```ruby
37
+ Tramway.configure do |config|
38
+ config.entities = [
39
+ { name: :user, route: { namespace: :admin } }, # `admin_users_path` link in the Tramway Navbar
40
+ { name: :podcast, route: { route_method: :shows } }, # `shows_path` link in the Tramway Navbar
41
+ { name: :episodes, route: { namespace: :podcasts, route_method: :episodes } }, # `podcasts_episodes_path` link in the Tramway Navbar
42
+ ]
43
+ end
44
+ ```
45
+
29
46
  ### Tailwind components
30
47
 
31
48
  Tramway uses [Tailwind](https://tailwindcss.com/) by default. All UI helpers are implemented with [ViewComponent](https://github.com/viewcomponent/view_component).
@@ -15,7 +15,9 @@ module Tramway
15
15
 
16
16
  def entities=(collection)
17
17
  @entities = collection.map do |entity|
18
- Tramway::Configs::Entity.new(name: entity)
18
+ entity_options = entity.is_a?(Hash) ? entity : { name: entity }
19
+
20
+ Tramway::Configs::Entity.new(**entity_options)
19
21
  end
20
22
  end
21
23
 
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Configs
5
+ module Entities
6
+ # Route struct describes rules for route management
7
+ #
8
+ class Route < Dry::Struct
9
+ attribute? :namespace, Types::Coercible::String
10
+ attribute? :route_method, Types::Coercible::String
11
+
12
+ def helper_method_by(underscored_name)
13
+ "#{[namespace, route_method || underscored_name].compact.join('_')}_path"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,24 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tramway/configs/entities/route'
4
+
3
5
  module Tramway
4
6
  module Configs
5
- # Tramway is entity based framework
6
- #
7
- class Entity
8
- attr_reader :name
7
+ # Tramway is an entity-based framework
8
+ class Entity < Dry::Struct
9
+ attribute :name, Types::Coercible::String
10
+ attribute? :route, Tramway::Configs::Entities::Route
9
11
 
10
- def initialize(name:)
11
- @name = name.to_s
12
+ def routes
13
+ OpenStruct.new index: Rails.application.routes.url_helpers.public_send(route_helper_method)
12
14
  end
13
15
 
14
- def routes
15
- underscored_name = name.parameterize.pluralize.underscore
16
+ def human_name
17
+ options = if model_class.present?
18
+ model_name = model_class.model_name.human
19
+ { single: model_name, plural: model_name.pluralize }
20
+ else
21
+ { single: name.capitalize, plural: name.pluralize.capitalize }
22
+ end
23
+ OpenStruct.new(**options)
24
+ end
25
+
26
+ private
16
27
 
17
- OpenStruct.new index: Rails.application.routes.url_helpers.public_send("#{underscored_name}_path")
28
+ def model_class
29
+ name.camelize.constantize
30
+ rescue StandardError
31
+ nil
18
32
  end
19
33
 
20
- def human_name
21
- OpenStruct.new single: name.capitalize, plural: name.pluralize.capitalize
34
+ def route_helper_method
35
+ underscored_name = name.parameterize.pluralize.underscore
36
+
37
+ if route.present?
38
+ route.helper_method_by(underscored_name)
39
+ else
40
+ "#{underscored_name}_path"
41
+ end
22
42
  end
23
43
  end
24
44
  end
@@ -51,8 +51,6 @@ module Tramway
51
51
  filling_side :left
52
52
 
53
53
  entities.each do |entity|
54
- entity.to_s.pluralize
55
-
56
54
  item entity.human_name.plural, entity.routes.index
57
55
  end
58
56
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.2.2'
4
+ VERSION = '0.2.3'
5
5
  end
data/lib/tramway.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'types'
3
4
  require 'tramway/version'
4
5
  require 'tramway/engine'
5
6
  require 'tramway/base_decorator'
data/lib/types.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+
5
+ # We need because of this https://dry-rb.org/gems/dry-struct/1.6/recipes/
6
+ module Types
7
+ include Dry.Types()
8
+ 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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-07-26 00:00:00.000000000 Z
12
+ date: 2023-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dry-struct
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: haml-rails
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -89,6 +103,7 @@ files:
89
103
  - lib/tramway.rb
90
104
  - lib/tramway/base_decorator.rb
91
105
  - lib/tramway/config.rb
106
+ - lib/tramway/configs/entities/route.rb
92
107
  - lib/tramway/configs/entity.rb
93
108
  - lib/tramway/decorators/class_helper.rb
94
109
  - lib/tramway/decorators/collection_decorator.rb
@@ -98,6 +113,7 @@ files:
98
113
  - lib/tramway/navbar.rb
99
114
  - lib/tramway/utils/render.rb
100
115
  - lib/tramway/version.rb
116
+ - lib/types.rb
101
117
  homepage: https://github.com/purple-magic/tramway
102
118
  licenses:
103
119
  - MIT