Add defs index and show, and relative unit tests
This commit is contained in:
parent
1de186f65a
commit
02ca539cc8
14 changed files with 113 additions and 19 deletions
24
app/controllers/api/v1/records_controller.rb
Normal file
24
app/controllers/api/v1/records_controller.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Api::V1::RecordsController < ApplicationController
|
||||
before_action :set_record, only: %i[show update destroy]
|
||||
before_action :check_login
|
||||
|
||||
def index
|
||||
render json: RecordSerializer.new(Task.find(params[:task_id]).records).serializable_hash.to_json
|
||||
end
|
||||
|
||||
def show
|
||||
render json: RecordSerializer.new(@record).serializable_hash.to_json
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
# Only allow a trusted parameter "white list" through.
|
||||
def record_params
|
||||
params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
|
||||
end
|
||||
|
||||
def set_record
|
||||
@record = Task.find(params[:task_id]).records.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
|
@ -4,4 +4,5 @@ class Task < ApplicationRecord
|
|||
|
||||
belongs_to :activity
|
||||
belongs_to :user
|
||||
has_many :records
|
||||
end
|
||||
|
|
|
|||
6
app/serializers/record_serializer.rb
Normal file
6
app/serializers/record_serializer.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class RecordSerializer
|
||||
include JSONAPI::Serializer
|
||||
attributes :duration, :user_id, :task_id, :is_handled, :handled_by_id
|
||||
|
||||
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
||||
end
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
class TaskSerializer
|
||||
include JSONAPI::Serializer
|
||||
attributes :name, :description, :user_id, :activity_id
|
||||
# has_many :records
|
||||
|
||||
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
#!/usr/bin/env ruby
|
||||
begin
|
||||
load File.expand_path('../spring', __FILE__)
|
||||
rescue LoadError => e
|
||||
raise unless e.message.include?('spring')
|
||||
end
|
||||
load File.expand_path("spring", __dir__)
|
||||
APP_PATH = File.expand_path('../config/application', __dir__)
|
||||
require_relative "../config/boot"
|
||||
|
|
|
|||
5
bin/rake
5
bin/rake
|
|
@ -1,4 +1,9 @@
|
|||
#!/usr/bin/env ruby
|
||||
begin
|
||||
load File.expand_path('../spring', __FILE__)
|
||||
rescue LoadError => e
|
||||
raise unless e.message.include?('spring')
|
||||
end
|
||||
load File.expand_path("spring", __dir__)
|
||||
require_relative "../config/boot"
|
||||
require "rake"
|
||||
|
|
|
|||
21
bin/spring
21
bin/spring
|
|
@ -1,14 +1,17 @@
|
|||
#!/usr/bin/env ruby
|
||||
if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
|
||||
gem "bundler"
|
||||
require "bundler"
|
||||
|
||||
# Load Spring without loading other gems in the Gemfile, for speed.
|
||||
Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
|
||||
# This file loads Spring without using Bundler, in order to be fast.
|
||||
# It gets overwritten when you run the `spring binstub` command.
|
||||
|
||||
unless defined?(Spring)
|
||||
require 'rubygems'
|
||||
require 'bundler'
|
||||
|
||||
lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
|
||||
spring = lockfile.specs.detect { |spec| spec.name == 'spring' }
|
||||
if spring
|
||||
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
|
||||
gem "spring", spring.version
|
||||
require "spring/binstub"
|
||||
rescue Gem::LoadError
|
||||
# Ignore when Spring is not installed.
|
||||
gem 'spring', spring.version
|
||||
require 'spring/binstub'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ Rails.application.routes.draw do
|
|||
resources :users
|
||||
resources :tokens, only: %i[create]
|
||||
resources :activities do
|
||||
resources :tasks
|
||||
resources :tasks do
|
||||
resources :records
|
||||
end
|
||||
end
|
||||
resources :tasks
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ class CreateRecords < ActiveRecord::Migration[6.1]
|
|||
t.references :user, null: false
|
||||
t.references :task, null: false
|
||||
t.boolean :is_handled, default: false
|
||||
t.references :handled_by, default: nil
|
||||
t.references :handled_by, default: nil, null: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
|
|
|||
11
db/seeds.rb
11
db/seeds.rb
|
|
@ -6,6 +6,8 @@ JoinedUserActivity.delete_all
|
|||
JoinedUserActivity.reset_pk_sequence
|
||||
Task.delete_all
|
||||
Task.reset_pk_sequence
|
||||
Record.delete_all
|
||||
Record.reset_pk_sequence
|
||||
|
||||
10.times do |i|
|
||||
name = Faker::Name.first_name.downcase
|
||||
|
|
@ -37,4 +39,13 @@ end
|
|||
activity_id: Activity.all.sample.id
|
||||
)
|
||||
puts "Created TASK ##{i} - #{task.name}"
|
||||
end
|
||||
|
||||
20.times do |i|
|
||||
record = Record.create(
|
||||
duration: rand(60..480),
|
||||
user_id: User.all.sample.id,
|
||||
task_id: Task.all.sample.id
|
||||
)
|
||||
puts "Created RECORD ##{i}"
|
||||
end
|
||||
BIN
erd.pdf
BIN
erd.pdf
Binary file not shown.
37
test/controllers/api/v1/records_controller_test.rb
Normal file
37
test/controllers/api/v1/records_controller_test.rb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
require "test_helper"
|
||||
|
||||
class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@record = records(:one)
|
||||
@activity = activities(:one)
|
||||
@task = tasks(:one)
|
||||
end
|
||||
|
||||
# INDEX
|
||||
test "should access index" do
|
||||
get api_v1_activity_task_records_url(@activity, @task),
|
||||
headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
|
||||
as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should not access index" do
|
||||
get api_v1_activity_task_records_url(@activity, @task),
|
||||
as: :json
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
# SHOW
|
||||
test "should access show" do
|
||||
get api_v1_activity_task_record_url(@activity, @task, @record),
|
||||
headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
|
||||
as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should not access show" do
|
||||
get api_v1_activity_task_record_url(@activity, @task, @record),
|
||||
as: :json
|
||||
assert_response :forbidden
|
||||
end
|
||||
end
|
||||
8
test/fixtures/records.yml
vendored
8
test/fixtures/records.yml
vendored
|
|
@ -4,4 +4,10 @@ one:
|
|||
duration: 666
|
||||
user: one
|
||||
task: one
|
||||
is_handled: false
|
||||
is_handled: false
|
||||
|
||||
two:
|
||||
duration: 100
|
||||
user: one
|
||||
task: two
|
||||
is_handled: yes
|
||||
|
|
@ -21,11 +21,4 @@ class RecordTest < ActiveSupport::TestCase
|
|||
record = Record.new(duration: 100)
|
||||
assert_not record.valid?
|
||||
end
|
||||
|
||||
# NOT SURE THE FOLLOWING TEST IS TESTING ANYTHING USEFULL
|
||||
# test "handled records should have a valid handled_by value" do
|
||||
# @record.update(is_handled: true)
|
||||
# assert @record.valid?
|
||||
# end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue