Skip to content

Commit b082ba9

Browse files
committed
added namespace for Team
1 parent 79313f7 commit b082ba9

File tree

19 files changed

+72
-68
lines changed

19 files changed

+72
-68
lines changed

DEBUGGING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ heroku run script/console --app=...
2020
2121
Running `script/console` attached to terminal... up, run.7593
2222
23-
2.2.1 > Team.count
23+
2.2.1 > SlackRubyBotServer::Team.count
2424
=> 3
2525
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class MyApp < SlackRubyBotServer::App
8484
private
8585

8686
def deactivate_sleepy_teams!
87-
Team.active.each do |team|
87+
SlackRubyBotServer::Team.active.each do |team|
8888
next unless team.sleepy?
8989
team.deactivate!
9090
end

UPGRADING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class MyApp < SlackRubyBotServer::App
9191
private
9292

9393
def deactivate_sleepy_teams!
94-
Team.active.each do |team|
94+
SlackRubyBotServer::Team.active.each do |team|
9595
next unless team.sleepy?
9696
team.deactivate!
9797
end

lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TeamsEndpoint < Grape::API
1313
requires :id, type: String, desc: 'Team ID.'
1414
end
1515
get ':id' do
16-
team = Team.find(params[:id]) || error!('Not Found', 404)
16+
team = SlackRubyBotServer::Team.find(params[:id]) || error!('Not Found', 404)
1717
present team, with: Presenters::TeamPresenter
1818
end
1919

@@ -22,9 +22,9 @@ class TeamsEndpoint < Grape::API
2222
optional :active, type: Boolean, desc: 'Return active teams only.'
2323
use :pagination
2424
end
25-
sort Team::SORT_ORDERS
25+
sort SlackRubyBotServer::Team::SORT_ORDERS
2626
get do
27-
teams = Team.all
27+
teams = SlackRubyBotServer::Team.all
2828
teams = teams.active if params[:active]
2929
teams = paginate_and_sort_by_cursor(teams, default_sort_order: '-id')
3030
present teams, with: Presenters::TeamsPresenter
@@ -46,14 +46,14 @@ class TeamsEndpoint < Grape::API
4646
)
4747

4848
token = rc['bot']['bot_access_token']
49-
team = Team.where(token: token).first
50-
team ||= Team.where(team_id: rc['team_id']).first
49+
team = SlackRubyBotServer::Team.where(token: token).first
50+
team ||= SlackRubyBotServer::Team.where(team_id: rc['team_id']).first
5151
if team && !team.active?
5252
team.activate!(token)
5353
elsif team
5454
raise "Team #{team.name} is already registered."
5555
else
56-
team = Team.create!(
56+
team = SlackRubyBotServer::Team.create!(
5757
token: token,
5858
team_id: rc['team_id'],
5959
name: rc['team_name']

lib/slack-ruby-bot-server/api/presenters/status_presenter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ module StatusPresenter
1616

1717
def ping
1818
if SlackRubyBotServer::Config.mongoid?
19-
team = Team.asc(:_id).first
19+
team = SlackRubyBotServer::Team.asc(:_id).first
2020
elsif SlackRubyBotServer::Config.activerecord?
21-
team = Team.last
21+
team = SlackRubyBotServer::Team.last
2222
else
2323
raise 'Unsupported database driver.'
2424
end
@@ -27,11 +27,11 @@ def ping
2727
end
2828

2929
def teams_count
30-
Team.count
30+
SlackRubyBotServer::Team.count
3131
end
3232

3333
def active_teams_count
34-
Team.active.count
34+
SlackRubyBotServer::Team.active.count
3535
end
3636

3737
def base_url(opts)

lib/slack-ruby-bot-server/app.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def init_database!
3232
end
3333

3434
def mark_teams_active!
35-
Team.where(active: nil).update_all(active: true)
35+
SlackRubyBotServer::Team.where(active: nil).update_all(active: true)
3636
end
3737

3838
def update_team_name_and_id!
39-
Team.active.where(team_id: nil).each do |team|
39+
SlackRubyBotServer::Team.active.where(team_id: nil).each do |team|
4040
begin
4141
auth = team.ping![:auth]
4242
team.update_attributes!(team_id: auth['team_id'], name: auth['team'])
@@ -50,13 +50,13 @@ def update_team_name_and_id!
5050
def migrate_from_single_team!
5151
return unless ENV.key?('SLACK_API_TOKEN')
5252
logger.info 'Migrating from env SLACK_API_TOKEN ...'
53-
team = Team.find_or_create_from_env!
53+
team = SlackRubyBotServer::Team.find_or_create_from_env!
5454
logger.info "Automatically migrated team: #{team}."
5555
logger.warn "You should unset ENV['SLACK_API_TOKEN']."
5656
end
5757

5858
def purge_inactive_teams!
59-
Team.purge!
59+
SlackRubyBotServer::Team.purge!
6060
end
6161

6262
def configure_global_aliases!
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
require_relative 'methods'
22

3-
class Team < ActiveRecord::Base
4-
include Methods
3+
module SlackRubyBotServer
4+
class Team < ActiveRecord::Base
5+
include Methods
56

6-
def self.purge!
7-
# destroy teams inactive for two weeks
8-
Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team|
9-
puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
10-
team.destroy
7+
def self.purge!
8+
# destroy teams inactive for two weeks
9+
SlackRubyBotServer::Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team|
10+
puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
11+
team.destroy
12+
end
1113
end
1214
end
1315
end

lib/slack-ruby-bot-server/models/team/methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def ping!
4242
def self.find_or_create_from_env!
4343
token = ENV['SLACK_API_TOKEN']
4444
return unless token
45-
team = Team.where(token: token).first
46-
team ||= Team.new(token: token)
45+
team = SlackRubyBotServer::Team.where(token: token).first
46+
team ||= SlackRubyBotServer::Team.new(token: token)
4747
info = Slack::Web::Client.new(token: token).team_info
4848
team.team_id = info['team']['id']
4949
team.name = info['team']['name']
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
require_relative 'methods'
22

3-
class Team
4-
include Mongoid::Document
5-
include Mongoid::Timestamps
3+
module SlackRubyBotServer
4+
class Team
5+
include Mongoid::Document
6+
include Mongoid::Timestamps
67

7-
field :team_id, type: String
8-
field :name, type: String
9-
field :domain, type: String
10-
field :token, type: String
11-
field :active, type: Boolean, default: true
8+
field :team_id, type: String
9+
field :name, type: String
10+
field :domain, type: String
11+
field :token, type: String
12+
field :active, type: Boolean, default: true
1213

13-
include Methods
14+
include Methods
1415

15-
def self.purge!
16-
# destroy teams inactive for two weeks
17-
Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team|
18-
Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
19-
team.destroy
16+
def self.purge!
17+
# destroy teams inactive for two weeks
18+
SlackRubyBotServer::Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team|
19+
Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
20+
team.destroy
21+
end
2022
end
2123
end
22-
end
24+
end

lib/slack-ruby-bot-server/rspec/fabricators/team.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Fabricator(:team) do
1+
Fabricator(:team, from: 'SlackRubyBotServer::Team') do
22
token { Fabricate.sequence(:team_token) { |i| "abc-#{i}" } }
33
team_id { Fabricate.sequence(:team_id) { |i| "T#{i}" } }
44
name { Faker::Lorem.word }

0 commit comments

Comments
 (0)