Skip to content

Commit c9e0cd1

Browse files
dblockCopilot
andcommitted
Document custom Team fields and Rails reloading
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7e42e01 commit c9e0cd1

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 2.3.0 (Next)
44

5+
* [#186](https://github.com/slack-ruby/slack-ruby-bot-server/pull/186): Document persisted custom Team attributes for both adapters and Rails concern loading/reloading - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
56
* [#183](https://github.com/slack-ruby/slack-ruby-bot-server/pull/183): Add a `config.server_pages` block to configure page middleware options centrally without changing the installed library's defaults - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
67
* [#184](https://github.com/slack-ruby/slack-ruby-bot-server/pull/184): Add a configurable API application and separately mountable team read/registration endpoints, preserving the default API while supporting custom routes and registration-only applications - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
78
* Your contribution here.

‎README.md‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Build a complete Slack bot service with Slack button integration, in Ruby.
1717
- [Storage](#storage)
1818
- [MongoDB](#mongodb)
1919
- [ActiveRecord](#activerecord)
20+
- [Extending Team](#extending-team)
21+
- [Mongoid Attributes](#mongoid-attributes)
22+
- [Active Record Attributes](#active-record-attributes)
23+
- [Rails Loading and Reloading](#rails-loading-and-reloading)
2024
- [OAuth Version and Scopes](#oauth-version-and-scopes)
2125
- [Slack App](#slack-app)
2226
- [API](#api)
@@ -110,6 +114,76 @@ db_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1'
110114
ActiveRecord::Base.establish_connection(db_config)
111115
```
112116

117+
#### Extending Team
118+
119+
The installation model is the top-level `::Team` class for both adapters, not `SlackRubyBotServer::Models::Team`. Load `slack-ruby-bot-server` before extending it. The model already includes its database adapter and timestamps; do not include them again or declare a different superclass.
120+
121+
##### Mongoid Attributes
122+
123+
Add fields by reopening the existing class in an application file, for example `lib/team_extensions.rb`:
124+
125+
```ruby
126+
require 'slack-ruby-bot-server'
127+
128+
class Team
129+
field :subscription_plan, type: String, default: 'free'
130+
131+
def paid_plan?
132+
subscription_plan == 'paid'
133+
end
134+
end
135+
```
136+
137+
Require that file from your startup code after loading the framework and before serving requests or starting the service. Mongoid does not require a schema migration for the new field.
138+
139+
##### Active Record Attributes
140+
141+
Add a column to the existing `teams` table with an application migration. Use your application's migration version; this example is also compatible with older supported Active Record versions:
142+
143+
```ruby
144+
class AddSubscriptionPlanToTeams < ActiveRecord::Migration[6.0]
145+
def change
146+
add_column :teams, :subscription_plan, :string, default: 'free', null: false
147+
end
148+
end
149+
```
150+
151+
Run the migration before code uses the attribute. Active Record provides its reader and writer from the column; do not use `attr_accessor`, which would bypass persistence. Add methods by reopening `class Team` after loading the framework, as above, but omit the Mongoid-only `field` declaration.
152+
153+
##### Rails Loading and Reloading
154+
155+
Putting another `app/models/team.rb` in a Rails app is not a reliable way to extend a class already loaded by a gem. Put reloadable behavior in a concern instead:
156+
157+
```ruby
158+
# app/models/concerns/team_extensions.rb
159+
module TeamExtensions
160+
extend ActiveSupport::Concern
161+
162+
def paid_plan?
163+
subscription_plan == 'paid'
164+
end
165+
end
166+
```
167+
168+
Include it in `::Team` from a reloader callback, rather than autoloading the concern directly during initializer execution:
169+
170+
```ruby
171+
# config/initializers/team_extensions.rb
172+
require 'slack-ruby-bot-server'
173+
174+
if SlackRubyBotServer.config.mongoid?
175+
::Team.field :subscription_plan, type: String, default: 'free'
176+
end
177+
178+
Rails.application.config.to_prepare do
179+
::Team.include(TeamExtensions)
180+
end
181+
```
182+
183+
With Active Record, use the migration above instead of declaring a field in the initializer. The callback runs at boot and after development reloads; including the same concern more than once is safe. Keep one-time field declarations outside the callback. Do not query the database or swallow database errors just to attach methods: the extension can be loaded before the `teams` table exists.
184+
185+
The framework currently owns the global `Team` constant. An unrelated application model with the same name is a separate [name-collision limitation](https://github.com/slack-ruby/slack-ruby-bot-server/pull/83), not an extension mechanism.
186+
113187
### OAuth Version and Scopes
114188

115189
Configure your app's [OAuth version](https://api.slack.com/authentication/oauth-v2) and [scopes](https://api.slack.com/legacy/oauth-scopes) as needed by your application.

0 commit comments

Comments
 (0)