Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class QuestionnairesController < ApplicationController
- before_action :customer, only: %i(create update)
- before_action :check_present, only: %i(create update)
- # before_action :authenticate_customer!, only: %i(create update)
- before_action :authenticate_customer!, only: :create
- before_action :set_questionnaire, only: :update
- before_action :authenticate_role, only: :update
- def show
- def show
- render json: @questionnaire.as_json(only: [:type_of_questionnaire, :description, :status, :price, :reward],
- include: { questions: { only: [:type_of_question, :text, :variants]}},
- methods: [:company_name, :videos_link, :images_link])
- end
- def create
- questionnaire = @customer.questionnaires.build(questionnaire_params)
- if questionnaire.save
- render json: questionnaire, status: :ok
- else
- render json: { message: 'An error occurred!' }, status: 422
- end
- end
- def update
- unless current_customer == @customer || current_admin # admin_signed_in?
- return render json: { error: 'An unauthorized!' }, status: 401
- end
- if @questionnaire.update(questionnaire_params)
- render json: @questionnaire, status: :ok
- else
- render json: { message: 'An error occurred!' }, status: 422
- end
- end
- private
- def customer
- @customer ||= Customer.find_by_id(params[:customer_id])
- end
- def check_present
- return render json: { message: 'Customer not found'}, status: 400 if @customer.nil?
- end
- def set_questionnaire
- @questionnaire = Questionnaire.find_by_id(params[:id])
- end
- def authenticate_role
- # return if admin_signed_in?
- return if authenticate_admin!
- authenticate_customer!
- end
- def questionnaire_params
- params.require(:questionnaire).permit(:customer,
- :name,
- :description,
- :status,
- :admin_id,
- :price,
- :reward,
- :type_of_questionnaire,
- :slogan,
- :min_age,
- :max_age,
- :min_monthly_income,
- :max_monthly_income,
- :regions,
- :sexs,
- :family_statuses,
- :is_having_childrens,
- :field_of_activity,
- images: [],
- videos:[],
- questions_attributes: [:id,
- :type_of_question,
- :text,
- :variants,
- :max_variants
- ]
- )
- end
- end
Add Comment
Please, Sign In to add comment