forked from hackclub/api
-
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.
Implement notes on new club applications
- Loading branch information
Showing
13 changed files
with
375 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module V1 | ||
class NotesController < ApiController | ||
include UserAuth | ||
|
||
before_action :find_application, only: %i[index create] | ||
before_action :find_note, only: %i[show update destroy] | ||
before_action :authorize_note, only: %i[show update destroy] | ||
|
||
def index | ||
authorize @app.notes | ||
|
||
render_success @app.notes | ||
end | ||
|
||
def create | ||
note = @app.notes.new(user: current_user, body: params[:body]) | ||
authorize note | ||
|
||
if note.save | ||
render_success note, 201 | ||
else | ||
render_field_errors note.errors | ||
end | ||
end | ||
|
||
def show | ||
render_success @note | ||
end | ||
|
||
def update | ||
if @note.update_attributes(body: params[:body]) | ||
render_success @note | ||
else | ||
render_field_errors @note.errors | ||
end | ||
end | ||
|
||
def destroy | ||
@note.destroy | ||
render_success @note | ||
end | ||
|
||
private | ||
|
||
def find_application | ||
@app = NewClubApplication.find(params[:new_club_application_id]) | ||
end | ||
|
||
def find_note | ||
@note = Note.find(params[:id]) | ||
end | ||
|
||
def authorize_note | ||
authorize @note | ||
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
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,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class Note < ApplicationRecord | ||
acts_as_paranoid | ||
|
||
belongs_to :noteable, polymorphic: true | ||
belongs_to :user | ||
|
||
validates :user, :body, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class NotePolicy < ApplicationPolicy | ||
def index? | ||
user.admin? | ||
end | ||
|
||
def create? | ||
user.admin? | ||
end | ||
|
||
def show? | ||
user.admin? | ||
end | ||
|
||
def update? | ||
user.admin? && record.user == user | ||
end | ||
|
||
def destroy? | ||
user.admin? && record.user == user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateNotes < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :notes do |t| | ||
t.references :noteable, polymorphic: true, index: true | ||
t.references :user, foreign_key: true | ||
t.text :body | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddDeletedAtToNotes < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :notes, :deleted_at, :datetime | ||
add_index :notes, :deleted_at | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :note do | ||
association :user, factory: :user_admin_authed | ||
association :noteable, factory: :new_club_application | ||
|
||
body { Faker::Lorem.sentence } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Note, type: :model do | ||
it { should have_db_column :created_at } | ||
it { should have_db_column :updated_at } | ||
it { should have_db_column :noteable_type } | ||
it { should have_db_column :noteable_id } | ||
it { should have_db_column :user_id } | ||
it { should have_db_column :body } | ||
|
||
it { should belong_to :noteable } | ||
it { should belong_to :user } | ||
|
||
it { should validate_presence_of :user } | ||
it { should validate_presence_of :body } | ||
end |
Oops, something went wrong.