Browse Source

Add API token to Group

Maarten van den Berg 6 years ago
parent
commit
6bff0c0dc4

+ 1 - 0
app/models/group.rb

@@ -6,6 +6,7 @@ class Group < ApplicationRecord
6 6
   #   @return [String]
7 7
   #     the name of the group. Must be unique across all groups.
8 8
 
9
+  has_secure_token :api_token
9 10
   has_many :members,
10 11
     dependent: :destroy
11 12
   has_many :people, through: :members

+ 6 - 0
db/migrate/20181104220516_add_api_token_to_groups.rb

@@ -0,0 +1,6 @@
1
+class AddApiTokenToGroups < ActiveRecord::Migration[5.0]
2
+  def change
3
+    add_column :groups, :api_token, :string
4
+    add_index :groups, :api_token, unique: true
5
+  end
6
+end

+ 9 - 0
db/migrate/20181104220610_generate_group_api_tokens.rb

@@ -0,0 +1,9 @@
1
+# Generate an API token for each group after adding the column.
2
+class GenerateGroupApiTokens < ActiveRecord::Migration[5.0]
3
+  def up
4
+    Group.all.each(&:regenerate_api_token)
5
+  end
6
+
7
+  # No-op
8
+  def down; end
9
+end

+ 3 - 1
db/schema.rb

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 20180904163645) do
13
+ActiveRecord::Schema.define(version: 20181104220610) do
14 14
 
15 15
   create_table "activities", force: :cascade do |t|
16 16
     t.string   "name"
@@ -58,6 +58,8 @@ ActiveRecord::Schema.define(version: 20180904163645) do
58 58
     t.string   "name"
59 59
     t.datetime "created_at", null: false
60 60
     t.datetime "updated_at", null: false
61
+    t.string   "api_token"
62
+    t.index ["api_token"], name: "index_groups_on_api_token", unique: true
61 63
   end
62 64
 
63 65
   create_table "members", force: :cascade do |t|