Class: Voluntary::Api::V1::FeedbacksController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Voluntary::Api::V1::FeedbacksController
- Includes:
- V1::BaseController
- Defined in:
- app/controllers/voluntary/api/v1/feedbacks_controller.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/controllers/voluntary/api/v1/feedbacks_controller.rb', line 41 def create resource = Community.friendly.find(params[:feedback][:community_slug]).feedbacks.new params[:feedback] resource.user_id = current_user.id resource.save resource.category_ids = params[:feedback][:category_ids] || [] if resource.persisted? respond_to do |format| format.json do render json: resource.persisted? ? resource : { errors: resource.errors.to_hash } end end end |
#destroy ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/controllers/voluntary/api/v1/feedbacks_controller.rb', line 69 def destroy resource = Community.friendly.find(params[:community_slug]).feedbacks.friendly.find(params[:id]) resource.destroy respond_to do |format| format.json do render json: if resource.persisted? { error: I18n.t('activerecord.errors.models.feedback.attributes.base.deletion_failed') } else {} end end end end |
#index ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/controllers/voluntary/api/v1/feedbacks_controller.rb', line 6 def index = {} community = Community.friendly.find(params[:community_slug]) collection = community.feedbacks.includes(:user) collection = collection.for_category(community.id, params[:category_slug]) if params[:category_slug].present? collection = collection.where(feedback_type: params[:feedback_type]) if params[:feedback_type].present? [:json] = collection.paginate page: params[:page], per_page: 10 [:meta] = { pagination: { total_pages: [:json].total_pages, current_page: [:json].current_page, previous_page: [:json].previous_page, next_page: [:json].next_page } } respond_with do |format| format.json { render } end end |
#show ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/controllers/voluntary/api/v1/feedbacks_controller.rb', line 27 def show resource = Community.friendly.find(params[:community_slug]).feedbacks.friendly.find(params[:id]) if current_user resource.positive = Feedback.likes_or_dislikes_for(current_user, [resource.id])[resource.id].try(:positive) end respond_to do |format| format.json do render json: resource end end end |
#update ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/controllers/voluntary/api/v1/feedbacks_controller.rb', line 54 def update resource = Community.friendly.find(params[:feedback][:community_slug]).feedbacks.friendly.find(params[:id]) raise CanCan::AccessDenied unless resource.user_id == current_user.id resource.update_attributes params[:feedback] resource.category_ids = params[:feedback][:category_ids] || [] if resource.valid? respond_to do |format| format.json do render json: resource.valid? ? resource : { errors: resource.errors.to_hash } end end end |