Class: Voluntary::Api::V1::BrainstormingsController

Inherits:
ActionController::Base
  • Object
show all
Includes:
V1::BaseController
Defined in:
app/controllers/voluntary/api/v1/brainstormings_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
# File 'app/controllers/voluntary/api/v1/brainstormings_controller.rb', line 35

def create
  brainstorming = current_user.brainstormings.create params[:brainstorming]
  
  respond_to do |format|
    format.json do
      render json: brainstorming.persisted? ? brainstorming : { errors: brainstorming.errors.to_hash }
    end
  end
end

#destroyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/voluntary/api/v1/brainstormings_controller.rb', line 56

def destroy
  brainstorming = current_user.brainstormings.friendly.find params[:id]
  brainstorming.destroy
  
  respond_to do |format|
    format.json do
      render json: if brainstorming.persisted?
        { error: I18n.t('activerecord.errors.models.brainstorming.attributes.base.deletion_failed') }
      else
        {}
      end
    end
  end
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/voluntary/api/v1/brainstormings_controller.rb', line 6

def index
  options = {}

  user = User.friendly.find params[:user_slug]
  collection = user.brainstormings
  options[:json] = collection.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

#showObject



25
26
27
28
29
30
31
32
33
# File 'app/controllers/voluntary/api/v1/brainstormings_controller.rb', line 25

def show
  user = User.friendly.find(params[:user_slug])
  
  respond_to do |format|
    format.json do
      render json: user.brainstormings.friendly.find(params[:id])
    end
  end
end

#updateObject



45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/voluntary/api/v1/brainstormings_controller.rb', line 45

def update
  brainstorming = current_user.brainstormings.friendly.find params[:id]
  brainstorming.update_attributes params[:brainstorming]
  
  respond_to do |format|
    format.json do
      render json: brainstorming.valid? ? brainstorming : { errors: brainstorming.errors.to_hash }
    end
  end
end