forked from RestPack/restpack_serializer
-
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.
Merge pull request RestPack#1 from camallen/include_links
add support for has_an_belongs_to_many relations
- Loading branch information
Showing
8 changed files
with
204 additions
and
90 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
56 changes: 56 additions & 0 deletions
56
lib/restpack_serializer/serializable/side_load_data_builder.rb
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 @@ | ||
module RestPack | ||
module Serializer | ||
class SideLoadDataBuilder | ||
|
||
def initialize(association, models, serializer) | ||
@association = association | ||
@models = models | ||
@serializer = serializer | ||
end | ||
|
||
def side_load_belongs_to | ||
foreign_keys = @models.map { |model| model.send(@association.foreign_key) }.uniq | ||
side_load = @association.klass.find(foreign_keys) | ||
json_model_data = side_load.map { |model| @serializer.as_json(model) } | ||
{ @association.plural_name.to_sym => json_model_data, meta: { } } | ||
end | ||
|
||
def side_load_has_many | ||
has_association_relation do |options| | ||
if join_table = @association.options[:through] | ||
options.scope = options.scope.joins(join_table) | ||
association_fk = @association.through_reflection.foreign_key.to_sym | ||
options.filters = { join_table => { association_fk => model_ids } } | ||
else | ||
options.filters = { @association.foreign_key.to_sym => model_ids } | ||
end | ||
end | ||
end | ||
|
||
def side_load_has_and_belongs_to_many | ||
has_association_relation do |options| | ||
join_table_name = @association.join_table | ||
join_clause = "join #{join_table_name} on #{@association.plural_name}.id = #{join_table_name}.#{@association.class_name.foreign_key}" | ||
options.scope = options.scope.joins(join_clause) | ||
association_fk = @association.foreign_key.to_sym | ||
options.filters = { join_table_name.to_sym => { association_fk => model_ids } } | ||
end | ||
end | ||
|
||
private | ||
|
||
def model_ids | ||
@models.map(&:id) | ||
end | ||
|
||
def has_association_relation | ||
return {} if @models.empty? | ||
serializer_class = @serializer.class | ||
options = RestPack::Serializer::Options.new(serializer_class) | ||
yield options | ||
options.include_links = false | ||
serializer_class.page_with_options(options) | ||
end | ||
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
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
44 changes: 44 additions & 0 deletions
44
spec/serializable/side_loading/has_and_belongs_many_spec.rb
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,44 @@ | ||
require 'spec_helper' | ||
|
||
describe RestPack::Serializer::SideLoading do | ||
context "when side-loading" do | ||
let(:side_loads) { MyApp::ArtistSerializer.side_loads(models, options) } | ||
|
||
describe ".has_and_belongs_to_many" do | ||
|
||
before(:each) do | ||
@artist1 = FactoryGirl.create(:artist_with_stalkers, stalker_count: 2) | ||
@artist2 = FactoryGirl.create(:artist_with_stalkers, stalker_count: 3) | ||
end | ||
|
||
context "with a single model" do | ||
let(:models) { [@artist1] } | ||
|
||
context "when including :albums" do | ||
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "stalkers" }) } | ||
let(:stalker_count) { @artist1.stalkers.count } | ||
|
||
it "returns side-loaded albums" do | ||
side_loads[:stalkers].count.should == stalker_count | ||
side_loads[:meta][:stalkers][:page].should == 1 | ||
side_loads[:meta][:stalkers][:count].should == stalker_count | ||
end | ||
end | ||
end | ||
|
||
context "with two models" do | ||
let(:models) { [@artist1, @artist2] } | ||
|
||
context "when including :albums" do | ||
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "stalkers" }) } | ||
let(:stalker_count) { @artist1.stalkers.count + @artist2.stalkers.count } | ||
|
||
it "returns side-loaded albums" do | ||
side_loads[:stalkers].count.should == stalker_count | ||
side_loads[:meta][:stalkers][:count].should == stalker_count | ||
end | ||
end | ||
end | ||
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