Sha256: 685dab716c5f93df4f2bc2165db33d105626f374afaeb368b1967db7d4b8883f

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

module ActiveRecordSurveyApi
	module Concerns
		module Controllers
			module Surveys
				extend ActiveSupport::Concern

				def index
					@surveys = all_surveys

					render json: serialize_models(@surveys, serializer: ActiveRecordSurveyApi::SurveySerializer, meta: { total: @surveys.length })
				end

				def show
					@survey = survey_by_id(params[:id])

					render json: serialize_model(@survey, serializer: ActiveRecordSurveyApi::SurveySerializer)
				end

				def destroy
					@survey = survey_by_id(params[:id])
					@survey.destroy

					head :no_content
				end

				def update
					@survey = survey_by_id(params[:id])
					@survey.update_attributes(survey_params)

					render json: serialize_model(@survey, serializer: ActiveRecordSurveyApi::SurveySerializer)
				end

				def create
					@survey = new_survey(survey_params)
					@survey.save

					#render json: @survey, serializer: SurveySerializer
					render json: serialize_model(@survey, serializer: ActiveRecordSurveyApi::SurveySerializer)
				end

				private 
					def all_surveys
						ActiveRecordSurvey::Survey.all
					end

					def survey_by_id(id)
						ActiveRecordSurvey::Survey.find(id)
					end

					def new_survey(params)
						ActiveRecordSurvey::Survey.new(survey_params)
					end

					def json_params
						ActionController::Parameters.new(JSON.parse(request.body.read))
					end

					def survey_params
						json_params.require(:survey).require(:attributes).permit(:name)
					end
			end
		end
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record_survey_api-0.0.14 lib/active_record_survey_api/concerns/controllers/surveys.rb
active_record_survey_api-0.0.12 lib/active_record_survey_api/concerns/controllers/surveys.rb
active_record_survey_api-0.0.11 lib/active_record_survey_api/concerns/controllers/surveys.rb
active_record_survey_api-0.0.7 lib/active_record_survey_api/concerns/controllers/surveys.rb
active_record_survey_api-0.0.6 lib/active_record_survey_api/concerns/controllers/surveys.rb