Skip to content

Commit 2435ae2

Browse files
committed
Component Registry
1 parent bc3736d commit 2435ae2

File tree

1 file changed

+104
-48
lines changed

1 file changed

+104
-48
lines changed

docs/getting-started/quick-start.md

Lines changed: 104 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ Wow! We just had to copy and paste a JavaScript snippet once in order to integra
755755

756756
We will take a short break before adding the next cool reactivity feature and refactor a little bit! Matestack encourages you to create a readable and maintainable UI implemetation. Therefore we will move some complexity from the current index page to a self contained Matestack component!
757757

758-
## Create a Matestack component
758+
## Create a Matestack Component
759759

760760
* [x] Create a components folder within the matestack folder
761761

@@ -765,6 +765,7 @@ touch app/matestack/components/post.rb
765765
```
766766

767767
* [x] Move code from the index page to the new component
768+
* [x] adjust references to the given post parameter to be called as a method of the context object (`context.post.id`)
768769

769770
`app/matestack/components/post.rb`
770771

@@ -775,16 +776,16 @@ class Components::Post < Matestack::Ui::Component
775776

776777
def response
777778
# copied from the index page
778-
async rerender_on: "cable__liked_post_#{post.id}", id: "post-#{post.id}" do
779+
async rerender_on: "cable__liked_post_#{context.post.id}", id: "post-#{context.post.id}" do
779780
div class: "mb-3 p-3 rounded shadow-sm" do
780781
heading size: 5 do
781-
plain post.username
782-
small text: post.created_at.strftime("%d.%m.%Y %H:%M")
782+
plain context.post.username
783+
small text: context.post.created_at.strftime("%d.%m.%Y %H:%M")
783784
end
784-
paragraph text: post.body, class: "mb-5"
785-
action path: like_post_path(post), method: :put do
785+
paragraph text: context.post.body, class: "mb-5"
786+
action path: like_post_path(context.post), method: :put do
786787
button class: "btn btn-light" do
787-
plain "Like (#{post.likes_count})"
788+
plain "Like (#{context.post.likes_count})"
788789
end
789790
end
790791
end
@@ -794,43 +795,109 @@ class Components::Post < Matestack::Ui::Component
794795
end
795796
```
796797

797-
* [x] Create a component registry file
798798

799-
```bash
800-
touch app/matestack/components/registry.rb
799+
* [x] Adjust the index page in order to use the new component
800+
801+
`app/matestack/twitter_clone/posts/index.rb`
802+
803+
```ruby
804+
class TwitterClone::Pages::Posts::Index < Matestack::Ui::Page
805+
806+
def prepare
807+
@posts = Post.all
808+
end
809+
810+
def response
811+
post_form_partial
812+
post_list_partial
813+
end
814+
815+
private
816+
817+
# ...
818+
819+
def post_list_partial
820+
async rerender_on: "submitted", id: "post-list" do
821+
@posts.each do |post|
822+
# post_partial(post)
823+
Components::Post.(post: post)
824+
end
825+
end
826+
end
827+
828+
# def post_partial post
829+
# async rerender_on: "cable__liked_post_#{post.id}", id: "post-#{post.id}" do
830+
# div class: "mb-3 p-3 rounded shadow-sm" do
831+
# heading size: 5 do
832+
# plain post.username
833+
# small text: post.created_at.strftime("%d.%m.%Y %H:%M")
834+
# end
835+
# paragraph text: post.body, class: "mb-5"
836+
# action path: like_post_path(post), method: :put do
837+
# button class: "btn btn-light" do
838+
# plain "Like (#{post.likes_count})"
839+
# end
840+
# end
841+
# end
842+
# end
843+
# end
844+
845+
end
801846
```
802847

803-
* [x] Register the new component
848+
**Test the current state**
849+
850+
* [x] Navigate to `localhost:3000/posts`
851+
852+
Everything should be the same! We just refactored some code in order to better manage complexity.
804853

805-
`app/matestack/components/registry.rb`
854+
855+
## Component Registry
856+
857+
Components can be invoked as we have done above (`Components::Post.(post: post)`). But sometimes the namespace can get a little long and in the interest of keeping our code beautiful, we can register our components so we can call them like:
806858

807859
```ruby
808-
module Components::Registry
860+
# ...
809861

810-
Matestack::Ui::Core::Component::Registry.register_components(
811-
post_component: Components::Post,
812-
)
862+
def post_list_partial
863+
async rerender_on: "submitted", id: "post-list" do
864+
@posts.each do |post|
865+
# post_partial(post)
866+
post_component post: post
867+
end
868+
end
869+
end
813870

814-
end
871+
# ...
815872
```
816873

817-
* [x] Include the component registry in your application controller
874+
Let's refactor and set up a component registry and register our component.
818875

819-
`app/controllers/application_controller.rb`
876+
* [x] Create a component registry file
877+
878+
```bash
879+
touch app/matestack/components/registry.rb
880+
```
881+
882+
* [x] Register the new component
883+
884+
`app/matestack/components/registry.rb`
820885

821886
```ruby
822-
class ApplicationController < ActionController::Base
887+
module Components::Registry
823888

824-
include Matestack::Ui::Core::ApplicationHelper
825-
include Components::Registry
889+
def post_component(post)
890+
Components::Post.(post: post)
891+
end
826892

827893
end
828894
```
829895

830-
* [x] Adjust the index page in order to use the new component
896+
* [x] Adjust the index page in order to use the component in the new way
831897

832898
`app/matestack/twitter_clone/posts/index.rb`
833899

900+
834901
```ruby
835902
class TwitterClone::Pages::Posts::Index < Matestack::Ui::Page
836903

@@ -845,42 +912,31 @@ class TwitterClone::Pages::Posts::Index < Matestack::Ui::Page
845912

846913
private
847914

848-
# ...
915+
# ...
849916

850-
def post_list_partial
851-
async rerender_on: "submitted", id: "post-list" do
852-
@posts.each do |post|
853-
# post_partial(post)
854-
post_component post: post
855-
end
917+
def post_list_partial
918+
async rerender_on: "submitted", id: "post-list" do
919+
@posts.each do |post|
920+
# post_partial(post)
921+
post_component post: post
856922
end
857923
end
924+
end
858925

859-
# def post_partial post
860-
# async rerender_on: "cable__liked_post_#{post.id}", id: "post-#{post.id}" do
861-
# div class: "mb-3 p-3 rounded shadow-sm" do
862-
# heading size: 5 do
863-
# plain post.username
864-
# small text: post.created_at.strftime("%d.%m.%Y %H:%M")
865-
# end
866-
# paragraph text: post.body, class: "mb-5"
867-
# action path: like_post_path(post), method: :put do
868-
# button class: "btn btn-light" do
869-
# plain "Like (#{post.likes_count})"
870-
# end
871-
# end
872-
# end
873-
# end
874-
# end
926+
# ...
875927

876928
end
877929
```
878930

879-
**Test the current state**
931+
932+
**Test the current state again**
880933

881934
* [x] Navigate to `localhost:3000/posts`
882935

883-
Everything should be the same! We just refactored some code in order to better manage complexity.
936+
Everything should be the same after this small refactoring.
937+
938+
939+
884940

885941
Now we will cover the last topic of this guide:
886942

0 commit comments

Comments
 (0)