Skip to content

Commit

Permalink
Refactor mixins into a namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Jul 7, 2023
1 parent ccf500d commit c8f8c10
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 31 deletions.
1 change: 0 additions & 1 deletion .yardopts
@@ -1,6 +1,5 @@
--private
--protected
lib/**/*.rb
-
LICENSE
README.md
2 changes: 1 addition & 1 deletion lib/rest_framework.rb
Expand Up @@ -179,11 +179,11 @@ def self.features
end
end

require_relative "rest_framework/controller_mixins"
require_relative "rest_framework/engine"
require_relative "rest_framework/errors"
require_relative "rest_framework/filters"
require_relative "rest_framework/generators"
require_relative "rest_framework/mixins"
require_relative "rest_framework/paginators"
require_relative "rest_framework/routers"
require_relative "rest_framework/serializers"
Expand Down
7 changes: 0 additions & 7 deletions lib/rest_framework/controller_mixins.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/rest_framework/mixins.rb
@@ -0,0 +1,7 @@
module RESTFramework::Mixins
end

require_relative "mixins/base_controller_mixin"

require_relative "mixins/bulk_model_controller_mixin"
require_relative "mixins/model_controller_mixin"
@@ -1,11 +1,7 @@
require_relative "../errors"
require_relative "../serializers"
require_relative "../utils"

