Module: RESTFramework::BaseControllerMixin

Included in:
BaseModelControllerMixin
Defined in:
lib/rest_framework/controller_mixins/base.rb

Overview

This module provides the common functionality for any controller mixins, a ‘root` action, and the ability to route arbitrary actions with `extra_actions`. This is also where `api_response` is defined.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rest_framework/controller_mixins/base.rb', line 30

def self.included(base)
  if base.is_a? Class
    base.extend ClassMethods

    # Add class attributes (with defaults) unless they already exist.
    {
      extra_actions: nil,
      extra_member_actions: nil,
      filter_backends: nil,
      paginator_class: nil,
      page_size: nil,
      page_query_param: 'page',
      page_size_query_param: 'page_size',
      max_page_size: nil,
      serializer_class: nil,
      singleton_controller: nil,
      skip_actions: nil,
    }.each do |a, default|
      unless base.respond_to?(a)
        base.class_attribute(a)

        # Set default manually so we can still support Rails 4. Maybe later we can use the
        # default parameter on `class_attribute`.
        base.send(:"#{a}=", default)
      end
    end

    # Alias `extra_actions` to `extra_collection_actions`.
    unless base.respond_to?(:extra_collection_actions)
      base.alias_method(:extra_collection_actions, :extra_actions)
      base.alias_method(:extra_collection_actions=, :extra_actions=)
    end

    # skip csrf since this is an API
    base.skip_before_action(:verify_authenticity_token) rescue nil

    # handle some common exceptions
    base.rescue_from(ActiveRecord::RecordNotFound, with: :record_not_found)
    base.rescue_from(ActiveRecord::RecordInvalid, with: :record_invalid)
    base.rescue_from(ActiveRecord::RecordNotSaved, with: :record_not_saved)
    base.rescue_from(ActiveRecord::RecordNotDestroyed, with: :record_not_destroyed)
  end
end

Instance Method Details

#rootObject

Default action for API root.



10
11
12
# File 'lib/rest_framework/controller_mixins/base.rb', line 10

def root
  api_response({message: "This is the root of your awesome API!"})
end