-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
22 changed files
with
538 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
class BloggerAbility < Ability | ||
def initialize(user) | ||
can [:show, :destroy, :update], User, { id: user.id } | ||
can [:create, :destroy, :update], Blog, { user_id: user.id } | ||
super | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Api::V1::BlogsController < Api::V1::ResourceController | ||
skip_before_action :authenticate_user!, only: [:show, :index] | ||
|
||
before_action :set_blogs, only: :index | ||
load_resource only: :show | ||
|
||
with_options only: [:create, :update, :destroy] do | ||
load_and_authorize_resource :user | ||
load_and_authorize_resource :blog, through: :user, shallow: true | ||
end | ||
|
||
private | ||
def set_blogs | ||
@blogs = Blog.all.includes(:author) | ||
end | ||
|
||
def resource_params | ||
params.require(:blog).permit(:title, :description) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Api::V1::ResourceController < Api::V1::BaseController | ||
def index | ||
resources = paginate(self.resources) | ||
render json: collection_serializer_klass.new( | ||
resources, | ||
each_serializer: collection_each_serializer_klass, | ||
root: controller_name, | ||
meta: meta_attributes(resources) | ||
) | ||
end | ||
|
||
def show | ||
render json: resource | ||
end | ||
|
||
def create | ||
if resource.save | ||
render json: resource, status: :created | ||
else | ||
render json: resource.errors.full_messages, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if resource.update(resource_params) | ||
render json: resource, status: :ok | ||
else | ||
render json: resource.errors.full_messages, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
if resource.destroy | ||
render json: { message: 'resource deleted successfully' }, status: :ok | ||
else | ||
render json: resource.errors.full_messages, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
protected | ||
def resource | ||
instance_variable_get("@#{ controller_name.singularize }") | ||
end | ||
|
||
def resources | ||
instance_variable_get("@#{ controller_name }") | ||
end | ||
|
||
def collection_each_serializer_klass | ||
Api::V1.const_get("#{ controller_name.classify }Serializer") | ||
end | ||
|
||
def collection_serializer_klass | ||
ActiveModel::ArraySerializer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Blog < ApplicationRecord | ||
# Associations | ||
belongs_to :author, foreign_key: :user_id, class_name: :User | ||
|
||
# Validations | ||
validates :title, :description, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Api::V1::BlogSerializer < ActiveModel::Serializer | ||
attributes :id, :title, :description | ||
|
||
has_one :author, serializer: UserSerializer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# https://github.com/rails/rails/pull/22830 | ||
module ActionController | ||
class Parameters | ||
delegate :include?, to: :@parameters | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
|
||
# Serve websocket cable requests in-process | ||
# mount ActionCable.server => '/cable' | ||
|
||
namespace :api do | ||
namespace :v1 do | ||
controller :sessions do | ||
post :login, action: :create | ||
end | ||
resources :users, only: [:index, :create, :show, :update, :destroy] | ||
|
||
resources :users, only: [:index, :create, :show, :update, :destroy] do | ||
resources :blogs, only: [:create, :show, :update, :destroy], shallow: true | ||
end | ||
resources :blogs, only: [:index] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateBlogs < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :blogs do |t| | ||
t.references :user, index: true, foreign_key: true | ||
t.string :title | ||
t.text :description | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.