simple_chat 0.1.0

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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +178 -0
  4. data/Rakefile +6 -0
  5. data/app/assets/stylesheets/simple_chat/application.css +726 -0
  6. data/app/assets/stylesheets/simple_chat/application.tailwind.css +1 -0
  7. data/app/controllers/simple_chat/application_controller.rb +16 -0
  8. data/app/controllers/simple_chat/chat_members_controller.rb +27 -0
  9. data/app/controllers/simple_chat/chat_rooms_controller.rb +74 -0
  10. data/app/controllers/simple_chat/messages_controller.rb +77 -0
  11. data/app/helpers/simple_chat/application_helper.rb +4 -0
  12. data/app/helpers/simple_chat/chat_rooms_helper.rb +4 -0
  13. data/app/helpers/simple_chat/messages_helper.rb +4 -0
  14. data/app/jobs/simple_chat/application_job.rb +4 -0
  15. data/app/mailers/simple_chat/application_mailer.rb +6 -0
  16. data/app/models/simple_chat/application_record.rb +5 -0
  17. data/app/models/simple_chat/chat_member.rb +6 -0
  18. data/app/models/simple_chat/chat_room.rb +22 -0
  19. data/app/models/simple_chat/message.rb +8 -0
  20. data/app/views/layouts/simple_chat/application.html.erb +24 -0
  21. data/app/views/simple_chat/chat_rooms/_chat_room.html.erb +7 -0
  22. data/app/views/simple_chat/chat_rooms/_form.html.erb +22 -0
  23. data/app/views/simple_chat/chat_rooms/edit.html.erb +12 -0
  24. data/app/views/simple_chat/chat_rooms/index.html.erb +14 -0
  25. data/app/views/simple_chat/chat_rooms/new.html.erb +11 -0
  26. data/app/views/simple_chat/chat_rooms/show.html.erb +80 -0
  27. data/app/views/simple_chat/messages/_form.html.erb +28 -0
  28. data/app/views/simple_chat/messages/_message.html.erb +28 -0
  29. data/app/views/simple_chat/messages/create.turbo_stream.erb +10 -0
  30. data/app/views/simple_chat/messages/edit.html.erb +12 -0
  31. data/app/views/simple_chat/messages/index.html.erb +14 -0
  32. data/app/views/simple_chat/messages/new.html.erb +11 -0
  33. data/app/views/simple_chat/messages/show.html.erb +8 -0
  34. data/config/routes.rb +7 -0
  35. data/config/tailwind.config.js +15 -0
  36. data/db/migrate/20260406070146_create_simple_chat_chat_rooms.rb +9 -0
  37. data/db/migrate/20260406080846_create_simple_chat_chat_members.rb +11 -0
  38. data/db/migrate/20260406081003_create_simple_chat_messages.rb +12 -0
  39. data/db/migrate/20260503095220_add_channel_hash_to_solid_cable_messages.rb +6 -0
  40. data/lib/generators/simple_chat/install_generator.rb +21 -0
  41. data/lib/simple_chat/configuration.rb +16 -0
  42. data/lib/simple_chat/engine.rb +13 -0
  43. data/lib/simple_chat/version.rb +3 -0
  44. data/lib/simple_chat.rb +42 -0
  45. data/lib/tasks/simple_chat_tasks.rake +40 -0
  46. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d3fa5a120911616d3139fa0da969eef52af9f61bb44d30509c261be204ea4d97
