Skip to content

Commit

Permalink
Implement notes on new club applications
Browse files Browse the repository at this point in the history
  • Loading branch information
zachlatta committed Jan 31, 2018
1 parent d0bbee1 commit c6ef022
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 1 deletion.
59 changes: 59 additions & 0 deletions api/app/controllers/v1/notes_controller.rb
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
1 change: 1 addition & 0 deletions api/app/models/new_club_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NewClubApplication < ApplicationRecord
has_many :leader_profiles
has_many :users, through: :leader_profiles
belongs_to :point_of_contact, class_name: 'User'
has_many :notes, as: :noteable

geocode_attrs address: :high_school_address,
latitude: :high_school_latitude,
Expand Down
10 changes: 10 additions & 0 deletions api/app/models/note.rb
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
23 changes: 23 additions & 0 deletions api/app/policies/note_policy.rb
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
4 changes: 4 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
post 'add_user'
delete 'remove_user'
post 'submit'

resources :notes, only: %i[index create]
end

resources :notes, only: %i[show update destroy]

resources :leader_profiles, only: %i[show update]

resources :users, only: [] do
Expand Down
13 changes: 13 additions & 0 deletions api/db/migrate/20180129220246_create_notes.rb
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
8 changes: 8 additions & 0 deletions api/db/migrate/20180130224937_add_deleted_at_to_notes.rb
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
16 changes: 15 additions & 1 deletion api/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180128111910) do
ActiveRecord::Schema.define(version: 20180130224937) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -281,6 +281,19 @@
t.index ["point_of_contact_id"], name: "index_new_club_applications_on_point_of_contact_id"
end

create_table "notes", force: :cascade do |t|
t.string "noteable_type"
t.bigint "noteable_id"
t.bigint "user_id"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.index ["deleted_at"], name: "index_notes_on_deleted_at"
t.index ["noteable_type", "noteable_id"], name: "index_notes_on_noteable_type_and_noteable_id"
t.index ["user_id"], name: "index_notes_on_user_id"
end

create_table "projects", id: :serial, force: :cascade do |t|
t.text "title"
t.text "description"
Expand Down Expand Up @@ -355,6 +368,7 @@
add_foreign_key "clubs", "leaders", column: "point_of_contact_id"
add_foreign_key "net_promoter_score_surveys", "leaders"
add_foreign_key "new_club_applications", "users", column: "point_of_contact_id"
add_foreign_key "notes", "users"
add_foreign_key "slack_invite_strategies", "hackbot_teams"
add_foreign_key "slack_invites", "hackbot_teams"
add_foreign_key "slack_invites", "slack_invite_strategies"
Expand Down
10 changes: 10 additions & 0 deletions api/spec/factories/notes.rb
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
13 changes: 13 additions & 0 deletions api/spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,18 @@
user.generate_auth_token!
end
end

factory :user_admin_authed do
after(:build) do |user|
# auth the user
user.generate_login_code!
# make it look like the login code was generated a minute ago
user.login_code_generation -= 1.minute
user.generate_auth_token!

# make them admin
user.make_admin!
end
end
end
end
1 change: 1 addition & 0 deletions api/spec/models/new_club_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
it { should have_many(:leader_profiles) }
it { should have_many(:users).through(:leader_profiles) }
it { should belong_to(:point_of_contact) }
it { should have_many :notes }

it 'requires points of contact to be associated users' do
bad_poc = create(:user)
Expand Down
18 changes: 18 additions & 0 deletions api/spec/models/note_spec.rb
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
Loading

0 comments on commit c6ef022

Please sign in to comment.