# 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.
module RESTFramework::BaseControllerMixin
module RESTFramework::Mixins::BaseControllerMixin
RRF_BASE_CONFIG = {
extra_actions: nil,
extra_member_actions: nil,
Expand Down Expand Up @@ -348,3 +344,6 @@ def options
return api_response(self.get_options_metadata)
end
end

# Alias for convenience.
RESTFramework::BaseControllerMixin = RESTFramework::Mixins::BaseControllerMixin
@@ -1,8 +1,8 @@
require_relative "models"
require_relative "model_controller_mixin"

# Mixin for creating records in bulk. This is unique compared to update/destroy because we overload
# the existing `create` action to support bulk creation.
module RESTFramework::BulkCreateModelMixin
module RESTFramework::Mixins::BulkCreateModelMixin
# While bulk update/destroy are obvious because they create new router endpoints, bulk create
# overloads the existing collection `POST` endpoint, so we add a special key to the options
# metadata to indicate bulk create is supported.
Expand Down Expand Up @@ -31,8 +31,11 @@ def create_all!
end
end

# Alias for convenience.
RESTFramework::BulkCreateModelMixin = RESTFramework::Mixins::BulkCreateModelMixin

# Mixin for updating records in bulk.
module RESTFramework::BulkUpdateModelMixin
module RESTFramework::Mixins::BulkUpdateModelMixin
def update_all
records = self.update_all!
serialized_records = self.bulk_serialize(records)
Expand All @@ -56,8 +59,11 @@ def update_all!
end
end

# Alias for convenience.
RESTFramework::BulkUpdateModelMixin = RESTFramework::Mixins::BulkUpdateModelMixin

# Mixin for destroying records in bulk.
module RESTFramework::BulkDestroyModelMixin
module RESTFramework::Mixins::BulkDestroyModelMixin
def destroy_all
if params[:_json].is_a?(Array)
records = self.destroy_all!
Expand All @@ -83,8 +89,11 @@ def destroy_all!
end
end

# Alias for convenience.
RESTFramework::BulkDestroyModelMixin = RESTFramework::Mixins::BulkDestroyModelMixin

# Mixin that includes all the CRUD bulk mixins.
module RESTFramework::BulkModelControllerMixin
module RESTFramework::Mixins::BulkModelControllerMixin
include RESTFramework::ModelControllerMixin

include RESTFramework::BulkCreateModelMixin
Expand All @@ -95,3 +104,6 @@ def self.included(base)
RESTFramework::ModelControllerMixin.included(base)
end
end

# Alias for convenience.
RESTFramework::BulkModelControllerMixin = RESTFramework::Mixins::BulkModelControllerMixin
@@ -1,8 +1,5 @@
require_relative "base"
require_relative "../filters"

# This module provides the core functionality for controllers based on models.
module RESTFramework::BaseModelControllerMixin
module RESTFramework::Mixins::BaseModelControllerMixin
BASE64_REGEX = /data:(.*);base64,(.*)/
BASE64_TRANSLATE = ->(field, value) {
_, content_type, payload = value.match(BASE64_REGEX).to_a
Expand Down Expand Up @@ -634,8 +631,11 @@ def bulk_serialize(records)
end
end

# Alias for convenience.
RESTFramework::BaseModelControllerMixin = RESTFramework::Mixins::BaseModelControllerMixin

# Mixin for listing records.
module RESTFramework::ListModelMixin
module RESTFramework::Mixins::ListModelMixin
def index
return api_response(self.get_index_records)
end
Expand All @@ -662,15 +662,21 @@ def get_index_records
end
end

# Alias for convenience.
RESTFramework::ListModelMixin = RESTFramework::Mixins::ListModelMixin

# Mixin for showing records.
module RESTFramework::ShowModelMixin
module RESTFramework::Mixins::ShowModelMixin
def show
return api_response(self.get_record)
end
end

# Alias for convenience.
RESTFramework::ShowModelMixin = RESTFramework::Mixins::ShowModelMixin

# Mixin for creating records.
module RESTFramework::CreateModelMixin
module RESTFramework::Mixins::CreateModelMixin
def create
return api_response(self.create!, status: :created)
end
Expand All @@ -681,8 +687,11 @@ def create!
end
end

# Alias for convenience.
RESTFramework::CreateModelMixin = RESTFramework::Mixins::CreateModelMixin

# Mixin for updating records.
module RESTFramework::UpdateModelMixin
module RESTFramework::Mixins::UpdateModelMixin
def update
return api_response(self.update!)
end
Expand All @@ -695,8 +704,11 @@ def update!
end
end

# Alias for convenience.
RESTFramework::UpdateModelMixin = RESTFramework::Mixins::UpdateModelMixin

# Mixin for destroying records.
module RESTFramework::DestroyModelMixin
module RESTFramework::Mixins::DestroyModelMixin
def destroy
self.destroy!
return api_response("")
Expand All @@ -708,8 +720,11 @@ def destroy!
end
end

# Alias for convenience.
RESTFramework::DestroyModelMixin = RESTFramework::Mixins::DestroyModelMixin

# Mixin that includes show/list mixins.
module RESTFramework::ReadOnlyModelControllerMixin
module RESTFramework::Mixins::ReadOnlyModelControllerMixin
include RESTFramework::BaseModelControllerMixin

include RESTFramework::ListModelMixin
Expand All @@ -720,8 +735,11 @@ def self.included(base)
end
end

# Alias for convenience.
RESTFramework::ReadOnlyModelControllerMixin = RESTFramework::Mixins::ReadOnlyModelControllerMixin

# Mixin that includes all the CRUD mixins.
module RESTFramework::ModelControllerMixin
module RESTFramework::Mixins::ModelControllerMixin
include RESTFramework::BaseModelControllerMixin

include RESTFramework::ListModelMixin
Expand All @@ -734,3 +752,6 @@ def self.included(base)
RESTFramework::BaseModelControllerMixin.included(base)
end
end

# Alias for convenience.
RESTFramework::ModelControllerMixin = RESTFramework::Mixins::ModelControllerMixin
1 change: 0 additions & 1 deletion lib/rest_framework/routers.rb
@@ -1,5 +1,4 @@
require "action_dispatch/routing/mapper"
require_relative "utils"

module ActionDispatch::Routing
class Mapper
Expand Down
1 change: 1 addition & 0 deletions rest_framework.gemspec
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
"README.md",
"LICENSE",
"VERSION",
".yardopts",
*Dir["app/**/*"],
*Dir["lib/**/*.rb"],
*Dir["vendor/assets/**/*"],
Expand Down

0 comments on commit c8f8c10

Please sign in to comment.