Skip to content

Commit

Permalink
Frerin/Paperwork CRUD
Browse files Browse the repository at this point in the history
Create, read, update, delete paths for paperworks and separate API namespace
  • Loading branch information
kwfk authored and adowski committed Oct 23, 2019
1 parent 84de27a commit 37d7f4f
Show file tree
Hide file tree
Showing 25 changed files with 475 additions and 10 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ gem 'react-rails', '~> 2.6.0'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'active_model_serializers', '~> 0.10.0'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'faker'
gem 'pry-byebug'
gem 'rspec-rails'
end

group :development do
Expand Down
59 changes: 49 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.10.10)
actionpack (>= 4.1, < 6.1)
activemodel (>= 4.1, < 6.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (6.0.0)
activesupport (= 6.0.0)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -67,28 +72,35 @@ GEM
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
case_transform (0.2)
activesupport
coderay (1.1.2)
concurrent-ruby (1.1.5)
connection_pool (2.2.2)
crass (1.0.4)
crass (1.0.5)
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.3)
erubi (1.9.0)
execjs (2.7.0)
faker (2.6.0)
i18n (>= 1.6, < 1.8)
faraday (0.17.0)
multipart-post (>= 1.2, < 3)
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
hashie (3.6.0)
i18n (1.6.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.3)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
jsonapi-renderer (0.2.2)
jwt (2.2.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand All @@ -105,9 +117,9 @@ GEM
mimemagic (0.3.3)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.12.0)
minitest (5.12.2)
msgpack (1.3.1)
multi_json (1.13.1)
multi_json (1.14.1)
multi_xml (0.6.0)
multipart-post (2.1.1)
nio4r (2.5.2)
Expand All @@ -130,10 +142,16 @@ GEM
oauth2 (~> 1.1)
omniauth (~> 1.9)
orm_adapter (0.5.0)
parallel (1.17.0)
parallel (1.18.0)
parser (2.6.5.0)
ast (~> 2.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.7.0)
byebug (~> 11.0)
pry (~> 0.10)
puma (3.12.1)
rack (2.0.7)
rack-proxy (0.6.5)
Expand All @@ -158,16 +176,16 @@ GEM
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.2.0)
loofah (~> 2.2, >= 2.2.2)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.0.0)
actionpack (= 6.0.0)
activesupport (= 6.0.0)
method_source
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
rainbow (3.0.0)
rake (12.3.3)
rake (13.0.0)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
Expand All @@ -180,7 +198,24 @@ GEM
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
rubocop (0.75.0)
rspec-core (3.9.0)
rspec-support (~> 3.9.0)
rspec-expectations (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (3.9.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.0)
rubocop (0.75.1)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.6)
Expand Down Expand Up @@ -234,22 +269,26 @@ GEM
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
zeitwerk (2.1.10)
zeitwerk (2.2.0)

PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.10.0)
bootsnap (>= 1.4.2)
byebug
devise
faker
jbuilder (~> 2.7)
listen (>= 3.0.5, < 3.2)
omniauth-google-oauth2
pg (>= 0.18, < 2.0)
pry-byebug
puma (~> 3.11)
rails (~> 6.0.0)
react-rails (~> 2.6.0)
rspec-rails
rubocop
sass-rails (~> 5)
spring
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/paperworks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Paperworks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
58 changes: 58 additions & 0 deletions app/controllers/api/paperworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Api::PaperworksController < ApplicationController
before_action :set_paperwork, only: [:show, :update, :complete, :destroy]
respond_to :json

def show
render json: @paperwork
end

def create
@paperwork = Paperwork.new(paperwork_params)
if @paperwork.save
render json: @paperwork, status: :created
else
render json: { error: 'Could not create paperwork' }, status: :unprocessable_entity
end
end

def update
if @paperwork.update(paperwork_params)
render json: @paperwork, status: :ok
else
render json: { error: 'Could not update paperwork' }, status: :unprocessable_entity
end
end

def complete
if @paperwork.update(agree: true)
render json: @paperwork, status: :ok
else
render json: { error: 'Failed to mark as agreed' }, status: :unprocessable_entity
end
end

def destroy
if @paperwork.destroy
render json: @paperwork, status: :ok
else
render json: { error: 'Failed to delete paperwork' }, status: :unprocessable_entity
end
end

