|
8 | 8 |
|
9 | 9 | ### DATABASE |
10 | 10 | ActiveRecord::Schema.define do |
| 11 | + create_table :sessions, id: false, force: true do |t| |
| 12 | + t.string :id, :limit => 36, :primary_key => true, null: false |
| 13 | + t.string :survey_id, :limit => 36, null: false |
| 14 | + |
| 15 | + t.timestamps |
| 16 | + end |
| 17 | + |
| 18 | + create_table :responses, force: true do |t| |
| 19 | + #t.string :id, :limit => 36, :primary_key => true, null: false |
| 20 | + |
| 21 | + t.string :session_id, limit: 36, null: false |
| 22 | + |
| 23 | + t.string :type |
| 24 | + t.string :question_id, limit: 36 |
| 25 | + |
| 26 | + t.timestamps |
| 27 | + end |
| 28 | + |
| 29 | + create_table :response_texts, force: true do |t| |
| 30 | + t.text :text |
| 31 | + t.integer :response_id |
| 32 | + |
| 33 | + t.timestamps |
| 34 | + end |
| 35 | + |
11 | 36 | create_table :people, force: true do |t| |
12 | 37 | t.string :name |
13 | 38 | t.string :email |
|
360 | 385 | end |
361 | 386 |
|
362 | 387 | ### MODELS |
| 388 | +class Session < ActiveRecord::Base |
| 389 | + self.primary_key = "id" |
| 390 | + has_many :responses |
| 391 | +end |
| 392 | + |
| 393 | +class Response < ActiveRecord::Base |
| 394 | + belongs_to :session |
| 395 | + has_one :paragraph, :class_name => "ResponseText::Paragraph" |
| 396 | + |
| 397 | + def response_type |
| 398 | + case self.type |
| 399 | + when "Response::SingleTextbox" |
| 400 | + "single_textbox" |
| 401 | + else |
| 402 | + "question" |
| 403 | + end |
| 404 | + end |
| 405 | + def response_type=type |
| 406 | + self.type = case type |
| 407 | + when "single_textbox" |
| 408 | + "Response::SingleTextbox" |
| 409 | + else |
| 410 | + "Response" |
| 411 | + end |
| 412 | + end |
| 413 | +end |
| 414 | + |
| 415 | +class Response::SingleTextbox < Response |
| 416 | + has_one :paragraph, :class_name => "ResponseText::Paragraph", :foreign_key => :response_id |
| 417 | +end |
| 418 | + |
| 419 | +class ResponseText < ActiveRecord::Base |
| 420 | +end |
| 421 | + |
| 422 | +class ResponseText::Paragraph < ResponseText |
| 423 | +end |
| 424 | + |
363 | 425 | class Person < ActiveRecord::Base |
364 | 426 | has_many :posts, foreign_key: 'author_id' |
365 | 427 | has_many :comments, foreign_key: 'author_id' |
@@ -736,6 +798,19 @@ class Robot < ActiveRecord::Base |
736 | 798 | end |
737 | 799 |
|
738 | 800 | ### CONTROLLERS |
| 801 | +class SessionsController < ActionController::Base |
| 802 | + include JSONAPI::ActsAsResourceController |
| 803 | + before_action :create_responses_relationships, :only => [:create,:update] |
| 804 | + |
| 805 | + private |
| 806 | + def create_responses_relationships |
| 807 | + if !params[:data][:relationships].nil? && !params[:data][:relationships][:responses].nil? |
| 808 | + responses_params = params[:data][:relationships].delete(:responses) |
| 809 | + params[:data][:attributes][:responses] = responses_params |
| 810 | + end |
| 811 | + end |
| 812 | +end |
| 813 | + |
739 | 814 | class AuthorsController < JSONAPI::ResourceControllerMetal |
740 | 815 | end |
741 | 816 |
|
@@ -1039,6 +1114,57 @@ class BaseResource < JSONAPI::Resource |
1039 | 1114 | abstract |
1040 | 1115 | end |
1041 | 1116 |
|
| 1117 | +class SessionResource < JSONAPI::Resource |
| 1118 | + key_type :uuid |
| 1119 | + |
| 1120 | + attributes :survey_id, :responses |
| 1121 | + |
| 1122 | + has_many :responses |
| 1123 | + |
| 1124 | + def responses=params |
| 1125 | + params[:data].each { |datum| |
| 1126 | + response = @model.responses.build(((datum[:attributes].respond_to?(:permit))? datum[:attributes].permit(:response_type, :question_id) : datum[:attributes])) |
| 1127 | + |
| 1128 | + (datum[:relationships] || {}).each_pair { |k,v| |
| 1129 | + case k |
| 1130 | + when "paragraph" |
| 1131 | + response.paragraph = ResponseText::Paragraph.create(((v[:data][:attributes].respond_to?(:permit))? v[:data][:attributes].permit(:text) : v[:data][:attributes])) |
| 1132 | + end |
| 1133 | + } |
| 1134 | + } |
| 1135 | + end |
| 1136 | + def responses |
| 1137 | + end |
| 1138 | + |
| 1139 | + def self.creatable_fields(context) |
| 1140 | + super + [ |
| 1141 | + :id, |
| 1142 | + ] |
| 1143 | + end |
| 1144 | + |
| 1145 | + def fetchable_fields |
| 1146 | + super - [:responses] |
| 1147 | + end |
| 1148 | +end |
| 1149 | + |
| 1150 | +class ResponseResource < JSONAPI::Resource |
| 1151 | + model_hint model: Response::SingleTextbox, resource: :response |
| 1152 | + |
| 1153 | + has_one :session |
| 1154 | + |
| 1155 | + attributes :question_id, :response_type |
| 1156 | + |
| 1157 | + has_one :paragraph |
| 1158 | +end |
| 1159 | + |
| 1160 | +class ParagraphResource < JSONAPI::Resource |
| 1161 | + model_name 'ResponseText::Paragraph' |
| 1162 | + |
| 1163 | + attributes :text |
| 1164 | + |
| 1165 | + has_one :response |
| 1166 | +end |
| 1167 | + |
1042 | 1168 | class PersonResource < BaseResource |
1043 | 1169 | attributes :name, :email |
1044 | 1170 | attribute :date_joined, format: :date_with_timezone |
|
0 commit comments