tramway 0.2.1.2 → 0.2.2

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: e2dd99c95cb679606a69128eb290b9c35fee411dc626ed2f43a1b3cfcba36efa
4
- data.tar.gz: 95ed3ccb11b51c8df14061aa9bc483257da99e05755f06935fd27bfabb3d1393
3
+ metadata.gz: 12b9bb73f91f5c694ee64efaa36d66a5ee68c6b41066a5e775b42e4ba81678d4
4
+ data.tar.gz: 79c240ff539d4762fb8a2c4b3fe29ff05805c2699e12225d53038fafb53c5a1d
5
5
  SHA512:
6
- metadata.gz: d759138c8522fa61b234affae4547f403992ade47ff99413e5abb42cc52101137294dcef77e4f17528d1d6463d8668fa85ef71cd1b0bc6f5042522aa8254fd27
7
- data.tar.gz: 2e63cf0b4fa9122c2f0e97aaad067a7b325c5644a4912c1636f4bc247e1f98dada96991ed231c1ab26d404dec0d719c406bacede57e21331576fbb005fdabf9d
6
+ metadata.gz: 5ccd8cdeb8bb8b9df08d4885ff473e68ef7a29f5980460050e52db4d7b7b758da170cabece248f78589dbc102d47f1ae4c3ee1410d50d8103cbb4ffc549d0a2e
7
+ data.tar.gz: 58c752011aa51d3c0afd9e074380411657a390b0a09568226de9d4c38b033d1b9b23f172b27af2a3ae137bc342185fad3736085412025d66cac4dab68e4ff994
data/README.md CHANGED
@@ -11,11 +11,26 @@ 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
+
14
29
  ### Tailwind components
15
30
 
