Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/restpack_serializer/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module RestPack::Serializer
class Options
attr_accessor :page, :page_size, :include, :filters, :serializer,
:model_class, :scope, :context, :include_links,
:sorting
:sorting, :key

def initialize(serializer, params = {}, scope = nil, context = {})
def initialize(serializer, params = {}, scope = nil, context = {}, key = nil)
params.symbolize_keys! if params.respond_to?(:symbolize_keys!)

@page = params[:page] ? params[:page].to_i : 1
Expand Down
9 changes: 6 additions & 3 deletions lib/restpack_serializer/serializable/paging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ def page_with_options(options)
page = page.reorder(options.sorting) if options.sorting.any?

result = RestPack::Serializer::Result.new
result.resources[self.key] = serialize_page(page, options)
result.meta[self.key] = serialize_meta(page, options)
result.resources[options.key || self.key] = serialize_page(page, options)
result.meta[options.key || self.key] = serialize_meta(page, options)

if options.include_links
result.links = self.links
Array(RestPack::Serializer::Factory.create(*options.include)).each do |serializer|
options.include.each do |inclusion|
association = model_class.reflect_on_association inclusion
class_name = association ? association.class_name : inclusion
serializer = RestPack::Serializer::Factory.create class_name
result.links.merge! serializer.class.links
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def side_load_belongs_to

def side_load_has_many
has_association_relation do |options|
options.key = @association.name
if join_table = @association.options[:through]
options.scope = options.scope.joins(join_table).distinct
association_fk = @association.through_reflection.foreign_key.to_sym
Expand All @@ -30,7 +31,7 @@ def side_load_has_many
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}"
join_clause = "join #{join_table_name} on #{@association.klass.table_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 } }
Expand Down
7 changes: 3 additions & 4 deletions lib/restpack_serializer/serializable/side_loading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ def links
associations.each do |association|
if association.macro == :belongs_to
link_key = "#{self.key}.#{association.name}"
href = "/#{association.plural_name}/{#{link_key}}"
href = "/#{association.class_name.underscore.pluralize}/{#{link_key}}"
elsif association.macro.to_s.match(/has_/)
singular_key = self.key.to_s.singularize
link_key = "#{self.key}.#{association.plural_name}"
href = "/#{association.plural_name}?#{singular_key}_id={#{key}.id}"
href = "/#{association.class_name.underscore.pluralize}?#{association.foreign_key}={#{key}.id}"
end

links.merge!(link_key => {
:href => href_prefix + href,
:type => association.plural_name.to_sym
:type => association.class_name.underscore.pluralize.to_sym
}
)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Artist < ActiveRecord::Base
attr_accessible :name, :website

has_many :albums
has_many :records, class_name: 'Album'
has_many :songs
has_many :payments
has_many :fans, :through => :payments
Expand All @@ -95,6 +96,7 @@ class Song < ActiveRecord::Base
attr_accessible :title, :artist, :album

belongs_to :artist
belongs_to :record, class_name: 'Album', foreign_key: 'album_id'
belongs_to :album
end

Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MyApp
class SongSerializer
include RestPack::Serializer
attributes :id, :title, :album_id
can_include :albums, :artists
can_include :albums, :records, :artists
can_filter_by :title
can_sort_by :id, :title

Expand All @@ -27,7 +27,7 @@ class AlbumReviewSerializer
class ArtistSerializer
include RestPack::Serializer
attributes :id, :name, :website
can_include :albums, :songs, :fans, :stalkers
can_include :albums, :records, :songs, :fans, :stalkers
end

class FanSerializer
Expand Down
2 changes: 2 additions & 0 deletions spec/serializable/paging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
it "includes links" do
page[:links].should == {
'songs.album' => { :href => "/albums/{songs.album}", :type => :albums },
'songs.record' => { :href => "/albums/{songs.record}", :type => :albums },
'songs.artist' => { :href => "/artists/{songs.artist}", :type => :artists }
}
end
Expand Down Expand Up @@ -99,6 +100,7 @@
album_id: first.album_id,
links: {
album: first.album_id.to_s,
record: first.album_id.to_s,
artist: first.artist_id.to_s
}
}
Expand Down
1 change: 1 addition & 0 deletions spec/serializable/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def custom_attributes
json = serializer.as_json(@album1.songs.first)
json[:links].should == {
artist: @album1.artist_id.to_s,
record: @album1.id.to_s,
album: @album1.id.to_s
}
end
Expand Down
12 changes: 12 additions & 0 deletions spec/serializable/side_loading/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
end
end

context 'with a renamed association' do
let(:models) { [MyApp::Song.first] }
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "records" }) }

it 'should side-load records' do
side_loads.should == {
records: [MyApp::AlbumSerializer.as_json(MyApp::Song.first.album)],
meta: { }
}
end
end

context 'without an associated model' do
let!(:b_side) { FactoryGirl.create(:song, album: nil) }
let(:models) { [b_side] }
Expand Down
11 changes: 11 additions & 0 deletions spec/serializable/side_loading/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
end
end
end

context 'with a model renaming an association' do
let(:models){ [@artist1] }
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "records" }) }

it 'should return side-loaded records' do
side_loads[:records].count.should == @artist1.albums.count
side_loads[:meta][:records][:page].should == 1
side_loads[:meta][:records][:count].should == @artist1.albums.count
end
end
end

describe '.has_many through' do
Expand Down