private

def set_paperwork
@paperwork = Paperwork.find(params[:id])
rescue ActiveRecord::RecordNotFound
render json: { error: 'Could not find paperwork' }, status: :not_found
end

def paperwork_params
paperwork_param = params.require(:paperwork).permit(:title,
:link,
:agree,
:participant_id)
# TODO: Replace staff_id with current_omniuser
paperwork_param.merge(staff_id: 1)
end
end
19 changes: 19 additions & 0 deletions app/controllers/paperworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class PaperworksController < ApplicationController
def index
@paperworks = Paperwork.all
end

def show
@paperwork = Paperwork.find(params[:id])
end

def new
@paperwork = Paperwork.new
@participants = Participant.all
end

def edit
@paperwork = Paperwork.find(params[:id])
@participants = Participant.all
end
end
2 changes: 2 additions & 0 deletions app/helpers/api/paperworks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Api::PaperworksHelper
end
2 changes: 2 additions & 0 deletions app/helpers/paperworks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PaperworksHelper
end
2 changes: 2 additions & 0 deletions app/models/participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ class Participant < ApplicationRecord
has_one :professional_questionnaire

enum status: {r0: 0, r1: 1, r2: 2, studio: 3}

delegate :name, to: :omniuser
end
2 changes: 2 additions & 0 deletions app/models/staff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Staff < ApplicationRecord
belongs_to :omniuser
has_many :casenotes
has_many :paperworks

delegate :name, to: :omniuser
end
6 changes: 6 additions & 0 deletions app/serializers/paperwork_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PaperworkSerializer < ActiveModel::Serializer
attributes :id, :title, :link, :agree, :staff, :participant

belongs_to :staff, serializer: SimpleStaffSerializer
belongs_to :participant, serializer: SimpleParticipantSerializer
end
17 changes: 17 additions & 0 deletions app/serializers/participant_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ParticipantSerializer < ActiveModel::Serializer
attributes :id, :email, :name, :status

has_many :casenotes
has_many :paperworks

has_one :personal_questionnaire
has_one :professional_questionnaire

def email
object.omniuser.email
end

def name
object.omniuser.name
end
end
11 changes: 11 additions & 0 deletions app/serializers/simple_participant_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SimpleParticipantSerializer < ActiveModel::Serializer
attributes :id, :email, :name, :status

def email
object.omniuser.email
end

def name
object.omniuser.name
end
end
11 changes: 11 additions & 0 deletions app/serializers/simple_staff_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SimpleStaffSerializer < ActiveModel::Serializer
attributes :id, :email, :name

def email
object.omniuser.email
end

def name
object.omniuser.name
end
end
14 changes: 14 additions & 0 deletions app/serializers/staff_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class StaffSerializer < ActiveModel::Serializer
attributes :id, :email, :name

has_many :casenotes
has_many :paperworks

def email
object.omniuser.email
end

def name
object.omniuser.name
end
end
23 changes: 23 additions & 0 deletions app/views/paperworks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Edit Paperwork</h1>
<%= form_with(model: @paperwork, url: api_paperwork_path, local: true) do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :link %><br>
<%= form.text_field :link %>
</p>
<p>
Participant: <br>
<%= form.collection_select :participant_id, @participants, :id, :name, prompt: true %>
</p>
<p>
<%= form.label :agree %><br>
<%= form.check_box :agree %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', paperworks_path %>
25 changes: 25 additions & 0 deletions app/views/paperworks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>All Paperworks</h1>
<table>
<tr>
<th>Title</th>
<th>Link</th>
<th>Participant</th>
<th>Agree</th>
<th colspan="3"></th>
</tr>

<% @paperworks.each do |paperwork| %>
<tr>
<td><%= paperwork.title %></td>
<td><%= paperwork.link %></td>
<td><%= paperwork.participant.name %></td>
<td><%= paperwork.agree %></td>
<td><%= link_to 'Show', paperwork_path(paperwork) %></td>
<td><%= link_to 'Edit', edit_paperwork_path(paperwork) %></td>
<td><%= link_to 'Delete', api_paperwork_path(paperwork),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<%= link_to 'New Paperwork', new_paperwork_path %>
Loading

0 comments on commit 37d7f4f

Please sign in to comment.