16
31
  Tramway uses [Tailwind](https://tailwindcss.com/) by default. All UI helpers are implemented with [ViewComponent](https://github.com/viewcomponent/view_component).
17
32
 
18
- ### Decorators
33
+ ### Tramway Decorators
19
34
 
20
35
  Tramway provides convenient decorators for your objects. **NOTE:** This is not the decorator pattern in its usual representation.
21
36
 
@@ -65,8 +80,7 @@ def show
65
80
  end
66
81
  ```
67
82
 
68
-
69
- ### Navbar
83
+ ### Tramway Navbar
70
84
 
71
85
  Tramway provides DSL for rendering Tailwind Navgiation bar.
72
86
 
@@ -97,6 +111,8 @@ background:
97
111
  intensity: Color intensity. Range: **100..950**. Used by Tailwind. Not supported in case of using HEX color in the background.color
98
112
  ```
99
113
 
114
+ **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.
115
+
100
116
  #### nav.left and nav.right
101
117
 
102
118
  Tramway navbar provides `left` and `right` methods that puts items to left and right part of navbar.
@@ -1,34 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tramway/decorators/collection_decorator'
4
+ require 'tramway/utils/render'
4
5
 
5
6
  module Tramway
6
7
  # Provides decorate function for Tramway projects
7
8
  #
8
9
  class BaseDecorator
9
10
  include Tramway::Decorators::CollectionDecorators
11
+ include Tramway::Utils::Render
10
12
 
11
- attr_reader :object, :context
13
+ attr_reader :object
12
14
 
13
- def initialize(object, context)
15
+ def initialize(object)
14
16
  @object = object
15
- @context = context
16
- end
17
-
18
- def render(*args)
19
- context.render(*args, layout: false)
20
17
  end
21
18
 
22
19
  class << self
23
- def decorate(object_or_array, context)
20
+ def decorate(object_or_array)
24
21
  if Tramway::Decorators::CollectionDecorators.collection?(object_or_array)
25
22
  Tramway::Decorators::CollectionDecorators.decorate_collection(
26
23
  collection: object_or_array,
27
- context:,
28
24
  decorator: self
29
25
  )
30
26
  else
31
- new(object_or_array, context)
27
+ new(object_or_array)
32
28
  end
33
29
  end
34
30
 
@@ -0,0 +1,24 @@
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
+ Tramway::Configs::Entity.new(name: entity)
19
+ end
20
+ end
21
+
22
+ attr_reader :entities
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Configs
5
+ # Tramway is entity based framework
6
+ #
7
+ class Entity
8
+ attr_reader :name
9
+
10
+ def initialize(name:)
11
+ @name = name.to_s
12
+ end
13
+
14
+ def routes
15
+ underscored_name = name.parameterize.pluralize.underscore
16
+
17
+ OpenStruct.new index: Rails.application.routes.url_helpers.public_send("#{underscored_name}_path")
18
+ end
19
+
20
+ def human_name
21
+ OpenStruct.new single: name.capitalize, plural: name.pluralize.capitalize
22
+ end
23
+ end
24
+ end
25
+ end
@@ -7,9 +7,9 @@ module Tramway
7
7
  module CollectionDecorators
8
8
  module_function
9
9
 
10
- def decorate_collection(collection:, context:, decorator:)
10
+ def decorate_collection(collection:, decorator:)
11
11
  collection.map do |item|
12
- decorator.decorate item, context
12
+ decorator.decorate item
13
13
  end
14
14
  end
15
15
 
@@ -10,7 +10,7 @@ module Tramway
10
10
  def tramway_decorate(object_or_array, decorator: nil)
11
11
  return [] if Tramway::Decorators::CollectionDecorators.collection?(object_or_array) && object_or_array.empty?
12
12
 
13
- Tramway::Decorators::ClassHelper.decorator_class(object_or_array, decorator).decorate object_or_array, self
13
+ Tramway::Decorators::ClassHelper.decorator_class(object_or_array, decorator).decorate object_or_array
14
14
  end
15
15
  end
16
16
  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,18 @@ 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
+ entity.to_s.pluralize
55
+
56
+ item entity.human_name.plural, entity.routes.index
57
+ end
58
+
59
+ reset_filling
60
+ end
61
+
46
62
  def provided_url_and_block?(url)
47
63
  url.present? && block_given?
48
64
  end
@@ -57,12 +73,14 @@ module Tramway
57
73
 
58
74
  def render_ignoring_block(text_or_url, url, options)
59
75
  options.merge!(href: url)
60
- @context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
76
+
77
+ context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
61
78
  end
62
79
 
63
80
  def render_using_block(text_or_url, options, &block)
64
81
  options.merge!(href: text_or_url)
65
- @context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
82
+
83
+ context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
66
84
  end
67
85
  end
68
86
  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.2'
4
+ VERSION = '0.2.2'
5
5
  end
data/lib/tramway.rb CHANGED
@@ -3,9 +3,19 @@
3
3
  require 'tramway/version'
4
4
  require 'tramway/engine'
5
5
  require 'tramway/base_decorator'
6
+ require 'tramway/config'
6
7
  require 'view_component/compiler'
7
8
  require 'view_component/engine'
8
9
 
9
10
  # Core module for the whole gem
10
11
  module Tramway
12
+ module_function
13
+
14
+ def configure(&)
15
+ yield config
16
+ end
17
+
18
+ def config
19
+ Tramway::Config.instance
20
+ end
11
21
  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.2
4
+ version: 0.2.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-07-21 00:00:00.000000000 Z
12
+ date: 2023-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml-rails
@@ -88,12 +88,15 @@ files:
88
88
  - lib/tasks/tramway_tasks.rake
89
89
  - lib/tramway.rb
90
90
  - lib/tramway/base_decorator.rb
91
+ - lib/tramway/config.rb
92
+ - lib/tramway/configs/entity.rb
91
93
  - lib/tramway/decorators/class_helper.rb
92
94
  - lib/tramway/decorators/collection_decorator.rb
93
95
  - lib/tramway/engine.rb
94
96
  - lib/tramway/helpers/decorate_helper.rb
95
97
  - lib/tramway/helpers/navbar_helper.rb
96
98
  - lib/tramway/navbar.rb
99
+ - lib/tramway/utils/render.rb
97
100
  - lib/tramway/version.rb
98
101
  homepage: https://github.com/purple-magic/tramway
99
102
  licenses: