From 4b474ef8f2c7d926ce42f2bef6a72b29c3f19fc0 Mon Sep 17 00:00:00 2001 From: Kyle Passarelli Date: Mon, 7 Oct 2013 18:20:06 -0600 Subject: [PATCH 1/2] remove '.DS_Store's --- .DS_Store | Bin 6148 -> 0 bytes spec/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store delete mode 100644 spec/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 25eddca5b73bd7b962dbd04aaf860a2b32db32ad..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO-sW-5Z!H~ZvB8PvR_hoOj-6T3g#YLhOqD(J-Dx>B>u&vnadur|HU%qe&Pn z?iA01e3^N{LV0e2d6;U=$LYS})u7e0k0aAbzAO%PPrGPmg#2)EE z3XlS%z&8bOe~?fRU4xZIwRJ#+*Jt!wh-kp^ErBQvx&|wa-~r=06;P*g^Tgmf9r~q- za}8D+bvol}W*EoJTt8m8njQM33TNEaNHr-y3al&8)J+@D|10=qHa_y#Q>aA>kOKcq z0dDpEz6XmkXX}sU;aMx8JU~IixC#jv@U=?-G;kj|R!;4g$RW-(SZTyjuwSJE(nUZC LLKP|S3krMyY$r!Q diff --git a/spec/.DS_Store b/spec/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Mon, 7 Oct 2013 19:30:20 -0600 Subject: [PATCH 2/2] add support for 'current_user' --- README.md | 31 +++++++++++++++++++ .../endpoint_extension.rb | 13 ++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7dfa5e2..a5b8026 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,37 @@ namespace 'foo', :serializer => :bar do end ``` +### current_user + +One of the nice features of ActiveModel::Serializers is that it +provides access to the authorization context via the `current_user`. + +In Grape, you can get the same behavior by defining a `current_user` +helper method: + +```ruby + helpers do + def current_user + @current_user ||= User.where( :access_token => params[:token]).first + end + + def authenticate! + error!('401 Unauthenticated', 401) unless current_user + end + end +``` + +Then, in your serializer, you could show or hide some elements +based on the current user's permissions: + +```ruby +class PostSerializer < ActiveModel::Serializer +... + def include_admin_comments? + current_user.roles.member? :admin + end +end +``` ### Full Example diff --git a/lib/grape-active_model_serializers/endpoint_extension.rb b/lib/grape-active_model_serializers/endpoint_extension.rb index dac398f..58bad83 100644 --- a/lib/grape-active_model_serializers/endpoint_extension.rb +++ b/lib/grape-active_model_serializers/endpoint_extension.rb @@ -16,9 +16,18 @@ module Grape options[:route_options] end + def self.included(base) + mattr_accessor :_serialization_scope + self._serialization_scope = :current_user + + base.class_eval do + def serialization_scope + send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true) + end + end + end + def default_serializer_options; end - def serialization_scope; end - def _serialization_scope; end def url_options; end end