tramway 0.2.1.3 → 0.2.3

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: c2ed52c776d761d5afe99a7d31c99084191c032af8f35c13a49f1d3f6155d01f
4
- data.tar.gz: e6cee0a18222ae4d3a99f63a8b8959bfaec6dab11107006040451254c14aa4b4
3
+ metadata.gz: eee7e8b57c93fd646e96a26e07e9dab81b6deee977c50c2ab34b1df93f48e96d
4
+ data.tar.gz: 48d411dcce7d057eb867862f2b8410b5320bbf2dfdc47f5a0e06508f34049139
5
5
  SHA512:
6
- metadata.gz: 676f475214beb3bab65636d145bf797d48efadf0519294c2551b26caf58c47270238fce3f2a3bb72953a9a8d55708aa88fc4cda61deed86d60cd82f8cbc47bf8
7
- data.tar.gz: 3f5847c31384d66b892ab8eddec5a730579cd0fcebbf85302316b8f98c00677b7ff3b2c778cdb22e39427c7a3f10baa73c74599c7237c06c9b6a2c24c68fec60
6
+ metadata.gz: dc78481a707c0688c774469549b8c1dc319adfe51f67cdd871b4b4751f03367206c674298c764a54110aa9c9b634082c6c844e1b97df0e9d2b2411fb536fa527
7
+ data.tar.gz: 2bdf9eca337acd11afacb0fca442e6047b1b600d5b6a499bad31ad7522777edf0c2079c247491334a32eb3bb8682d54c78e596256b120c14a6c4a62593592150
data/README.md CHANGED
@@ -11,11 +11,43 @@ gem "view_component"
11
11
 
12
12
  ## Usage
13
13
 
