Sha256: a03682f327da967915888fab64f9e1b78dee7bf155ccee46d852bb6bb8c5208e

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

class Voluntary::Api::V1::CommunitiesController < ActionController::Base
  include Voluntary::V1::BaseController
  
  respond_to :json
  
  def index
    options = {}
  
    options[:json] = Community.paginate page: params[:page], per_page: 10
    
    options[:meta] = { 
      pagination: {
        total_pages: options[:json].total_pages, current_page: options[:json].current_page,
        previous_page: options[:json].previous_page, next_page: options[:json].next_page
      }
    }
    
    respond_with do |format|
      format.json { render options }
    end
  end
  
  def show
    respond_to do |format|
      format.json do
        render json: Community.friendly.find(params[:id])
      end
    end
  end
  
  def create
    current_user.organizations.find(params[:community][:organization_id])
    resource = Community.create params[:community]
    
    respond_to do |format|
      format.json do
        render json: resource.persisted? ? resource : { errors: resource.errors.to_hash }
      end
    end
  end
  
  def update
    resource = current_user.communities.friendly.find params[:id]
    resource.update_attributes params[:community]
    
    respond_to do |format|
      format.json do
        render json: resource.valid? ? resource : { errors: resource.errors.to_hash }
      end
    end
  end
  
  def destroy
    resource = current_user.communities.friendly.find params[:id]
    resource.destroy
    
    respond_to do |format|
      format.json do
        render json: if resource.persisted?
          { error: I18n.t('activerecord.errors.models.community.attributes.base.deletion_failed') }
        else
          {}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voluntary_feedback-0.1.0 app/controllers/voluntary/api/v1/communities_controller.rb