4
+ data.tar.gz: d22d02acf74b58304075280710421b5912125f9395e5725305436ae793b5e5d9
5
+ SHA512:
6
+ metadata.gz: 831c8b3dc9b0da1e60e0a6fb3897552642ea52a38be3fadb1062b62ed1a713a7919cb9036e950891e3c12aed0f624f7a6a7cedd29655e005a3ff5b53fab93c9e
7
+ data.tar.gz: e076aa779fc16a29002822713e949368edced4fcb9a0271090d3df1d83e76dc4f506da6b6cc8a3ef781ce1bef5b72252ae09a815169108c92b4a905a5043c503
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright YiSheng, Lee
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # SimpleChat
2
+
3
+ SimpleChat is a Rails engine that allows you to easily add a chat system to your Rails application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "simple_chat"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install simple_chat
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### 1. Run the generator
28
+
29
+ Run the install generator to create the configuration file:
30
+
31
+ ```bash
32
+ rails generate simple_chat:install
33
+ ```
34
+
35
+ This will create `config/initializers/simple_chat.rb`. You can configure your user model and the method to fetch the current user there.
36
+
37
+ ```ruby
38
+ SimpleChat.configure do |config|
39
+ # Set the user model (defaults to 'User')
40
+ config.chat_user_model = 'User'
41
+
42
+ # Set the method to get the current user in the controller (defaults to :current_user)
43
+ # This is compatible with Devise.
44
+ config.current_user_method = :current_user
45
+ end
46
+ ```
47
+
48
+ ### 2. Run migrations
49
+
50
+ Copy and run the migrations:
51
+
52
+ ```bash
53
+ rails simple_chat:install:migrations
54
+ rails db:migrate
55
+ ```
56
+
57
+ ### 3. Mount the engine
58
+
59
+ Add the following to your `config/routes.rb` file:
60
+
61
+ ```ruby
62
+ mount SimpleChat::Engine => "/chat"
63
+ ```
64
+
65
+ Now you can access the chat system at `/chat`.
66
+
67
+ ### 4. Membership
68
+
69
+ By default, `SimpleChat` requires users to be members of a chat room before they can post messages.
70
+ - **Visibility**: On the chat rooms index page, users only see rooms that they are members of.
71
+ - **Joining**: When a user creates a new chat room, they are automatically added as a member. For other rooms, users must be added as members (e.g., via console or custom logic) to see them in their list and to access them. Users who are not members will be redirected if they try to access a chat room's page directly.
72
+ - **Membership Management**: Membership is managed via the `simple_chat_chat_members` table.
73
+
74
+ ### 5. Setup Test Users (Optional)
75
+
76
+ For testing purposes, you can quickly set up a `User` model and table with some seed data.
77
+
78
+ If you are running this within the **gem's development environment** (e.g., in the dummy app), run:
79
+
80
+ ```bash
81
+ bin/rails app:simple_chat:setup_test_user
82
+ ```
83
+
84
+ If you are running this in a **host Rails application** that has the gem installed, run:
85
+
86
+ ```bash
87
+ rails simple_chat:setup_test_user
88
+ ```
89
+
90
+ This will:
91
+ - Create a migration for the `users` table with a `name` column (if it doesn't exist).
92
+ - Run the migration.
93
+ - Create a `User` model in `app/models/user.rb` (if it doesn't exist).
94
+ - Seed 5 test user records.
95
+
96
+ ### 6. Programmatic Usage
97
+
98
+ You can create a chat room between multiple users directly from your Rails app:
99
+
100
+ ```ruby
101
+ user1 = User.find(1)
102
+ user2 = User.find(2)
103
+ user3 = User.find(3)
104
+
105
+ # Create a chat with multiple users as arguments
106
+ chat_room = SimpleChat.create_chat(user1, user2, user3)
107
+
108
+ # Or pass them as an array
109
+ chat_room = SimpleChat.create_chat([user1, user2])
110
+
111
+ # Or specify a custom title
112
+ chat_room = SimpleChat.create_chat(user1, user2, title: "Support Chat")
113
+ ```
114
+
115
+ ### 7. Real-Time Messaging Setup
116
+
117
+ To enable real-time messaging, `SimpleChat` uses **Turbo Streams** and **ActionCable** with **Solid Cable** as the backend adapter.
118
+
119
+ #### ActionCable & Solid Cable Configuration
120
+ In your host application, configure `config/cable.yml` to use the `solid_cable` adapter for the environments where you want real-time features enabled:
121
+
122
+ ```yaml
123
+ development:
124
+ adapter: solid_cable
125
+
126
+ production:
127
+ adapter: solid_cable
128
+ ```
129
+
130
+ Ensure you have run the migrations to create the `solid_cable_messages` table:
131
+
132
+ ```bash
133
+ rails db:migrate
134
+ ```
135
+
136
+ #### Frontend Setup (Importmaps)
137
+ The real-time updates require **Turbo** and **ActionCable** to be active in the browser. If you are using Importmaps, ensure the following are pinned in `config/importmap.rb`:
138
+
139
+ ```ruby
140
+ pin "@hotwired/turbo-rails", to: "turbo.min.js"
141
+ pin "@rails/actioncable", to: "actioncable.esm.js"
142
+ ```
143
+
144
+ And imported in your `app/javascript/application.js`:
145
+
146
+ ```javascript
147
+ import "@hotwired/turbo-rails"
148
+ import "@rails/actioncable"
149
+ ```
150
+
151
+ Your main layout (`app/views/layouts/application.html.erb`) must also include the necessary tags in the `<head>`:
152
+
153
+ ```erb
154
+ <%= csrf_meta_tags %>
155
+ <%= csp_meta_tag %>
156
+ <%= javascript_importmap_tags %>
157
+ ```
158
+
159
+ #### Background Job Processing
160
+ The gem uses `broadcast_append_later_to` to prevent UI lag. This enqueues an `ActiveJob` to handle the broadcast. Ensure you have a queue adapter configured (like `async` for development or `solid_queue` for production) and that a worker process is running to handle these jobs.
161
+
162
+ ### 8. CSS & Styling
163
+
164
+ SimpleChat comes with its own pre-compiled Tailwind CSS. This means **you do not need to install or configure Tailwind CSS in your host application** to use SimpleChat.
165
+
166
+ The engine's layout automatically includes the necessary styles. If you are not using the engine's layout and want to include the styles in your own views, you can add:
167
+
168
+ ```erb
169
+ <%= stylesheet_link_tag "simple_chat/application" %>
170
+ ```
171
+
172
+ ## Contributing
173
+
174
+ Contribution directions go here.
175
+
176
+ ## License
177
+
178
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"