Skip to content

Commit

Permalink
Add flags and constraints to enable default boards and discussions fo…
Browse files Browse the repository at this point in the history
…r subjects
  • Loading branch information
Michael Parrish committed Jun 25, 2015
1 parent cd0702e commit a553e5b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/models/discussion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Discussion < ActiveRecord::Base
belongs_to :board, required: true, counter_cache: true
has_many :comments, dependent: :destroy

validates :title, presence: true, length: 3..140
validates :title, presence: true, length: { in: 3..140, unless: ->{ subject_default? } }
validates :section, presence: true

before_validation :set_section
Expand Down
8 changes: 5 additions & 3 deletions app/schemas/board_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def create
string :title, required: true
string :description, required: true
string :section, required: true
boolean :subject_default
entity :parent_id do
one_of integer, null
end
Expand All @@ -19,9 +20,10 @@ def create
def update
root do |root_object|
additional_properties false
string :title
string :description
entity :parent_id do
string :title
string :description
boolean :subject_default
entity :parent_id do
one_of integer, null
end
permissions root_object
Expand Down
7 changes: 4 additions & 3 deletions app/schemas/discussion_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class DiscussionSchema
def create
root do |root_object|
additional_properties false
string :title, required: true, min_length: 3, max_length: 140
integer :board_id, required: true
integer :user_id, required: true
string :title, required: true, min_length: 3, max_length: 140
integer :board_id, required: true
integer :user_id, required: true
boolean :subject_default
sticky root_object

array :comments, required: true, min_items: 1 do
Expand Down
1 change: 1 addition & 0 deletions app/serializers/board_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class BoardSerializer
include TalkSerializer
all_attributes
can_include :discussions, :parent, :sub_boards
can_filter_by :subject_default
can_sort_by :created_at
self.default_sort = 'created_at'
end
2 changes: 1 addition & 1 deletion app/serializers/discussion_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DiscussionSerializer

all_attributes
can_include :comments, :board, :user
can_filter_by :sticky
can_filter_by :title, :subject_default, :sticky
can_sort_by :updated_at, :sticky, :sticky_position
self.default_sort = '-sticky,sticky_position,-updated_at'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddSubjectDefaultToBoardsAndDiscussions < ActiveRecord::Migration
def change
add_column :boards, :subject_default, :boolean, null: false, default: false
add_column :discussions, :subject_default, :boolean, null: false, default: false

add_index :boards, [:section, :subject_default], unique: true, where: 'subject_default = true'
add_index :discussions, [:board_id, :title, :subject_default], unique: true, where: 'subject_default = true'
end
end
12 changes: 8 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150624172525) do
ActiveRecord::Schema.define(version: 20150625215239) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -31,20 +31,22 @@
add_index "announcements", ["section", "created_at"], name: "index_announcements_on_section_and_created_at", using: :btree

create_table "boards", force: :cascade do |t|
t.string "title", null: false
t.string "description", null: false
t.string "section", null: false
t.string "title", null: false
t.string "description", null: false
t.string "section", null: false
t.integer "users_count", default: 0
t.integer "comments_count", default: 0
t.integer "discussions_count", default: 0
t.json "permissions", default: {}
t.datetime "created_at"
t.datetime "updated_at"
t.integer "parent_id"
t.boolean "subject_default", default: false, null: false
end

add_index "boards", ["parent_id", "created_at"], name: "index_boards_on_parent_id_and_created_at", using: :btree
add_index "boards", ["section", "created_at"], name: "index_boards_on_section_and_created_at", using: :btree
add_index "boards", ["section", "subject_default"], name: "index_boards_on_section_and_subject_default", unique: true, where: "(subject_default = true)", using: :btree

create_table "comments", force: :cascade do |t|
t.string "category"
Expand Down Expand Up @@ -90,10 +92,12 @@
t.datetime "created_at"
t.datetime "updated_at"
t.float "sticky_position"
t.boolean "subject_default", default: false, null: false
end

add_index "discussions", ["board_id", "sticky", "sticky_position"], name: "index_discussions_on_board_id_and_sticky_and_sticky_position", where: "(sticky = true)", using: :btree
add_index "discussions", ["board_id", "sticky", "updated_at"], name: "index_discussions_on_board_id_and_sticky_and_updated_at", using: :btree
add_index "discussions", ["board_id", "title", "subject_default"], name: "index_discussions_on_board_id_and_title_and_subject_default", unique: true, where: "(subject_default = true)", using: :btree
add_index "discussions", ["board_id", "updated_at"], name: "index_discussions_on_board_id_and_updated_at", using: :btree

create_table "mentions", force: :cascade do |t|
Expand Down
16 changes: 16 additions & 0 deletions spec/models/board_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
end
end

describe '#subject_default' do
let!(:default_board){ create :board, section: 'project-1', subject_default: true }

it 'should not allow other default boards in the section' do
expect {
create :board, section: 'project-1', subject_default: true
}.to raise_error ActiveRecord::RecordNotUnique
end

it 'should be scoped to section' do
expect {
create :board, section: 'project-2', subject_default: true
}.to_not raise_error
end
end

describe '#cascade_searchable' do
let(:board){ create :board_with_discussions, discussion_count: 2 }
let(:children){ board.discussions.all.to_a + board.comments.all.to_a }
Expand Down
24 changes: 24 additions & 0 deletions spec/models/discussion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@
end
end

describe '#subject_default' do
let(:board){ create :board }
let(:subject){ create :subject }
let!(:default_discussion){ create :discussion, title: subject.id, board: board, subject_default: true }

it 'should not allow other default discussions for the subject on the board' do
expect {
create :discussion, title: subject.id, board: board, subject_default: true
}.to raise_error ActiveRecord::RecordNotUnique
end

it 'should be scoped to the board' do
expect {
create :discussion, title: subject.id, subject_default: true
}.to_not raise_error
end

it 'should be scoped to the title' do
expect {
create :discussion, board: board, title: create(:subject).id, subject_default: true
}.to_not raise_error
end
end

context 'creating' do
it 'should set the section' do
discussion = build :discussion, section: nil
Expand Down
1 change: 1 addition & 0 deletions spec/schemas/board_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
its(:title){ is_expected.to eql type: 'string' }
its(:description){ is_expected.to eql type: 'string' }
its(:parent_id){ is_expected.to eql oneOf: [{ 'type' => 'integer' }, 'type' => 'null' ] }
its(:subject_default){ is_expected.to eql type: 'boolean' }

with :permissions do
its(:type){ is_expected.to eql 'object' }
Expand Down
4 changes: 4 additions & 0 deletions spec/schemas/discussion_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
with 'properties .discussions' do
its(:required){ is_expected.to eql %w(title board_id user_id comments) }

with :properties do
its(:subject_default){ is_expected.to eql type: 'boolean' }
end

with 'properties .comments' do
its(:type){ is_expected.to eql 'array' }
its(:minItems){ is_expected.to eql 1 }
Expand Down

0 comments on commit a553e5b

Please sign in to comment.