14
+ ### Tramway Entities
15
+
16
+ Tramway is an entity-based framework. **Entity** is the class on whose objects actions be applied: _index, show, create, update, and destroy_. Tramway will support numerous classes as entities. For now, Entity could be only **ActiveRecord::Base** class.
17
+
18
+ #### Define entity for Tramway
19
+
20
+ *config/initializers/tramway.rb*
21
+ ```ruby
22
+ Tramway.configure do |config|
23
+ config.entities = [ :user, :podcast, :episode ] # entities based on models User, Podcast and Episode are defined
24
+ end
25
+ ```
26
+
27
+ By default, links to the Tramway Entities index page are rendered in [Tramway Navbar](https://github.com/Purple-Magic/tramway#tramway-navbar).
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
+
14
46
  ### Tailwind components
15
47
 
16
48
  Tramway uses [Tailwind](https://tailwindcss.com/) by default. All UI helpers are implemented with [ViewComponent](https://github.com/viewcomponent/view_component).
17
49
 
18
- ### Decorators
50
+ ### Tramway Decorators
19
51
 
20
52
  Tramway provides convenient decorators for your objects. **NOTE:** This is not the decorator pattern in its usual representation.
21
53
 
@@ -65,8 +97,7 @@ def show
65
97
  end
66
98
  ```
67
99
 
68
-
69
- ### Navbar
100
+ ### Tramway Navbar
70
101
 
71
102
  Tramway provides DSL for rendering Tailwind Navgiation bar.
72
103
 
@@ -97,6 +128,8 @@ background:
97
128
  intensity: Color intensity. Range: **100..950**. Used by Tailwind. Not supported in case of using HEX color in the background.color
98
129
  ```
99
130
 
131
+ **NOTE:** `tramway_navbar` method called without arguments and block of code will render only [Tramway Entities](https://github.com/Purple-Magic/tramway#tramway-entities) links on the left.
132
+
100
133
  #### nav.left and nav.right
101
134
 
102
135
  Tramway navbar provides `left` and `right` methods that puts items to left and right part of navbar.
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tramway/decorators/collection_decorator'
4
- require 'tramway/utils/decorators/render'
4
+ require 'tramway/utils/render'
5
5
 
6
6
  module Tramway
7
7
  # Provides decorate function for Tramway projects
8
8
  #
9
9
  class BaseDecorator
10
10
  include Tramway::Decorators::CollectionDecorators
11
- include Tramway::Utils::Decorators::Render
11
+ include Tramway::Utils::Render
12
12
 
13
13
  attr_reader :object
14
14
 
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'tramway/configs/entity'
5
+
6
+ module Tramway
7
+ # Basic configuration of Tramway
8
+ #
9
+ class Config
10
+ include Singleton
11
+
12
+ def initialize
13
+ @entities = []
14
+ end
15
+
16
+ def entities=(collection)
17
+ @entities = collection.map do |entity|
18
+ entity_options = entity.is_a?(Hash) ? entity : { name: entity }
19
+
20
+ Tramway::Configs::Entity.new(**entity_options)
21
+ end
22
+ end
23
+
24
+ attr_reader :entities
25
+ end
26
+ end
@@ -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
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tramway/configs/entities/route'
4
+
5
+ module Tramway
6
+ module Configs
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
11
+
12
+ def routes
13
+ OpenStruct.new index: Rails.application.routes.url_helpers.public_send(route_helper_method)
14
+ end
15
+
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
27
+
28
+ def model_class
29
+ name.camelize.constantize
30
+ rescue StandardError
31
+ nil
32
+ end
33
+
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
42
+ end
43
+ end
44
+ end
45
+ end
@@ -7,13 +7,11 @@ module Tramway
7
7
  # Providing navbar helpers for ActionView
8
8
  module NavbarHelper
9
9
  def tramway_navbar(**options)
10
- if block_given?
11
- initialize_navbar
10
+ initialize_navbar
12
11
 
13
- yield @navbar
12
+ yield @navbar if block_given?
14
13
 
15
- assign_navbar_items(options)
16
- end
14
+ assign_navbar_items(options)
17
15
 
18
16
  render_navbar_component(options)
19
17
  end
@@ -21,7 +19,7 @@ module Tramway
21
19
  private
22
20
 
23
21
  def initialize_navbar
24
- @navbar = Tramway::Navbar.new(self)
22
+ @navbar = Tramway::Navbar.new self
25
23
  end
26
24
 
27
25
  def assign_navbar_items(options)
@@ -3,18 +3,23 @@
3
3
  module Tramway
4
4
  # Navbar object provides left and right elements
5
5
  class Navbar
6
- attr_reader :items
6
+ attr_reader :items, :context
7
7
 
8
8
  def initialize(context)
9
- @items = {}
10
9
  @context = context
10
+ @items = { left: [], right: [] }
11
11
  @filling = nil
12
+
13
+ entities = Tramway.config.entities
14
+
15
+ return unless entities.any?
16
+
17
+ preset_left entities
12
18
  end
13
19
 
14
20
  def left
15
21
  return unless block_given?
16
22
 
17
- @items[:left] = []
18
23
  filling_side(:left)
19
24
  yield self
20
25
  reset_filling
@@ -23,7 +28,6 @@ module Tramway
23
28
  def right
24
29
  return unless block_given?
25
30
 
26
- @items[:right] = []
27
31
  filling_side(:right)
28
32
  yield self
29
33
  reset_filling
@@ -43,6 +47,16 @@ module Tramway
43
47
 
44
48
  private
45
49
 
50
+ def preset_left(entities)
51
+ filling_side :left
52
+
53
+ entities.each do |entity|
54
+ item entity.human_name.plural, entity.routes.index
55
+ end
56
+
57
+ reset_filling
58
+ end
59
+
46
60
  def provided_url_and_block?(url)
47
61
  url.present? && block_given?
48
62
  end
@@ -57,12 +71,14 @@ module Tramway
57
71
 
58
72
  def render_ignoring_block(text_or_url, url, options)
59
73
  options.merge!(href: url)
60
- @context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
74
+
75
+ context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
61
76
  end
62
77
 
63
78
  def render_using_block(text_or_url, options, &block)
64
79
  options.merge!(href: text_or_url)
65
- @context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
80
+
81
+ context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
66
82
  end
67
83
  end
68
84
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Utils
5
+ # Provides helper method render that depends on ActionController::Base.render method
6
+ #
7
+ module Render
8
+ def render(*args, &)
9
+ ActionController::Base.render(*args, &)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.2.1.3'
4
+ VERSION = '0.2.3'
5
5
  end
data/lib/tramway.rb CHANGED
@@ -1,11 +1,22 @@
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'
7
+ require 'tramway/config'
6
8
  require 'view_component/compiler'
7
9
  require 'view_component/engine'
8
10
 
9
11
  # Core module for the whole gem
10
12
  module Tramway
13
+ module_function
14
+
15
+ def configure(&)
16
+ yield config
17
+ end
18
+
19
+ def config
20
+ Tramway::Config.instance
21
+ end
11
22
  end
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.1.3
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-22 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
@@ -88,14 +102,18 @@ files:
88
102
  - lib/tasks/tramway_tasks.rake
89
103
  - lib/tramway.rb
90
104
  - lib/tramway/base_decorator.rb
105
+ - lib/tramway/config.rb
106
+ - lib/tramway/configs/entities/route.rb
107
+ - lib/tramway/configs/entity.rb
91
108
  - lib/tramway/decorators/class_helper.rb
92
109
  - lib/tramway/decorators/collection_decorator.rb
93
110
  - lib/tramway/engine.rb
94
111
  - lib/tramway/helpers/decorate_helper.rb
95
112
  - lib/tramway/helpers/navbar_helper.rb
96
113
  - lib/tramway/navbar.rb
97
- - lib/tramway/utils/decorators/render.rb
114
+ - lib/tramway/utils/render.rb
98
115
  - lib/tramway/version.rb
116
+ - lib/types.rb
99
117
  homepage: https://github.com/purple-magic/tramway
100
118
  licenses:
101
119
  - MIT
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway
4
- module Utils
5
- module Decorators
6
- # Provides helper method render that depends on ActionController::Base.render method
7
- #
8
- module Render
9
- def render(*args)
10
- ActionController::Base.render(*args, layout: false)
11
- end
12
- end
13
- end
14
- end
15
- end