Class: Voluntary::Api::V1::ArgumentsController

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

Instance Method Summary collapse

Methods included from V1::BaseController

#parent, #voluntary_application_javascripts, #voluntary_application_stylesheets

Instance Method Details

#createObject

Raises:

  • (CanCan::AccessDenied)


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

def create
  raise CanCan::AccessDenied if current_user.blank?
  
  respond_to do |format|
    format.json { render json: Argument.create_with_topic(current_user.id, params[:argument]) }
  end
end

#destroyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/voluntary/api/v1/arguments_controller.rb', line 59

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

#indexObject



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/arguments_controller.rb', line 9

def index
  options = {}

  arguments = Argument
  arguments = Argument.where(
    argumentable_type: params[:argumentable_type], argumentable_id: params[:argumentable_id]
  ) if params[:argumentable_id].present?
  options[:json] = arguments.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

#updateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/voluntary/api/v1/arguments_controller.rb', line 38

def update
  resource = current_user.arguments.where('arguments.id = ?', params[:id]).first
  errors = {}
  
  if resource.nil?
    errors[:base] = I18n.t('activerecord.errors.models.general.attributes.base.user_resource_not_found')
  else
    topic = ArgumentTopic.find_or_create_by_name params[:argument][:topic_name]
    params[:argument][:topic_id] = topic.id
    resource.update_attributes params[:argument]
    resource.valid?
    errors = resource.errors.to_hash
  end
  
  respond_to do |format|
    format.json do
      render json: errors.empty? ? resource : { errors: errors }
    end
  end
end