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



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
73
74
75
76
77
78
79
80
81
82
# File 'lib/rest_framework/controller_mixins/base.rb', line 31

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

    # Add class attributes (with defaults) unless they already exist.
    {
      filter_pk_from_request_body: true,
      exclude_body_fields: [:created_at, :created_by, :updated_at, :updated_by],
      accept_generic_params_as_body_params: true,
      extra_actions: nil,
      extra_member_actions: nil,
      filter_backends: nil,
      paginator_class: nil,
      page_size: 20,
      page_query_param: "page",
      page_size_query_param: "page_size",
      max_page_size: nil,
      serializer_class: nil,
      serialize_to_json: true,
      serialize_to_xml: true,
      singleton_controller: nil,
      skip_actions: nil,
    }.each do |a, default|
      next if 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

    # 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.
    begin
      base.skip_before_action(:verify_authenticity_token)
    rescue
      nil
    end

    # 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