Browse Source

Add defs index and show, and relative unit tests

Lou 3 years ago
parent
commit
02ca539cc8

+ 24
- 0
app/controllers/api/v1/records_controller.rb View File

@@ -0,0 +1,24 @@
1
+class Api::V1::RecordsController < ApplicationController
2
+  before_action :set_record, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: RecordSerializer.new(Task.find(params[:task_id]).records).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: RecordSerializer.new(@record).serializable_hash.to_json
11
+  end
12
+  
13
+
14
+  private
15
+
16
+  # Only allow a trusted parameter "white list" through.
17
+  def record_params
18
+    params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
19
+  end
20
+
21
+  def set_record
22
+    @record = Task.find(params[:task_id]).records.find(params[:id])
23
+  end
24
+end

+ 1
- 0
app/models/task.rb View File

@@ -4,4 +4,5 @@ class Task < ApplicationRecord
4 4
 
5 5
   belongs_to :activity
6 6
   belongs_to :user
7
+  has_many :records
7 8
 end

+ 6
- 0
app/serializers/record_serializer.rb View File

@@ -0,0 +1,6 @@
1
+class RecordSerializer
2
+  include JSONAPI::Serializer
3
+  attributes :duration, :user_id, :task_id, :is_handled, :handled_by_id
4
+
5
+  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
+end

+ 1
- 0
app/serializers/task_serializer.rb View File

@@ -1,6 +1,7 @@
1 1
 class TaskSerializer
2 2
   include JSONAPI::Serializer
3 3
   attributes :name, :description, :user_id, :activity_id
4
+  # has_many :records
4 5
 
5 6
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6 7
 end

+ 5
- 0
bin/rails View File

@@ -1,4 +1,9 @@
1 1
 #!/usr/bin/env ruby
2
+begin
3
+  load File.expand_path('../spring', __FILE__)
4
+rescue LoadError => e
5
+  raise unless e.message.include?('spring')
6
+end
2 7
 load File.expand_path("spring", __dir__)
3 8
 APP_PATH = File.expand_path('../config/application', __dir__)
4 9
 require_relative "../config/boot"

+ 5
- 0
bin/rake View File

@@ -1,4 +1,9 @@
1 1
 #!/usr/bin/env ruby
2
+begin
3
+  load File.expand_path('../spring', __FILE__)
4
+rescue LoadError => e
5
+  raise unless e.message.include?('spring')
6
+end
2 7
 load File.expand_path("spring", __dir__)
3 8
 require_relative "../config/boot"
4 9
 require "rake"

+ 12
- 9
bin/spring View File

@@ -1,14 +1,17 @@
1 1
 #!/usr/bin/env ruby
2
-if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
3
-  gem "bundler"
4
-  require "bundler"
5 2
 
6
-  # Load Spring without loading other gems in the Gemfile, for speed.
7
-  Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
3
+# This file loads Spring without using Bundler, in order to be fast.
4
+# It gets overwritten when you run the `spring binstub` command.
5
+
6
+unless defined?(Spring)
7
+  require 'rubygems'
8
+  require 'bundler'
9
+
10
+  lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
11
+  spring = lockfile.specs.detect { |spec| spec.name == 'spring' }
12
+  if spring
8 13
     Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
9
-    gem "spring", spring.version
10
-    require "spring/binstub"
11
-  rescue Gem::LoadError
12
-    # Ignore when Spring is not installed.
14
+    gem 'spring', spring.version
15
+    require 'spring/binstub'
13 16
   end
14 17
 end

+ 3
- 1
config/routes.rb View File

@@ -4,7 +4,9 @@ Rails.application.routes.draw do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6 6
       resources :activities do
7
-        resources :tasks
7
+        resources :tasks do
8
+          resources :records
9
+        end
8 10
       end
9 11
       resources :tasks
10 12
     end

+ 1
- 1
db/migrate/20210511212634_create_records.rb View File

@@ -5,7 +5,7 @@ class CreateRecords < ActiveRecord::Migration[6.1]
5 5
       t.references :user, null: false
6 6
       t.references :task, null: false
7 7
       t.boolean :is_handled, default: false
8
-      t.references :handled_by, default: nil
8
+      t.references :handled_by, default: nil, null: true
9 9
 
10 10
       t.timestamps
11 11
     end

+ 11
- 0
db/seeds.rb View File

@@ -6,6 +6,8 @@ JoinedUserActivity.delete_all
6 6
 JoinedUserActivity.reset_pk_sequence
7 7
 Task.delete_all
8 8
 Task.reset_pk_sequence
9
+Record.delete_all
10
+Record.reset_pk_sequence
9 11
 
10 12
 10.times do |i|
11 13
   name = Faker::Name.first_name.downcase
@@ -37,4 +39,13 @@ end
37 39
     activity_id: Activity.all.sample.id
38 40
   )
39 41
   puts "Created TASK ##{i} - #{task.name}"
42
+end
43
+
44
+20.times do |i|
45
+  record = Record.create(
46
+    duration: rand(60..480),
47
+    user_id: User.all.sample.id,
48
+    task_id: Task.all.sample.id
49
+  )
50
+  puts "Created RECORD ##{i}"
40 51
 end

BIN
erd.pdf View File


+ 37
- 0
test/controllers/api/v1/records_controller_test.rb View File

@@ -0,0 +1,37 @@
1
+require "test_helper"
2
+
3
+class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @record = records(:one)
6
+    @activity = activities(:one)
7
+    @task = tasks(:one)
8
+  end
9
+
10
+  # INDEX
11
+  test "should access index" do
12
+    get api_v1_activity_task_records_url(@activity, @task),
13
+    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
14
+    as: :json
15
+    assert_response :success
16
+  end
17
+
18
+  test "should not access index" do
19
+    get api_v1_activity_task_records_url(@activity, @task),
20
+    as: :json
21
+    assert_response :forbidden
22
+  end
23
+
24
+  # SHOW
25
+  test "should access show" do
26
+    get api_v1_activity_task_record_url(@activity, @task, @record),
27
+    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
28
+    as: :json
29
+    assert_response :success
30
+  end
31
+
32
+  test "should not access show" do
33
+    get api_v1_activity_task_record_url(@activity, @task, @record),
34
+    as: :json
35
+    assert_response :forbidden
36
+  end
37
+end

+ 7
- 1
test/fixtures/records.yml View File

@@ -4,4 +4,10 @@ one:
4 4
   duration: 666
5 5
   user: one
6 6
   task: one
7
-  is_handled: false
7
+  is_handled: false
8
+
9
+two:
10
+  duration: 100
11
+  user: one
12
+  task: two 
13
+  is_handled: yes

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

@@ -21,11 +21,4 @@ class RecordTest < ActiveSupport::TestCase
21 21
     record = Record.new(duration: 100)
22 22
     assert_not record.valid?
23 23
   end
24
-
25
-  # NOT SURE THE FOLLOWING TEST IS TESTING ANYTHING USEFULL
26
-  # test "handled records should have a valid handled_by value" do
27
-  #   @record.update(is_handled: true)
28
-  #   assert @record.valid?
29
-  # end
30
-
31 24
 end

Loading…
Cancel
Save