diff --git a/app/models/group.rb b/app/models/group.rb index 8962bc83..060c5b70 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,4 +1,12 @@ class Group < ActiveRecord::Base belongs_to :user - has_many :users + has_many :group_members, dependent: :destroy + + before_validation do + self.name = self.display_name.downcase.sub(/\s+/, '-') + end + + def add_member(user) + GroupMember.create! group: self, user: user + end end diff --git a/app/models/group_member.rb b/app/models/group_member.rb new file mode 100644 index 00000000..3cd7913b --- /dev/null +++ b/app/models/group_member.rb @@ -0,0 +1,4 @@ +class GroupMember < ActiveRecord::Base + has_one :user + has_one :group +end diff --git a/app/models/user.rb b/app/models/user.rb index dd0bb853..931feb6e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,7 +26,7 @@ class User < ActiveRecord::Base has_many :moderation_comments, dependent: :destroy has_many :moderation_votes, dependent: :destroy has_many :groups, dependent: :destroy - belongs_to :group, foreign_key: :target_id + has_many :group_memberships, class_name: "GroupMember", dependent: :destroy SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i diff --git a/db/migrate/20150108121010_create_groups.rb b/db/migrate/20150108121010_create_groups.rb index a66886bd..252ae75b 100644 --- a/db/migrate/20150108121010_create_groups.rb +++ b/db/migrate/20150108121010_create_groups.rb @@ -2,16 +2,26 @@ class CreateGroups < ActiveRecord::Migration def change create_table :groups do |t| t.integer :user_id, null: false - t.integer :target_id, null: false t.string :name t.string :display_name + t.boolean :private, default: true t.timestamps end add_index :groups, :user_id - add_index :groups, :target_id add_index :groups, :name add_index :groups, [:user_id, :name], unique: true + + create_table :group_members do |t| + t.integer :group_id, null: false + t.integer :user_id, null: false + + t.timestamps + end + + add_index :group_members, :group_id + add_index :group_members, :user_id + add_index :group_members, [:group_id, :user_id], unique: true end end diff --git a/db/schema.rb b/db/schema.rb index 860473f8..4a23f1b5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -38,17 +38,27 @@ ActiveRecord::Schema.define(version: 20150108121010) do add_index "comments", ["user_id", "created_at"], name: "index_comments_on_user_id_and_created_at", using: :btree + create_table "group_members", force: true do |t| + t.integer "group_id", null: false + t.integer "user_id", null: false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "group_members", ["group_id", "user_id"], name: "index_group_members_on_group_id_and_user_id", unique: true, using: :btree + add_index "group_members", ["group_id"], name: "index_group_members_on_group_id", using: :btree + add_index "group_members", ["user_id"], name: "index_group_members_on_user_id", using: :btree + create_table "groups", force: true do |t| - t.integer "user_id", null: false - t.integer "target_id", null: false + t.integer "user_id", null: false t.string "name" t.string "display_name" + t.boolean "private", default: true t.datetime "created_at" t.datetime "updated_at" end add_index "groups", ["name"], name: "index_groups_on_name", using: :btree - add_index "groups", ["target_id"], name: "index_groups_on_target_id", using: :btree add_index "groups", ["user_id", "name"], name: "index_groups_on_user_id_and_name", unique: true, using: :btree add_index "groups", ["user_id"], name: "index_groups_on_user_id", using: :btree