소스 검색

model: changement majeur des données de Task

Lou 3 년 전
부모
커밋
eb92b35d19

+ 3
- 3
app/controllers/api/v1/tasks_controller.rb 파일 보기

@@ -22,7 +22,7 @@ class Api::V1::TasksController < ApplicationController
22 22
       render json: TaskSerializer.new(task).serializable_hash.to_json,
23 23
       status: :created
24 24
     else
25
-      render json: { errors: activity.errors }, status: :unprocessable_entity
25
+      render json: { errors: task.errors }, status: :unprocessable_entity
26 26
     end
27 27
   end
28 28
 
@@ -42,7 +42,7 @@ class Api::V1::TasksController < ApplicationController
42 42
   private
43 43
 
44 44
   def task_params
45
-    params.require(:task).permit(:name, :description, :duration, :activity_id, :created_at, :updated_at)
45
+    params.require(:task).permit(:name, :description, :start_time, :end_time, :is_paid, :paid_by, :owner_id, :activity_id)
46 46
   end
47 47
 
48 48
   def set_task
@@ -50,6 +50,6 @@ class Api::V1::TasksController < ApplicationController
50 50
   end
51 51
 
52 52
   def check_owner
53
-    head :forbidden unless @task.user_id === current_user.id
53
+    head :forbidden unless @task.owner_id === current_user.id
54 54
   end
55 55
 end

+ 4
- 2
app/models/task.rb 파일 보기

@@ -1,7 +1,9 @@
1 1
 class Task < ApplicationRecord
2 2
   validates :name, presence: true
3
-  validates :user, presence: true
3
+  validates :owner, presence: true
4
+  validates :activity, presence: true
5
+  validates :start_time, presence: true
4 6
 
5 7
   belongs_to :activity
6
-  belongs_to :user
8
+  belongs_to :owner, class_name: "User"
7 9
 end

+ 1
- 1
app/models/user.rb 파일 보기

@@ -9,7 +9,7 @@ class User < ApplicationRecord
9 9
   has_many :joined_user_activities
10 10
   has_many :activities, :through => :joined_user_activities
11 11
 
12
-  has_many :tasks
12
+  has_many :tasks, foreign_key: 'owner_id', class_name: 'Task'
13 13
   
14 14
   has_many :memberships, foreign_key: :member_id
15 15
   has_many :teams, through: :memberships

+ 1
- 1
app/serializers/task_serializer.rb 파일 보기

@@ -1,6 +1,6 @@
1 1
 class TaskSerializer
2 2
   include JSONAPI::Serializer
3
-  attributes :name, :description, :duration, :user_id, :activity_id, :created_at, :updated_at
3
+  attributes :name, :description, :start_time, :end_time, :is_paid, :paid_by_id, :owner_id, :activity_id
4 4
 
5 5
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6 6
 end

+ 5
- 2
db/migrate/20210510133939_create_tasks.rb 파일 보기

@@ -4,9 +4,12 @@ class CreateTasks < ActiveRecord::Migration[6.1]
4 4
       t.string :name, null: false
5 5
       t.index :name
6 6
       t.text :description
7
-      t.integer :duration
7
+      t.datetime :start_time, null: false
8
+      t.datetime :end_time, default: nil
9
+      t.boolean :is_paid, default: false
10
+      t.references :paid_by, default: nil
8 11
       t.references :activity, null: false
9
-      t.references :user, null: false
12
+      t.references :owner, null: false
10 13
 
11 14
       t.timestamps
12 15
     end

+ 7
- 3
db/schema.rb 파일 보기

@@ -48,14 +48,18 @@ ActiveRecord::Schema.define(version: 2021_08_03_152520) do
48 48
   create_table "tasks", force: :cascade do |t|
49 49
     t.string "name", null: false
50 50
     t.text "description"
51
-    t.integer "duration"
51
+    t.datetime "start_time", null: false
52
+    t.datetime "end_time"
53
+    t.boolean "is_paid", default: false
54
+    t.bigint "paid_by_id"
52 55
     t.bigint "activity_id", null: false
