Skip to content

Commit

Permalink
modify scope with openstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
iannono committed Aug 26, 2013
1 parent 26ca077 commit 24ba966
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery

serialization_scope :view_context

private

def current_user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ class ArticleSerializer < ActiveModel::Serializer
has_many :comments
embed :ids, include: true

delegate :current_user, to: :scope

def url
article_url(object)
end

def attributes
data = super
data[:edit_url] = edit_article_url(object) if current_user.admin?
data
end
end
7 changes: 7 additions & 0 deletions xiongbo/409-active-model-serializers/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# rails casts practice --409 active model serializers

## resources
1. [OpenStruct](http://www.ruby-doc.org/stdlib-2.0/libdoc/ostruct/rdoc/OpenStruct.html)

## tips
1. you can custom root name by `{ render json: @article, root: :article}`

## differences
1. scope.admin? did not throw the exception

0 comments on commit 24ba966

Please sign in to comment.