Class: Voluntary::Api::V1::BrainstormingIdeasController

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

Instance Method Summary collapse

Instance Method Details

#createObject



30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/voluntary/api/v1/brainstorming_ideas_controller.rb', line 30

def create
  params[:brainstorming_idea][:brainstorming_id] = User.friendly.find(params[:user_slug]).brainstormings.friendly.find(params[:brainstorming_slug]).id
  resource = current_user.brainstorming_ideas.create params[:brainstorming_idea]
  
  respond_to do |format|
    format.json do
      render json: resource.persisted? ? resource : { errors: resource.errors.to_hash }
    end
  end
end

#destroyObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/voluntary/api/v1/brainstorming_ideas_controller.rb', line 52

def destroy
  resource = current_user.brainstorming_ideas.find params[:id]
  resource.destroy
  
  respond_to do |format|
    format.json do
      render json: if resource.persisted?
        { error: I18n.t('activerecord.errors.models.brainstorming_idea.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
24
25
26
27
28
# File 'app/controllers/voluntary/api/v1/brainstorming_ideas_controller.rb', line 6

def index
  user = User.friendly.find params[:user_slug]
  collection = user.brainstormings.friendly.find(params[:brainstorming_slug]).ideas.order('votes_count DESC').includes(:user)
  collection = collection.with_current_user_vote(current_user.id) if current_user  
  collection = collection.to_a
  
  collection.each_with_index do |resource, index|
    arguments = resource.arguments.includes(:user, :topic).to_a
    argument_likes = Argument.likes_or_dislikes_for(current_user, arguments.map(&:id))
    
    arguments.map! do |argument|
      argument.positive = argument_likes[argument.id].try(:positive)
      argument
    end
    
    resource.arguments = arguments
    collection[index] = resource
  end
  
  respond_with do |format|
    format.json { render json: collection }
  end
end

#updateObject



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/voluntary/api/v1/brainstorming_ideas_controller.rb', line 41

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