Class: Voluntary::Api::V1::StoriesController

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

Instance Method Summary collapse

Methods included from V1::BaseController

#parent, #voluntary_application_javascripts, #voluntary_application_stylesheets

Instance Method Details

#activateObject



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

def activate
  story = Story.find params[:id]
  
  authorize! :update, story
  
  story.activate
  
  respond_to do |format|
    format.json do
      render json: story.errors.empty? && story.valid? ? product_serializer('Story', story) : { errors: story.errors.to_hash }
    end
  end
end

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/voluntary/api/v1/stories_controller.rb', line 16

def create
  project = Project.find(params[:story][:project_id])
  story = project.story_class.new({ project_id: project.id }.merge(params[:story] || {}))
  
  authorize! :create, story
  
  story.save
  
  respond_to do |format|
    format.json do
      render json: story.persisted? ? product_serializer('Story', story) : { errors: story.errors.to_hash }
    end
  end
end

#deactivateObject



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

def deactivate
  story = Story.find params[:id]
  
  authorize! :update, story
  
  story.deactivate
  
  respond_to do |format|
    format.json do
      render json: story.errors.empty? && story.valid? ? product_serializer('Story', story) : { errors: story.errors.to_hash }
    end
  end
end

#destroyObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/voluntary/api/v1/stories_controller.rb', line 73

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

#showObject



6
7
8
9
10
11
12
13
14
# File 'app/controllers/voluntary/api/v1/stories_controller.rb', line 6

def show
  story = Story.find(params[:id])
  
  respond_to do |format|
    format.json do
      render json: product_serializer('Story', story)
    end
  end
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/voluntary/api/v1/stories_controller.rb', line 31

def update
  story = Story.find params[:id]
  
  authorize! :update, story
  
  story.update_attributes params[:story]
  
  respond_to do |format|
    format.json do
      render json: story.valid? ? product_serializer('Story', story) : { errors: story.errors.to_hash }
    end
  end
end