2017-11-06 7 views
1

が含まれます。Railsの評論家のpolicy_scopeはで私の前の質問を続けフィールド

など。 UserATargonというストーリーを作成し、2つの公開された章と2つの未公開の章を提供する場合、UserBTargonストーリーの公開された章のみを表示する必要があります。

通常、Punditのポリシースコープでは、index CRUDアクションのスコープが設定されています。私が試してみました

render json: story, include: [:user, :chapters], status: :ok 

::私はスコープに必要なもの

は、しかし、レンダリングJSONライン中Storyに属する​​ある

# --------------------------------------------------------------------------- 
# ActiveRecord auto-save will kick in and delete all unpublished chapters 
# --------------------------------------------------------------------------- 
story.chapters = policy_scope(story.chapters) 

render json: story, include: [:user, :chapters], status: :ok 

https://gist.github.com/demisx/9896113(has_manyのセクション)上記によると、コードを再割り当てするとTargonに属する未公開のチャプタがすべて削除されますstory.chapters

story.chapters = policy_scope(story.chapters) # BAD 

私はこのような何か行うことができますいくつかの方法があり期待しています:ID 16(Targon)とストーリーのためにフェッチstory.chaptersすべてのユーザーが得るスコープなしで、現時点では

render json: story, include: [:user, policy_scope(:chapters)], status: :ok 

は、バックJSONAPI:

{ 
    "data": { 
     "id": "16", 
     "type": "stories", 
     "attributes": { 
      "title": "Mount Targon", 
      "summary": "Mount Targon is the mightiest peak in Runeterra, a towering peak of sun-baked rock amid a range of summits unmatched in scale anywhere else in the world. Located far from civilization, Mount Targon is utterly remote and all but impossible to reach save by the most determined seeker. Many legends cling to Mount Targon, and, like any place of myth, it is a beacon to dreamers, madmen and questors of adventure. Some of these brave souls attempt to scale the impossible mountain, perhaps seeking wisdom or enlightenment, perhaps chasing glory or some soul-deep yearning to witness its summit. The ascent is all but impossible, and those hardy few who somehow survive to reach the top almost never speak of what they have seen. Some return with a haunted, empty look in their eyes, others changed beyond all recognition, imbued by an Aspect of unearthly, inhuman power with a destiny few mortals can comprehend.", 
      "published": true, 
      "published-date": "2017-11-02T10:35:33.184Z", 
      "created-at": "2017-11-02T10:35:33.184Z", 
      "updated-at": "2017-11-04T07:35:04.083Z", 
      "cover": { 
       "url": "http://res.cloudinary.com/chewedon/image/upload/v1509780931/c8ubn3tfivxziyxwynsa.png", 
       "standard": { 
        "url": "http://res.cloudinary.com/chewedon/image/upload/c_fill,g_north,h_300,w_200/c8ubn3tfivxziyxwynsa.png" 
       } 
      } 
     }, 
     "relationships": { 
      "user": { 
       "data": { 
        "id": "1", 
        "type": "users" 
       } 
      }, 
      "chapters": { 
       "data": [{ 
        "id": "26", 
        "type": "chapters" 
       }, { 
        "id": "27", 
        "type": "chapters" 
       }, { 
        "id": "37", 
        "type": "chapters" 
       }, { 
        "id": "38", 
        "type": "chapters" 
       }] 
      } 
     } 
    }, 
    "included": [{ 
     "id": "1", 
     "type": "users", 
     "attributes": { 
      "username": "Chewedon", 
      "photo": { 
       "url": "http://res.cloudinary.com/chewedon/image/upload/v1509857442/nx1tqlcdxrhz6r3kjx87.jpg", 
       "standard": { 
        "url": "http://res.cloudinary.com/chewedon/image/upload/c_fill,g_north,h_150,w_150/nx1tqlcdxrhz6r3kjx87.jpg" 
       } 
      } 
     }, 
     "relationships": { 
      "stories": { 
       "data": [{ 
        "id": "1", 
        "type": "stories" 
       }, { 
        "id": "2", 
        "type": "stories" 
       }, { 
        "id": "3", 
        "type": "stories" 
       }, { 
        "id": "4", 
        "type": "stories" 
       }, { 
        "id": "5", 
        "type": "stories" 
       }, { 
        "id": "6", 
        "type": "stories" 
       }, { 
        "id": "8", 
        "type": "stories" 
       }, { 
        "id": "9", 
        "type": "stories" 
       }, { 
        "id": "10", 
        "type": "stories" 
       }, { 
        "id": "11", 
        "type": "stories" 
       }, { 
        "id": "12", 
        "type": "stories" 
       }, { 
        "id": "13", 
        "type": "stories" 
       }, { 
        "id": "14", 
        "type": "stories" 
       }, { 
        "id": "15", 
        "type": "stories" 
       }, { 
        "id": "16", 
        "type": "stories" 
       }] 
      } 
     } 
    }] 
} 

ここでは関係セクションでは、各章3738が私のエムに禁断の403につながる、未発表ですberフロントエンド。

理想的には、レコードを返す前にサーバがこれらのスコープを設定していなければなりませんが、私は前述のStackoverflowの質問でバグがあったため、Punditのフィールドのスコープを取りません。

アイデア? (私はあなたがそれを行うことができます知らなかった)Storyシリアライザのchaptersフィールドを上書き提案前のリンク、質問からユーザーoowowaeeへ

答えて

0

おかげで、コードは今働いているとレコードがデータベースから削除されません。

class StorySerializer < ActiveModel::Serializer 
    include Pundit 

    attributes :id, :title, :summary, :published, :published_date, :created_at, :updated_at, :cover 

    belongs_to :user 
    has_many :chapters 

    # ------------------------------------------------------------------------ 
    # Note: need to use 'object.chapters' not 'self.chapters` below. 
    # ------------------------------------------------------------------------ 
    def chapters 
    policy_scope(object.chapters) 
    end 
end 
関連する問題