Class: Railspress::Api::V1::TagsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/railspress/api/v1/tags_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_key

Instance Method Summary collapse

Instance Method Details

#createObject



29
30
31
32
33
34
35
36
37
# File 'app/controllers/railspress/api/v1/tags_controller.rb', line 29

def create
  tag = Railspress::Tag.new(tag_params)

  if tag.save
    render json: { data: serialize_tag(tag) }, status: :created
  else
    render_validation_errors(tag)
  end
end

#destroyObject



47
48
49
50
# File 'app/controllers/railspress/api/v1/tags_controller.rb', line 47

def destroy
  @tag.destroy
  head :no_content
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/railspress/api/v1/tags_controller.rb', line 9

def index
  tags = Railspress::Tag.ordered
  total_count = tags.count
  tags = tags.offset((page - 1) * per_page).limit(per_page)

  render json: {
    data: tags.map { |tag| serialize_tag(tag) },
    meta: {
      page: page,
      per: per_page,
      total_count: total_count,
      total_pages: (total_count.to_f / per_page).ceil
    }
  }
end

#showObject



25
26
27
# File 'app/controllers/railspress/api/v1/tags_controller.rb', line 25

def show
  render json: { data: serialize_tag(@tag) }
end

#updateObject



39
40
41
42
43
44
45
# File 'app/controllers/railspress/api/v1/tags_controller.rb', line 39

def update
  if @tag.update(tag_params)
    render json: { data: serialize_tag(@tag) }
  else
    render_validation_errors(@tag)
  end
end