Browse Source

Adds JoinedUserActivity model to associate users to every activities they created

Lou 3 years ago
parent
commit
99747e3f2d

+ 4
- 0
app/controllers/api/v1/activities_controller.rb View File

@@ -13,6 +13,10 @@ class Api::V1::ActivitiesController < ApplicationController
13 13
   def create
14 14
     activity = current_user.created_activities.build(activity_params)
15 15
     if activity.save
16
+      JoinedUserActivity.create!(
17
+        user_id: current_user.id,
18
+        activity_id: activity.id
19
+      )
16 20
       render json: activity, status: :created
17 21
     else
18 22
       render json: { errors: activity.errors }, status: :unprocessable_entity

+ 3
- 0
app/models/activity.rb View File

@@ -1,6 +1,9 @@
1 1
 class Activity < ApplicationRecord
2 2
   belongs_to :author, class_name: "User"
3 3
   
4
+  has_one :joined_user_activities
5
+  has_one :user, :through => :joined_user_activities
6
+
4 7
   validates :name, presence: true
5 8
   validates :author, presence: true
6 9
 end

+ 4
- 0
app/models/joined_user_activity.rb View File

@@ -0,0 +1,4 @@
1
+class JoinedUserActivity < ApplicationRecord
2
+  belongs_to :user
3
+  belongs_to :activity
4
+end

+ 4
- 0
app/models/user.rb View File

@@ -5,5 +5,9 @@ class User < ApplicationRecord
5 5
   validates :password_digest, presence: true
6 6
 
7 7
   has_many :created_activities, foreign_key: 'author_id', class_name: 'Activity', dependent: :destroy
8
+
9
+  has_many :joined_user_activities
10
+  has_many :activities, :through => :joined_user_activities
11
+
8 12
   has_secure_password
9 13
 end

+ 10
- 0
db/migrate/20210429184439_create_joined_user_activities.rb View File

@@ -0,0 +1,10 @@
1
+class CreateJoinedUserActivities < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :joined_user_activities do |t|
4
+      t.references :user
5
+      t.references :activity
6
+
7
+      t.timestamps
8
+    end
9
+  end
10
+end

+ 10
- 1
db/schema.rb View File

@@ -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: 2021_04_13_142821) do
13
+ActiveRecord::Schema.define(version: 2021_04_29_184439) do
14 14
 
15 15
   create_table "activities", force: :cascade do |t|
16 16
     t.string "name", null: false
@@ -23,6 +23,15 @@ ActiveRecord::Schema.define(version: 2021_04_13_142821) do
23 23
     t.index ["name"], name: "index_activities_on_name"
24 24
   end
25 25
 
26
+  create_table "joined_user_activities", force: :cascade do |t|
27
+    t.integer "user_id"
28
+    t.integer "activity_id"
29
+    t.datetime "created_at", precision: 6, null: false
30
+    t.datetime "updated_at", precision: 6, null: false
31
+    t.index ["activity_id"], name: "index_joined_user_activities_on_activity_id"
32
+    t.index ["user_id"], name: "index_joined_user_activities_on_user_id"
33
+  end
34
+
26 35
   create_table "users", force: :cascade do |t|
27 36
     t.string "email", null: false
28 37
     t.string "username", null: false

+ 10
- 2
db/seeds.rb View File

@@ -2,11 +2,13 @@ User.delete_all
2 2
 User.reset_pk_sequence
3 3
 Activity.delete_all
4 4
 Activity.reset_pk_sequence
5
+JoinedUserActivity.delete_all
6
+JoinedUserActivity.reset_pk_sequence
5 7
 
6 8
 10.times do |i|
7 9
   name = Faker::Name.first_name.downcase
8 10
   user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
9
-  puts "Created user ##{i} - #{user.username}"
11
+  puts "Created USER ##{i} - #{user.username}"
10 12
   
11 13
   2.times do
12 14
     activity = Activity.create!(
@@ -15,6 +17,12 @@ Activity.reset_pk_sequence
15 17
       description: Faker::Company.catch_phrase,
16 18
       author_id: user.id
17 19
     )
18
-    puts "Created activity \"#{activity.name}\" for #{activity.client}"
20
+    puts "Created ACTIVITY \"#{activity.name}\" for #{activity.client}"
21
+
22
+    joined_user_activity = JoinedUserActivity.create!(
23
+      user_id: user.id,
24
+      activity_id: activity.id
25
+    )
26
+    puts "Created ASSOCIATION ##{joined_user_activity.id}"
19 27
   end
20 28
 end

+ 11
- 0
test/fixtures/joined_user_activities.yml View File

@@ -0,0 +1,11 @@
1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+# This model initially had no columns defined. If you add columns to the
4
+# model remove the '{}' from the fixture names and add the columns immediately
5
+# below each fixture, per the syntax in the comments below
6
+#
7
+one: {}
8
+# column: value
9
+#
10
+two: {}
11
+# column: value

+ 7
- 0
test/models/joined_user_activity_test.rb View File

@@ -0,0 +1,7 @@
1
+require "test_helper"
2
+
3
+class JoinedUserActivityTest < ActiveSupport::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end

Loading…
Cancel
Save