53
-    t.bigint "user_id", null: false
56
+    t.bigint "owner_id", null: false
54 57
     t.datetime "created_at", precision: 6, null: false
55 58
     t.datetime "updated_at", precision: 6, null: false
56 59
     t.index ["activity_id"], name: "index_tasks_on_activity_id"
57 60
     t.index ["name"], name: "index_tasks_on_name"
58
-    t.index ["user_id"], name: "index_tasks_on_user_id"
61
+    t.index ["owner_id"], name: "index_tasks_on_owner_id"
62
+    t.index ["paid_by_id"], name: "index_tasks_on_paid_by_id"
59 63
   end
60 64
 
61 65
   create_table "teams", force: :cascade do |t|

+ 5
- 4
db/seeds.rb 파일 보기

@@ -48,12 +48,13 @@ end
48 48
 User.create(username: "admin", email: "admin@email.com", password: "azerty", is_admin: true)
49 49
 puts "Created Admin"
50 50
 
51
-30.times do |i|
51
+60.times do |i|
52 52
   task = Task.create(
53
-    name: Faker::Fantasy::Tolkien.character,
53
+    name: Faker::Verb.ing_form,
54 54
     description: Faker::Fantasy::Tolkien.poem,
55
-    duration: rand(1..120),
56
-    user_id: User.all.sample.id,
55
+    start_time: Faker::Time.between(from: DateTime.now - 1, to: DateTime.now),
56
+    end_time: Faker::Time.between(from: DateTime.now + 1, to: DateTime.now + 5),
57
+    owner_id: User.all.sample.id,
57 58
     activity_id: Activity.all.sample.id
58 59
   )
59 60
   puts "Created TASK ##{i+1} - #{task.name}"

+ 3
- 3
test/controllers/api/v1/tasks_controller_test.rb 파일 보기

@@ -30,7 +30,7 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
30 30
 
31 31
     json_response = JSON.parse(self.response.body)
32 32
     assert_equal @task.name, json_response['data']['attributes']['name']
33
-    assert_equal @task.user_id, json_response['data']['attributes']['user_id']
33
+    assert_equal @task.owner_id, json_response['data']['attributes']['owner_id']
34 34
     assert_equal @task.activity_id, json_response['data']['attributes']['activity_id']
35 35
   end
36 36
 
@@ -44,7 +44,7 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
44 44
   test "should create task" do
45 45
     assert_difference("Task.count") do
46 46
       post api_v1_activity_tasks_url(@activity),
47
-      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
47
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity, start_time: DateTime.now } },
48 48
       headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
49 49
       as: :json
50 50
     end
@@ -54,7 +54,7 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
54 54
   test "should not create task" do
55 55
     assert_no_difference("Task.count") do
56 56
       post api_v1_activity_tasks_url(@activity),
57
-      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
57
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity } },
58 58
       as: :json
59 59
     end
60 60
     assert_response :forbidden

+ 5
- 3
test/fixtures/tasks.yml 파일 보기

@@ -3,6 +3,8 @@
3 3
 one:
4 4
   name: Do something
5 5
   description: Description of a task
6
-  duration: 60
7
-  user: one
8
-  activity: one
6
+  start_time: <%= DateTime.now - 1%>
7
+  end_time: <%= DateTime.now +1 %>
8
+  is_paid: false
9
+  owner: one
10
+  activity: one

+ 1
- 1
test/models/task_test.rb 파일 보기

@@ -7,7 +7,7 @@ class TaskTest < ActiveSupport::TestCase
7 7
   end
8 8
 
9 9
   test "task should be valid" do
10
-    task = Task.new(name: "Write", description: "Write a new line", user_id: @user.id, activity_id: @activity.id)
10
+    task = Task.new(name: "Write", description: "Write a new line", owner_id: @user.id, activity_id: @activity.id, start_time: DateTime.now)
11 11
     assert task.valid?
12 12
   end
13 13
 

Loading…
취소
저장