Skip to content

Commit 7275831

Browse files
committed
Fix JSON parsing, add tests, update swagger file
1 parent e20495e commit 7275831

14 files changed

+323
-7
lines changed

app/controllers/pias_controller.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def show
2626

2727
# POST /pias
2828
def create
29-
@pia = Pia.new(pia_params)
29+
pia_parameters = pia_params
30+
pia_parameters[:structure_data] = JSON.parse(pia_parameters[:structure_data]) if pia_parameters[:structure_data]
31+
@pia = Pia.new(pia_parameters)
3032

3133
if @pia.save
3234
render json: @pia, status: :created
@@ -37,7 +39,10 @@ def create
3739

3840
# PATCH/PUT /pias/1
3941
def update
40-
if @pia.update(pia_params)
42+
pia_parameters = pia_params
43+
pia_parameters[:structure_data] = JSON.parse(pia_parameters[:structure_data]) if pia_parameters[:structure_data]
44+
45+
if @pia.update(pia_parameters)
4146
render json: @pia
4247
else
4348
render json: @pia.errors, status: :unprocessable_entity
@@ -100,6 +105,10 @@ def pia_params
100105
:concerned_people_searched_opinion,
101106
:rejection_reason,
102107
:applied_adjustments,
103-
:is_example)
108+
:is_example,
109+
:structure_id,
110+
:structure_name,
111+
:structure_sector_name,
112+
:structure_data)
104113
end
105114
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class StructuresController < ApplicationController
2+
before_action :set_structure, only: %i[show update destroy duplicate]
3+
4+
# GET /structures
5+
def index
6+
sorting = sorting_params
7+
sorting = nil unless Structure.attribute_names.include?(sorting[:column])
8+
sorting[:direction] = 'asc' if sorting && sorting[:direction] != 'desc'
9+
@structures = Structure.all
10+
@structures = @structures.order("#{sorting[:column]} #{sorting[:direction]}") if sorting.present?
11+
12+
render json: @structures
13+
end
14+
15+
# GET /structures/1
16+
def show
17+
render json: @structure
18+
end
19+
20+
# POST /structures
21+
def create
22+
structures_parameters = structure_params
23+
structures_parameters[:data] = JSON.parse(structures_parameters[:data])
24+
@structure = Structure.new(structures_parameters)
25+
26+
if @structure.save
27+
render json: @structure, status: :created
28+
else
29+
render json: @structure.errors, status: :unprocessable_entity
30+
end
31+
end
32+
33+
# PATCH/PUT /structures/1
34+
def update
35+
structures_parameters = structure_params
36+
structures_parameters[:data] = JSON.parse(structures_parameters[:data]) if structures_parameters[:data]
37+
38+
if @structure.update(structures_parameters)
39+
render json: @structure
40+
else
41+
render json: @structure.errors, status: :unprocessable_entity
42+
end
43+
end
44+
45+
# DELETE /structures/1
46+
def destroy
47+
@structure.destroy
48+
end
49+
50+
private
51+
52+
def import_params
53+
params.fetch(:import, {}).permit(:data)
54+
end
55+
56+
# Use callbacks to share common setup or constraints between actions.
57+
def set_structure
58+
@structure = Structure.find(params[:id])
59+
end
60+
61+
# Only allow trusted sorting parameters
62+
def sorting_params
63+
params.fetch(:sort, {}).permit(:column, :direction)
64+
end
65+
66+
# Only allow a trusted parameter "white list" through.
67+
def structure_params
68+
params.fetch(:structure, {}).permit(:name,
69+
:sector_name,
70+
:data)
71+
end
72+
end

app/models/pia.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Pia < ApplicationRecord
44
has_many :evaluations, inverse_of: :pia, dependent: :destroy
55
has_many :measures, inverse_of: :pia, dependent: :destroy
66
has_many :attachments, inverse_of: :pia, dependent: :destroy
7+
belongs_to :structure, optional: true
78
validates :name, presence: true
89

910
def self.import(json_string)

app/models/structure.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Structure < ApplicationRecord
2+
has_many :pias, dependent: :nullify
3+
end

app/serializers/pia_serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ class PiaSerializer < ActiveModel::Serializer
1717
:created_at,
1818
:updated_at,
1919
:concerned_people_searched_opinion,
20-
:concerned_people_searched_content
20+
:concerned_people_searched_content,
21+
:structure_id,
22+
:structure_name,
23+
:structure_sector_name,
24+
:structure_data
2125
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class StructureSerializer < ActiveModel::Serializer
2+
attributes :id,
3+
:name,
4+
:sector_name,
5+
:data,
6+
:created_at,
7+
:updated_at
8+
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
post '/import', to: 'pias#import'
2020
end
2121
end
22+
resources :structures
2223
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateStructures < ActiveRecord::Migration[5.0]
2+
def change
3+
create_table :structures do |t|
4+
t.string :name, null: false
5+
t.string :sector_name, null: false
6+
t.jsonb :data, null: false, default: {}
7+
8+
t.timestamps
9+
end
10+
end
11+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AddStructureToPia < ActiveRecord::Migration[5.0]
2+
def change
3+
add_reference :pias, :structure, foreign_key: true
4+
add_column :pias, :structure_name, :string
5+
add_column :pias, :structure_sector_name, :string
6+
add_column :pias, :structure_data, :jsonb
7+
end
8+
end

db/schema.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20180525222420) do
13+
ActiveRecord::Schema.define(version: 20181008150920) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -89,11 +89,25 @@
8989
t.integer "is_example", default: 0
9090
t.boolean "concerned_people_searched_opinion", default: false
9191
t.string "concerned_people_searched_content"
92+
t.integer "structure_id"
93+
t.string "structure_name"
94+
t.string "structure_sector_name"
95+
t.jsonb "structure_data"
96+
t.index ["structure_id"], name: "index_pias_on_structure_id", using: :btree
97+
end
98+
99+
create_table "structures", force: :cascade do |t|
100+
t.string "name", null: false
101+
t.string "sector_name", null: false
102+
t.jsonb "data", default: {}, null: false
103+
t.datetime "created_at", null: false
104+
t.datetime "updated_at", null: false
92105
end
93106

94107
add_foreign_key "answers", "pias"
95108
add_foreign_key "attachments", "pias"
96109
add_foreign_key "comments", "pias"
97110
add_foreign_key "evaluations", "pias"
98111
add_foreign_key "measures", "pias"
112+
add_foreign_key "pias", "structures"
99113
end

0 commit comments

Comments
 (0)