Skip to content

Commit

Permalink
Side-loadable board hierarchy. Closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Parrish committed Feb 11, 2015
1 parent dcebc2d commit 8e2b8b2
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gem 'rails', '~> 4.2.0'
gem 'rack-cors', '~> 0.2.9'
gem 'pg'
gem 'restpack_serializer', '~> 0.5.6'
gem 'restpack_serializer', github: 'parrish/restpack_serializer', branch: 'dev'
gem 'json-schema', '~> 2.5'
gem 'json-schema_builder', '~> 0.0.5'
gem 'faraday', '~> 0.9'
Expand Down
18 changes: 12 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
GIT
remote: git://github.com/parrish/restpack_serializer.git
revision: f2915cfcaba3abc20b87c7cc47f140483c55db2c
branch: dev
specs:
restpack_serializer (0.5.9)
activerecord (>= 4.0.3, < 5.0)
activesupport (>= 4.0.3, < 5.0)
kaminari (~> 0.16.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -85,7 +95,7 @@ GEM
json-schema_builder (0.0.5)
activesupport (~> 4.0)
json-schema (~> 2.5)
kaminari (0.16.1)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
listen (2.8.4)
Expand Down Expand Up @@ -149,10 +159,6 @@ GEM
ffi (>= 0.5.0)
rdoc (4.2.0)
json (~> 1.4)
restpack_serializer (0.5.6)
activerecord (>= 4.0.3, < 5.0)
activesupport (>= 4.0.3, < 5.0)
kaminari (~> 0.16.1)
rspec (3.1.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
Expand Down Expand Up @@ -227,7 +233,7 @@ DEPENDENCIES
pundit (~> 0.3)
rack-cors (~> 0.2.9)
rails (~> 4.2.0)
restpack_serializer (~> 0.5.6)
restpack_serializer!
rspec-its
rspec-rails
sdoc (~> 0.4)
Expand Down
4 changes: 2 additions & 2 deletions app/models/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Board < ActiveRecord::Base
has_many :discussions, dependent: :restrict_with_error
has_many :comments, through: :discussions
has_many :users, through: :comments
has_many :boards
belongs_to :board
has_many :sub_boards, class_name: 'Board', foreign_key: 'parent_id'
belongs_to :parent, class_name: 'Board'

validates :title, presence: true
validates :description, presence: true
Expand Down
4 changes: 2 additions & 2 deletions app/schemas/board_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def create
string :title, required: true
string :description, required: true
string :section, required: true
entity :board_id do
entity :parent_id do
one_of integer, null
end
end
Expand All @@ -20,7 +20,7 @@ def update
additional_properties false
string :title
string :description
entity :board_id do
entity :parent_id do
one_of integer, null
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/board_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BoardSerializer
include TalkSerializer
all_attributes except: :permissions
can_include :discussions
can_include :discussions, :parent, :sub_boards
end
22 changes: 0 additions & 22 deletions config/initializers/restpack_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,6 @@ def side_load_belongs_to
{ @association.plural_name.to_sym => json_model_data, meta: { } }
end
end

def add_links(model, data)
self.class.associations.each do |association|
data[:links] ||= {}
links_value = case
when association.macro == :belongs_to && association.polymorphic?
model.send association.name
when association.macro == :belongs_to
model.send(association.foreign_key).try(:to_s)
when association.macro.to_s.match(/has_/)
if model.send(association.name).loaded?
model.send(association.name).collect { |associated| associated.id.to_s }
else
model.send(association.name).pluck(:id).map(&:to_s)
end
end
unless links_value.blank?
data[:links][association.name.to_sym] = links_value
end
end
data
end
end

# preload autoloaded serializers
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20150206185515_rename_board_parent_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameBoardParentKey < ActiveRecord::Migration
def change
rename_column :boards, :board_id, :parent_id
end
end
4 changes: 2 additions & 2 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: 20150204190434) do
ActiveRecord::Schema.define(version: 20150206185515) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -27,7 +27,7 @@
t.json "permissions", default: {}
t.datetime "created_at"
t.datetime "updated_at"
t.integer "board_id"
t.integer "parent_id"
end

create_table "comments", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/boards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

after :create do |board, evaluator|
create_list :board, evaluator.subboard_count, board: board
create_list :board, evaluator.subboard_count, parent: board
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/schemas/board_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
with :properties do
its(:title){ is_expected.to eql type: 'string' }
its(:description){ is_expected.to eql type: 'string' }
its(:board_id){ is_expected.to eql oneOf: [{ 'type' => 'integer' }, 'type' => 'null' ] }
its(:parent_id){ is_expected.to eql oneOf: [{ 'type' => 'integer' }, 'type' => 'null' ] }
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/serializers/board_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'spec_helper'

RSpec.describe BoardSerializer, type: :serializer do
it_behaves_like 'a talk serializer', exposing: :all, excluding: [:permissions], including: [:discussions] do
let(:model_instance){ create :board_with_subboards }
it_behaves_like 'a talk serializer', exposing: :all, excluding: [:permissions], including: [:discussions, :parent, :sub_boards] do
let(:parent){ create :board }
let(:model_instance){ create :board_with_subboards, parent: parent }
end
end
4 changes: 2 additions & 2 deletions spec/services/board_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
title: 'works',
description: 'works',
section: 'test',
board_id: parent_board.id
parent_id: parent_board.id
}
}
end
Expand All @@ -23,7 +23,7 @@
boards: {
title: 'new title',
description: 'new description',
board_id: nil
parent_id: nil
}
}
end
Expand Down

0 comments on commit 8e2b8b2

Please sign in to comment.