From b082ba98e290261b65fe5917d37abb4951bf59bc Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Wed, 14 Nov 2018 22:29:36 +0800 Subject: [PATCH 01/12] added namespace for Team --- DEBUGGING.md | 2 +- README.md | 2 +- UPGRADING.md | 2 +- .../api/endpoints/teams_endpoint.rb | 12 +++---- .../api/presenters/status_presenter.rb | 8 ++--- lib/slack-ruby-bot-server/app.rb | 8 ++--- .../models/team/activerecord.rb | 16 ++++++---- .../models/team/methods.rb | 4 +-- .../models/team/mongoid.rb | 32 ++++++++++--------- .../rspec/fabricators/team.rb | 2 +- lib/slack-ruby-bot-server/service.rb | 2 +- spec/api/endpoints/status_endpoint_spec.rb | 2 +- spec/api/endpoints/teams_endpoint_spec.rb | 14 ++++---- spec/integration/teams_spec.rb | 4 +-- spec/models/team_spec.rb | 20 ++++++------ spec/slack-ruby-bot-server/app_spec.rb | 2 +- spec/slack-ruby-bot-server/ping_spec.rb | 2 +- spec/slack-ruby-bot-server/server_spec.rb | 2 +- .../endpoints/it_behaves_like_a_cursor_api.rb | 4 +-- 19 files changed, 72 insertions(+), 68 deletions(-) diff --git a/DEBUGGING.md b/DEBUGGING.md index 4e1da53..e75faa1 100644 --- a/DEBUGGING.md +++ b/DEBUGGING.md @@ -20,6 +20,6 @@ heroku run script/console --app=... Running `script/console` attached to terminal... up, run.7593 -2.2.1 > Team.count +2.2.1 > SlackRubyBotServer::Team.count => 3 ``` diff --git a/README.md b/README.md index b4975a0..87f9f6d 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ class MyApp < SlackRubyBotServer::App private def deactivate_sleepy_teams! - Team.active.each do |team| + SlackRubyBotServer::Team.active.each do |team| next unless team.sleepy? team.deactivate! end diff --git a/UPGRADING.md b/UPGRADING.md index 91ff05e..a758b08 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -91,7 +91,7 @@ class MyApp < SlackRubyBotServer::App private def deactivate_sleepy_teams! - Team.active.each do |team| + SlackRubyBotServer::Team.active.each do |team| next unless team.sleepy? team.deactivate! end diff --git a/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb b/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb index b1d6e41..90070aa 100644 --- a/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb +++ b/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb @@ -13,7 +13,7 @@ class TeamsEndpoint < Grape::API requires :id, type: String, desc: 'Team ID.' end get ':id' do - team = Team.find(params[:id]) || error!('Not Found', 404) + team = SlackRubyBotServer::Team.find(params[:id]) || error!('Not Found', 404) present team, with: Presenters::TeamPresenter end @@ -22,9 +22,9 @@ class TeamsEndpoint < Grape::API optional :active, type: Boolean, desc: 'Return active teams only.' use :pagination end - sort Team::SORT_ORDERS + sort SlackRubyBotServer::Team::SORT_ORDERS get do - teams = Team.all + teams = SlackRubyBotServer::Team.all teams = teams.active if params[:active] teams = paginate_and_sort_by_cursor(teams, default_sort_order: '-id') present teams, with: Presenters::TeamsPresenter @@ -46,14 +46,14 @@ class TeamsEndpoint < Grape::API ) token = rc['bot']['bot_access_token'] - team = Team.where(token: token).first - team ||= Team.where(team_id: rc['team_id']).first + team = SlackRubyBotServer::Team.where(token: token).first + team ||= SlackRubyBotServer::Team.where(team_id: rc['team_id']).first if team && !team.active? team.activate!(token) elsif team raise "Team #{team.name} is already registered." else - team = Team.create!( + team = SlackRubyBotServer::Team.create!( token: token, team_id: rc['team_id'], name: rc['team_name'] diff --git a/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb b/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb index fe4651a..f7b05c1 100644 --- a/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb +++ b/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb @@ -16,9 +16,9 @@ module StatusPresenter def ping if SlackRubyBotServer::Config.mongoid? - team = Team.asc(:_id).first + team = SlackRubyBotServer::Team.asc(:_id).first elsif SlackRubyBotServer::Config.activerecord? - team = Team.last + team = SlackRubyBotServer::Team.last else raise 'Unsupported database driver.' end @@ -27,11 +27,11 @@ def ping end def teams_count - Team.count + SlackRubyBotServer::Team.count end def active_teams_count - Team.active.count + SlackRubyBotServer::Team.active.count end def base_url(opts) diff --git a/lib/slack-ruby-bot-server/app.rb b/lib/slack-ruby-bot-server/app.rb index 8891ddb..062746f 100644 --- a/lib/slack-ruby-bot-server/app.rb +++ b/lib/slack-ruby-bot-server/app.rb @@ -32,11 +32,11 @@ def init_database! end def mark_teams_active! - Team.where(active: nil).update_all(active: true) + SlackRubyBotServer::Team.where(active: nil).update_all(active: true) end def update_team_name_and_id! - Team.active.where(team_id: nil).each do |team| + SlackRubyBotServer::Team.active.where(team_id: nil).each do |team| begin auth = team.ping![:auth] team.update_attributes!(team_id: auth['team_id'], name: auth['team']) @@ -50,13 +50,13 @@ def update_team_name_and_id! def migrate_from_single_team! return unless ENV.key?('SLACK_API_TOKEN') logger.info 'Migrating from env SLACK_API_TOKEN ...' - team = Team.find_or_create_from_env! + team = SlackRubyBotServer::Team.find_or_create_from_env! logger.info "Automatically migrated team: #{team}." logger.warn "You should unset ENV['SLACK_API_TOKEN']." end def purge_inactive_teams! - Team.purge! + SlackRubyBotServer::Team.purge! end def configure_global_aliases! diff --git a/lib/slack-ruby-bot-server/models/team/activerecord.rb b/lib/slack-ruby-bot-server/models/team/activerecord.rb index 7e41015..dc49fe8 100644 --- a/lib/slack-ruby-bot-server/models/team/activerecord.rb +++ b/lib/slack-ruby-bot-server/models/team/activerecord.rb @@ -1,13 +1,15 @@ require_relative 'methods' -class Team < ActiveRecord::Base - include Methods +module SlackRubyBotServer + class Team < ActiveRecord::Base + include Methods - def self.purge! - # destroy teams inactive for two weeks - Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team| - puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago." - team.destroy + def self.purge! + # destroy teams inactive for two weeks + SlackRubyBotServer::Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team| + puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago." + team.destroy + end end end end diff --git a/lib/slack-ruby-bot-server/models/team/methods.rb b/lib/slack-ruby-bot-server/models/team/methods.rb index f3679f9..86f0886 100644 --- a/lib/slack-ruby-bot-server/models/team/methods.rb +++ b/lib/slack-ruby-bot-server/models/team/methods.rb @@ -42,8 +42,8 @@ def ping! def self.find_or_create_from_env! token = ENV['SLACK_API_TOKEN'] return unless token - team = Team.where(token: token).first - team ||= Team.new(token: token) + team = SlackRubyBotServer::Team.where(token: token).first + team ||= SlackRubyBotServer::Team.new(token: token) info = Slack::Web::Client.new(token: token).team_info team.team_id = info['team']['id'] team.name = info['team']['name'] diff --git a/lib/slack-ruby-bot-server/models/team/mongoid.rb b/lib/slack-ruby-bot-server/models/team/mongoid.rb index 883032b..8f5c3ce 100644 --- a/lib/slack-ruby-bot-server/models/team/mongoid.rb +++ b/lib/slack-ruby-bot-server/models/team/mongoid.rb @@ -1,22 +1,24 @@ require_relative 'methods' -class Team - include Mongoid::Document - include Mongoid::Timestamps +module SlackRubyBotServer + class Team + include Mongoid::Document + include Mongoid::Timestamps - field :team_id, type: String - field :name, type: String - field :domain, type: String - field :token, type: String - field :active, type: Boolean, default: true + field :team_id, type: String + field :name, type: String + field :domain, type: String + field :token, type: String + field :active, type: Boolean, default: true - include Methods + include Methods - def self.purge! - # destroy teams inactive for two weeks - Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team| - Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago." - team.destroy + def self.purge! + # destroy teams inactive for two weeks + SlackRubyBotServer::Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team| + Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago." + team.destroy + end end end -end +end \ No newline at end of file diff --git a/lib/slack-ruby-bot-server/rspec/fabricators/team.rb b/lib/slack-ruby-bot-server/rspec/fabricators/team.rb index 4262eb9..e2552db 100644 --- a/lib/slack-ruby-bot-server/rspec/fabricators/team.rb +++ b/lib/slack-ruby-bot-server/rspec/fabricators/team.rb @@ -1,4 +1,4 @@ -Fabricator(:team) do +Fabricator(:team, from: 'SlackRubyBotServer::Team') do token { Fabricate.sequence(:team_token) { |i| "abc-#{i}" } } team_id { Fabricate.sequence(:team_id) { |i| "T#{i}" } } name { Faker::Lorem.word } diff --git a/lib/slack-ruby-bot-server/service.rb b/lib/slack-ruby-bot-server/service.rb index 4c94727..6c77c51 100644 --- a/lib/slack-ruby-bot-server/service.rb +++ b/lib/slack-ruby-bot-server/service.rb @@ -60,7 +60,7 @@ def stop!(team) end def start_from_database! - Team.active.each do |team| + SlackRubyBotServer::Team.active.each do |team| run_callbacks :booting, team start!(team) run_callbacks :booted, team diff --git a/spec/api/endpoints/status_endpoint_spec.rb b/spec/api/endpoints/status_endpoint_spec.rb index cb79b63..2ce3169 100644 --- a/spec/api/endpoints/status_endpoint_spec.rb +++ b/spec/api/endpoints/status_endpoint_spec.rb @@ -4,7 +4,7 @@ include SlackRubyBotServer::Api::Test::EndpointTest before do - allow_any_instance_of(Team).to receive(:ping!).and_return(ok: 1) + allow_any_instance_of(SlackRubyBotServer::Team).to receive(:ping!).and_return(ok: 1) end context 'status' do diff --git a/spec/api/endpoints/teams_endpoint_spec.rb b/spec/api/endpoints/teams_endpoint_spec.rb index 79288bc..6d2cfe3 100644 --- a/spec/api/endpoints/teams_endpoint_spec.rb +++ b/spec/api/endpoints/teams_endpoint_spec.rb @@ -3,7 +3,7 @@ describe SlackRubyBotServer::Api::Endpoints::TeamsEndpoint do include SlackRubyBotServer::Api::Test::EndpointTest - it_behaves_like 'a cursor api', Team + it_behaves_like 'a cursor api', SlackRubyBotServer::Team context 'team' do let(:existing_team) { Fabricate(:team) } @@ -68,9 +68,9 @@ team = client.teams._post(code: 'code') expect(team.team_id).to eq 'team_id' expect(team.name).to eq 'team_name' - team = Team.find(team.id) + team = SlackRubyBotServer::Team.find(team.id) expect(team.token).to eq 'token' - end.to change(Team, :count).by(1) + end.to change(SlackRubyBotServer::Team, :count).by(1) end it 'reactivates a deactivated team' do expect(SlackRubyBotServer::Service.instance).to receive(:start!) @@ -80,10 +80,10 @@ expect(team.team_id).to eq existing_team.team_id expect(team.name).to eq existing_team.name expect(team.active).to be true - team = Team.find(team.id) + team = SlackRubyBotServer::Team.find(team.id) expect(team.token).to eq 'token' expect(team.active).to be true - end.to_not change(Team, :count) + end.to_not change(SlackRubyBotServer::Team, :count) end it 'returns a useful error when team already exists' do existing_team = Fabricate(:team, token: 'token') @@ -100,10 +100,10 @@ expect(team.team_id).to eq existing_team.team_id expect(team.name).to eq existing_team.name expect(team.active).to be true - team = Team.find(team.id) + team = SlackRubyBotServer::Team.find(team.id) expect(team.token).to eq 'token' expect(team.active).to be true - end.to_not change(Team, :count) + end.to_not change(SlackRubyBotServer::Team, :count) end end end diff --git a/spec/integration/teams_spec.rb b/spec/integration/teams_spec.rb index 100b997..2e341c1 100644 --- a/spec/integration/teams_spec.rb +++ b/spec/integration/teams_spec.rb @@ -11,14 +11,14 @@ end context 'oauth', vcr: { cassette_name: 'auth_test' } do it 'registers a team' do - allow_any_instance_of(Team).to receive(:ping!).and_return(ok: true) + allow_any_instance_of(SlackRubyBotServer::Team).to receive(:ping!).and_return(ok: true) expect(SlackRubyBotServer::Service.instance).to receive(:start!) oauth_access = { 'bot' => { 'bot_access_token' => 'token' }, 'team_id' => 'team_id', 'team_name' => 'team_name' } allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(hash_including(code: 'code')).and_return(oauth_access) expect do visit '/?code=code' expect(page.find('#messages')).to have_content 'Team successfully registered!' - end.to change(Team, :count).by(1) + end.to change(SlackRubyBotServer::Team, :count).by(1) end end context 'homepage' do diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index 6f8b2a4..4ff75ea 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' -describe Team do +describe SlackRubyBotServer::Team do context '#find_or_create_from_env!' do before do ENV['SLACK_API_TOKEN'] = 'token' end context 'team', vcr: { cassette_name: 'team_info' } do it 'creates a team' do - expect { Team.find_or_create_from_env! }.to change(Team, :count).by(1) - team = Team.first + expect { SlackRubyBotServer::Team.find_or_create_from_env! }.to change(SlackRubyBotServer::Team, :count).by(1) + team = SlackRubyBotServer::Team.first expect(team.team_id).to eq 'T04KB5WQH' expect(team.name).to eq 'dblock' expect(team.domain).to eq 'dblockdotorg' @@ -27,13 +27,13 @@ let!(:inactive_team_a_month_ago) { Fabricate(:team, updated_at: 1.month.ago, active: false) } it 'destroys teams inactive for two weeks' do expect do - Team.purge! - end.to change(Team, :count).by(-2) - expect(Team.where(id: active_team.id).first).to eq active_team - expect(Team.where(id: inactive_team.id).first).to eq inactive_team - expect(Team.where(id: inactive_team_a_week_ago.id).first).to eq inactive_team_a_week_ago - expect(Team.where(id: inactive_team_two_weeks_ago.id).first).to be nil - expect(Team.where(id: inactive_team_a_month_ago.id).first).to be nil + SlackRubyBotServer::Team.purge! + end.to change(SlackRubyBotServer::Team, :count).by(-2) + expect(SlackRubyBotServer::Team.where(id: active_team.id).first).to eq active_team + expect(SlackRubyBotServer::Team.where(id: inactive_team.id).first).to eq inactive_team + expect(SlackRubyBotServer::Team.where(id: inactive_team_a_week_ago.id).first).to eq inactive_team_a_week_ago + expect(SlackRubyBotServer::Team.where(id: inactive_team_two_weeks_ago.id).first).to be nil + expect(SlackRubyBotServer::Team.where(id: inactive_team_a_month_ago.id).first).to be nil end end end diff --git a/spec/slack-ruby-bot-server/app_spec.rb b/spec/slack-ruby-bot-server/app_spec.rb index d1539d1..76aa355 100644 --- a/spec/slack-ruby-bot-server/app_spec.rb +++ b/spec/slack-ruby-bot-server/app_spec.rb @@ -13,7 +13,7 @@ end context '#purge_inactive_teams!' do it 'purges teams' do - expect(Team).to receive(:purge!) + expect(SlackRubyBotServer::Team).to receive(:purge!) subject.send(:purge_inactive_teams!) end end diff --git a/spec/slack-ruby-bot-server/ping_spec.rb b/spec/slack-ruby-bot-server/ping_spec.rb index 34ec4ab..9a34c3f 100644 --- a/spec/slack-ruby-bot-server/ping_spec.rb +++ b/spec/slack-ruby-bot-server/ping_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe SlackRubyBotServer::Ping do - let(:team) { Team.new(token: 'token') } + let(:team) { SlackRubyBotServer::Team.new(token: 'token') } let(:options) { {} } let(:server) { SlackRubyBotServer::Server.new({ team: team }.merge(options)) } let(:client) { server.send(:client) } diff --git a/spec/slack-ruby-bot-server/server_spec.rb b/spec/slack-ruby-bot-server/server_spec.rb index b5518ab..f32a7a2 100644 --- a/spec/slack-ruby-bot-server/server_spec.rb +++ b/spec/slack-ruby-bot-server/server_spec.rb @@ -2,7 +2,7 @@ describe SlackRubyBotServer::Server do let(:logger) { subject.send :logger } - let(:team) { Team.new(token: 'token') } + let(:team) { SlackRubyBotServer::Team.new(token: 'token') } context 'with options' do subject do SlackRubyBotServer::Server.new( diff --git a/spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb b/spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb index 60d2c3e..5effe58 100644 --- a/spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb +++ b/spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb @@ -1,6 +1,6 @@ shared_examples_for 'a cursor api' do |model| - let(:model_s) { model.name.underscore.to_sym } - let(:model_ps) { model.name.underscore.pluralize.to_sym } + let(:model_s) { :team } + let(:model_ps) { :teams } context model.name do let(:cursor_params) { @cursor_params || {} } From 0da7fda605cfe8984f974ac9ef1af1eaa6b6cbdc Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Fri, 16 Nov 2018 22:07:24 +0800 Subject: [PATCH 02/12] Allow customization of ActiveRecord table name --- lib/slack-ruby-bot-server/config.rb | 2 ++ .../config/database_adapters/activerecord.rb | 4 ++-- lib/slack-ruby-bot-server/models/team/activerecord.rb | 2 ++ .../db/migrate/20170307164946_create_teams_table.rb | 2 +- sample_apps/sample_app_activerecord/db/schema.rb | 2 +- spec/database_adapters/activerecord/schema.rb | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/slack-ruby-bot-server/config.rb b/lib/slack-ruby-bot-server/config.rb index 85f487e..d6016d4 100644 --- a/lib/slack-ruby-bot-server/config.rb +++ b/lib/slack-ruby-bot-server/config.rb @@ -5,6 +5,7 @@ module Config attr_accessor :server_class attr_accessor :ping attr_accessor :database_adapter + attr_accessor :teams_table def reset! self.ping = nil @@ -16,6 +17,7 @@ def reset! else raise 'One of "mongoid" or "activerecord" is required.' end + self.teams_table = :teams end def activerecord? diff --git a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb index a760341..d587b5d 100644 --- a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +++ b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb @@ -11,8 +11,8 @@ def self.check! end def self.init! - return if ActiveRecord::Base.connection.tables.include?('teams') - ActiveRecord::Base.connection.create_table :teams do |t| + return if ActiveRecord::Base.connection.tables.include?(SlackRubyBotServer::Config.teams_table) + ActiveRecord::Base.connection.create_table SlackRubyBotServer::Config.teams_table do |t| t.string :team_id t.string :name t.string :domain diff --git a/lib/slack-ruby-bot-server/models/team/activerecord.rb b/lib/slack-ruby-bot-server/models/team/activerecord.rb index dc49fe8..e927722 100644 --- a/lib/slack-ruby-bot-server/models/team/activerecord.rb +++ b/lib/slack-ruby-bot-server/models/team/activerecord.rb @@ -2,6 +2,8 @@ module SlackRubyBotServer class Team < ActiveRecord::Base + self.table_name = SlackRubyBotServer::Config.teams_table + include Methods def self.purge! diff --git a/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb b/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb index a2b45af..16c0c69 100644 --- a/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb +++ b/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb @@ -1,6 +1,6 @@ class CreateTeamsTable < ActiveRecord::Migration[5.0] def change - create_table :teams, force: true do |t| + create_table SlackRubyBotServer::Config.teams_table, force: true do |t| t.string :team_id t.string :name t.boolean :active, default: true diff --git a/sample_apps/sample_app_activerecord/db/schema.rb b/sample_apps/sample_app_activerecord/db/schema.rb index 6d6c44f..61d4ea0 100644 --- a/sample_apps/sample_app_activerecord/db/schema.rb +++ b/sample_apps/sample_app_activerecord/db/schema.rb @@ -14,7 +14,7 @@ # These are extensions that must be enabled in order to support this database enable_extension 'plpgsql' - create_table 'teams', force: :cascade do |t| + create_table SlackRubyBotServer::Config.teams_table, force: :cascade do |t| t.string 'team_id' t.string 'name' t.boolean 'active', default: true diff --git a/spec/database_adapters/activerecord/schema.rb b/spec/database_adapters/activerecord/schema.rb index bf04f0f..2d10314 100644 --- a/spec/database_adapters/activerecord/schema.rb +++ b/spec/database_adapters/activerecord/schema.rb @@ -3,7 +3,7 @@ ActiveRecord::Schema.define do self.verbose = false - create_table :teams, force: true do |t| + create_table SlackRubyBotServer::Config.teams_table, force: true do |t| t.string :team_id t.string :name t.boolean :active, default: true From b3b8e1fbad055aa3c9d3aa124cf6f5d8b22c73b4 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Fri, 16 Nov 2018 22:28:11 +0800 Subject: [PATCH 03/12] modified specs to work with ActiveRecord --- spec/ext/activerecord/{commands_spec.rb => commands.rb} | 0 spec/ext/mongoid/{commands_spec.rb => commands.rb} | 0 spec/spec_helper.rb | 4 ++++ 3 files changed, 4 insertions(+) rename spec/ext/activerecord/{commands_spec.rb => commands.rb} (100%) rename spec/ext/mongoid/{commands_spec.rb => commands.rb} (100%) diff --git a/spec/ext/activerecord/commands_spec.rb b/spec/ext/activerecord/commands.rb similarity index 100% rename from spec/ext/activerecord/commands_spec.rb rename to spec/ext/activerecord/commands.rb diff --git a/spec/ext/mongoid/commands_spec.rb b/spec/ext/mongoid/commands.rb similarity index 100% rename from spec/ext/mongoid/commands_spec.rb rename to spec/ext/mongoid/commands.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8502534..b7736b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,3 +16,7 @@ Dir[File.join(__dir__, 'database_adapters', SlackRubyBotServer::Config.database_adapter.to_s, '**/*.rb')].each do |file| require file end + +Dir[File.join(__dir__, 'ext', SlackRubyBotServer::Config.database_adapter.to_s, '**/*.rb')].each do |file| + require file +end \ No newline at end of file From 0e715442f0a66031d3b4436658a57a2040dcb0b6 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Fri, 16 Nov 2018 22:37:05 +0800 Subject: [PATCH 04/12] fixed console script --- script/console | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/script/console b/script/console index e133500..36016f9 100755 --- a/script/console +++ b/script/console @@ -1,6 +1,8 @@ #!/usr/bin/env ruby -require_relative '../config/environment' +require 'bundler' + +Bundler.require # Usage: script/console # Starts an IRB console with slack-ruby-bot-server loaded. From db9a303cd967d428d311268d323dc6c6d9f3c72d Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Fri, 16 Nov 2018 22:39:52 +0800 Subject: [PATCH 05/12] fixed rubocop offenses --- lib/slack-ruby-bot-server/models/team/mongoid.rb | 2 +- spec/spec_helper.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/slack-ruby-bot-server/models/team/mongoid.rb b/lib/slack-ruby-bot-server/models/team/mongoid.rb index 8f5c3ce..d579426 100644 --- a/lib/slack-ruby-bot-server/models/team/mongoid.rb +++ b/lib/slack-ruby-bot-server/models/team/mongoid.rb @@ -21,4 +21,4 @@ def self.purge! end end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b7736b7..15dec99 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,4 +19,4 @@ Dir[File.join(__dir__, 'ext', SlackRubyBotServer::Config.database_adapter.to_s, '**/*.rb')].each do |file| require file -end \ No newline at end of file +end From 535e50541f1043f3d2f5f53b8b18a4ae0ffdea70 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Sat, 17 Nov 2018 07:24:25 +0800 Subject: [PATCH 06/12] Fixed problem with not recognizing table --- .../config/database_adapters/activerecord.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb index d587b5d..5c75b59 100644 --- a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +++ b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb @@ -11,7 +11,7 @@ def self.check! end def self.init! - return if ActiveRecord::Base.connection.tables.include?(SlackRubyBotServer::Config.teams_table) + return if ActiveRecord::Base.connection.tables.include?(SlackRubyBotServer::Config.teams_table.to_s) ActiveRecord::Base.connection.create_table SlackRubyBotServer::Config.teams_table do |t| t.string :team_id t.string :name From 388c5202fc0065086e7066e59ee3a0efec6e8878 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Sat, 17 Nov 2018 07:57:54 +0800 Subject: [PATCH 07/12] update docs --- CHANGELOG.md | 6 +++++- CONTRIBUTING.md | 35 +++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2064c35..7e2bcb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,13 @@ ### Changelog -#### 0.8.3 (Next) +#### 0.8.4 (Next) * Your contribution here. +#### 0.8.3 (2018/11/17) + +* [#83](https://github.com/slack-ruby/slack-ruby-bot-server/pull/83): Prevents name collision for Teams, allow configuration of ActiveRecord table name - [@leechou](https://github.com/leechou). + #### 0.8.2 (2018/10/11) * [#80](https://github.com/slack-ruby/slack-ruby-bot-server/pull/80): Fix: closed stream when closing connection in ping worker - [@dblock](https://github.com/dblock). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 09cc534..0debd14 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,7 +10,7 @@ In the examples below, substitute your Github username for `contributor` in URLs Fork the [project on Github](https://github.com/slack-ruby/slack-ruby-bot-server) and check out your copy. -``` +```bash git clone https://github.com/contributor/slack-ruby-bot-server.git cd slack-ruby-bot-server git remote add upstream https://github.com/slack-ruby/slack-ruby-bot-server.git @@ -20,7 +20,7 @@ git remote add upstream https://github.com/slack-ruby/slack-ruby-bot-server.git Make sure your fork is up-to-date and create a topic branch for your feature or bug fix. -``` +```bash git checkout master git pull upstream master git checkout -b my-feature-branch @@ -30,11 +30,30 @@ git checkout -b my-feature-branch Ensure that you can build the project and run tests. -``` +```bash +export DATABASE_ADAPTER='activerecord' bundle install bundle exec rake ``` +## Run Tests + +Run tests before making changes and then run tests after major commits. +Make sure to test both ActiveRecord and Mongoid. + +```bash +export DATABASE_ADAPTER='activerecord' +bundle install +bundle exec rspec +``` + +Test the sample apps + +```bash +cd sample_apps/sample_app_active_record +ln -s ../../lib . +``` + ## Write Tests Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. @@ -64,21 +83,21 @@ Make it look like every other line, including your name and link to your Github Make sure git knows your name and email address: -``` +```bash git config --global user.name "Your Name" git config --global user.email "contributor@example.com" ``` Writing good commit logs is important. A commit log should describe what changed and why. -``` +```bash git add ... git commit ``` ## Push -``` +```bash git push origin my-feature-branch ``` @@ -91,7 +110,7 @@ Click the 'Pull Request' button and fill out the form. Pull requests are usually If you've been working on a change for a while, rebase with upstream/master. -``` +```bash git fetch upstream git rebase upstream/master git push origin my-feature-branch -f @@ -107,7 +126,7 @@ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical ent Amend your previous commit and force push the changes. -``` +```bash git commit --amend git push origin my-feature-branch -f ``` From 57040ad8046b2c9866a7a0f56b5dcb9855145c2a Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Wed, 21 Nov 2018 19:54:24 +0800 Subject: [PATCH 08/12] changed the variable that saves the table name to Config.teams[:name] --- lib/slack-ruby-bot-server/config.rb | 4 ++-- .../config/database_adapters/activerecord.rb | 4 ++-- lib/slack-ruby-bot-server/models/team/activerecord.rb | 2 +- spec/database_adapters/activerecord/schema.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/slack-ruby-bot-server/config.rb b/lib/slack-ruby-bot-server/config.rb index d6016d4..075944d 100644 --- a/lib/slack-ruby-bot-server/config.rb +++ b/lib/slack-ruby-bot-server/config.rb @@ -5,7 +5,7 @@ module Config attr_accessor :server_class attr_accessor :ping attr_accessor :database_adapter - attr_accessor :teams_table + attr_accessor :teams def reset! self.ping = nil @@ -17,7 +17,7 @@ def reset! else raise 'One of "mongoid" or "activerecord" is required.' end - self.teams_table = :teams + self.teams = {name:"teams"} # the ActiveRecord table name end def activerecord? diff --git a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb index 5c75b59..866eb01 100644 --- a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +++ b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb @@ -11,8 +11,8 @@ def self.check! end def self.init! - return if ActiveRecord::Base.connection.tables.include?(SlackRubyBotServer::Config.teams_table.to_s) - ActiveRecord::Base.connection.create_table SlackRubyBotServer::Config.teams_table do |t| + return if ActiveRecord::Base.connection.tables.include?(SlackRubyBotServer::Config.teams[:name].to_s) + ActiveRecord::Base.connection.create_table SlackRubyBotServer::Config.teams[:name] do |t| t.string :team_id t.string :name t.string :domain diff --git a/lib/slack-ruby-bot-server/models/team/activerecord.rb b/lib/slack-ruby-bot-server/models/team/activerecord.rb index e927722..cf85b29 100644 --- a/lib/slack-ruby-bot-server/models/team/activerecord.rb +++ b/lib/slack-ruby-bot-server/models/team/activerecord.rb @@ -2,7 +2,7 @@ module SlackRubyBotServer class Team < ActiveRecord::Base - self.table_name = SlackRubyBotServer::Config.teams_table + self.table_name = SlackRubyBotServer::Config.teams[:name] include Methods diff --git a/spec/database_adapters/activerecord/schema.rb b/spec/database_adapters/activerecord/schema.rb index 2d10314..20c0322 100644 --- a/spec/database_adapters/activerecord/schema.rb +++ b/spec/database_adapters/activerecord/schema.rb @@ -3,7 +3,7 @@ ActiveRecord::Schema.define do self.verbose = false - create_table SlackRubyBotServer::Config.teams_table, force: true do |t| + create_table SlackRubyBotServer::Config.teams[:name], force: true do |t| t.string :team_id t.string :name t.boolean :active, default: true From 77d8ce580a5a66ceabcff5d0ed1f6ae58af8e0d8 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Wed, 21 Nov 2018 19:54:37 +0800 Subject: [PATCH 09/12] updated docs --- CHANGELOG.md | 7 ++----- CONTRIBUTING.md | 9 ++------- UPGRADING.md | 4 ++++ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2bcb4..423409a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,9 @@ ### Changelog -#### 0.8.4 (Next) - -* Your contribution here. - -#### 0.8.3 (2018/11/17) +#### 0.8.3 (Next) * [#83](https://github.com/slack-ruby/slack-ruby-bot-server/pull/83): Prevents name collision for Teams, allow configuration of ActiveRecord table name - [@leechou](https://github.com/leechou). +* Your contribution here. #### 0.8.2 (2018/10/11) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0debd14..a1e85c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,7 @@ git checkout -b my-feature-branch Ensure that you can build the project and run tests. ```bash -export DATABASE_ADAPTER='activerecord' +export DATABASE_ADAPTER=activerecord bundle install bundle exec rake ``` @@ -42,18 +42,13 @@ Run tests before making changes and then run tests after major commits. Make sure to test both ActiveRecord and Mongoid. ```bash -export DATABASE_ADAPTER='activerecord' +export DATABASE_ADAPTER=activerecord bundle install bundle exec rspec ``` Test the sample apps -```bash -cd sample_apps/sample_app_active_record -ln -s ../../lib . -``` - ## Write Tests Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. diff --git a/UPGRADING.md b/UPGRADING.md index a758b08..8f84822 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,10 @@ Upgrading Slack-Ruby-Bot-Server =============================== +### Upgrading to >= 0.8.3 + +To avoid name collisions, `Team` has been renamed `SlackRubyBot::Team`, if you modify the class `Team` add `Team = SlackRubyBot::Team` before the code. + ### Upgrading to >= 0.8.0 ### Different Asynchronous I/O Library From c59cc1f8a7c139375762c346aeb9301f602967bb Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Wed, 21 Nov 2018 20:00:31 +0800 Subject: [PATCH 10/12] missed some changes --- .../db/migrate/20170307164946_create_teams_table.rb | 2 +- sample_apps/sample_app_activerecord/db/schema.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb b/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb index 16c0c69..c5c6ce9 100644 --- a/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb +++ b/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb @@ -1,6 +1,6 @@ class CreateTeamsTable < ActiveRecord::Migration[5.0] def change - create_table SlackRubyBotServer::Config.teams_table, force: true do |t| + create_table SlackRubyBotServer::Config.teams[:name], force: true do |t| t.string :team_id t.string :name t.boolean :active, default: true diff --git a/sample_apps/sample_app_activerecord/db/schema.rb b/sample_apps/sample_app_activerecord/db/schema.rb index 61d4ea0..fdbc234 100644 --- a/sample_apps/sample_app_activerecord/db/schema.rb +++ b/sample_apps/sample_app_activerecord/db/schema.rb @@ -14,7 +14,7 @@ # These are extensions that must be enabled in order to support this database enable_extension 'plpgsql' - create_table SlackRubyBotServer::Config.teams_table, force: :cascade do |t| + create_table SlackRubyBotServer::Config.teams[:name], force: :cascade do |t| t.string 'team_id' t.string 'name' t.boolean 'active', default: true From 08af36b3d6663fa7201bcb77054cd1191f8c0d80 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Wed, 21 Nov 2018 20:03:13 +0800 Subject: [PATCH 11/12] fixed rubocop offenses --- lib/slack-ruby-bot-server/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack-ruby-bot-server/config.rb b/lib/slack-ruby-bot-server/config.rb index 075944d..b311169 100644 --- a/lib/slack-ruby-bot-server/config.rb +++ b/lib/slack-ruby-bot-server/config.rb @@ -17,7 +17,7 @@ def reset! else raise 'One of "mongoid" or "activerecord" is required.' end - self.teams = {name:"teams"} # the ActiveRecord table name + self.teams = { name: 'teams' } # the ActiveRecord table name end def activerecord? From b765e9edc3aa005f257e5fa28b94e2fa0528fc38 Mon Sep 17 00:00:00 2001 From: Lee Chou Date: Fri, 23 Nov 2018 09:47:05 +0800 Subject: [PATCH 12/12] Fixed docs --- UPGRADING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADING.md b/UPGRADING.md index 8f84822..f4c7fba 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -3,7 +3,7 @@ Upgrading Slack-Ruby-Bot-Server ### Upgrading to >= 0.8.3 -To avoid name collisions, `Team` has been renamed `SlackRubyBot::Team`, if you modify the class `Team` add `Team = SlackRubyBot::Team` before the code. +To avoid name collisions, `Team` has been renamed `SlackRubyBotServer::Team`, if you modify the class `Team` add `Team = SlackRubyBotServer::Team` before the code. ### Upgrading to >= 0.8.0