Skip to content

Commit

Permalink
Excluding attributes from serializers. Towards #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Parrish committed Jan 16, 2015
1 parent 372334b commit 49cc356
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
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
all_attributes except: :permissions
can_include :discussions
end
7 changes: 4 additions & 3 deletions app/serializers/concerns/talk_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module TalkSerializer
end

module ClassMethods
def all_attributes
attrs = model_class.attribute_names rescue []
attributes *attrs
def all_attributes(except: [])
attrs = model_class.attribute_names.sort rescue []
except = Array.wrap(except).sort.collect &:to_s
attributes *(attrs.sort - except)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class UserSerializer
include TalkSerializer
all_attributes
all_attributes except: :email
end
2 changes: 1 addition & 1 deletion spec/serializers/board_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'

RSpec.describe BoardSerializer, type: :serializer do
it_behaves_like 'a talk serializer', exposing: :all, including: [:discussions]
it_behaves_like 'a talk serializer', exposing: :all, excluding: [:permissions], including: [:discussions]
end
2 changes: 1 addition & 1 deletion spec/serializers/user_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'

RSpec.describe UserSerializer, type: :serializer do
it_behaves_like 'a talk serializer', exposing: :all
it_behaves_like 'a talk serializer', exposing: :all, excluding: [:email]
end
9 changes: 7 additions & 2 deletions spec/support/shared_examples_for_talk_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
let(:json){ serializer.as_json model_instance }
end

RSpec.shared_examples_for 'a talk serializer' do |exposing: nil, including: nil|
RSpec.shared_examples_for 'a talk serializer' do |exposing: nil, excluding: [], including: nil|
[exposing, excluding, including].compact.each{ |opt| opt.map!(&:to_sym) if opt.is_a?(Array) }
include_context 'a serializer'
let(:model_instance){ defined?(object) ? object : instance }

Expand All @@ -17,14 +18,18 @@

if exposing == :all
described_class.model_class.attribute_names.each do |name|
it{ is_expected.to include name.to_sym }
it{ is_expected.to include name.to_sym } unless name.to_sym.in?(excluding)
end
elsif exposing
exposing.each do |name|
it{ is_expected.to include name.to_sym }
end
end

excluding.each do |name|
it{ is_expected.to_not include name.to_sym }
end

it{ is_expected.to include :href }
end

Expand Down

0 comments on commit 49cc356

Please sign in to comment.