Impression of Pundit (3)
‣ ຊདྷɺݖݶཧͱʮAdminͳΒArticleϦ
ιʔεͷߋ৽ΛೝՄ͢ΔʯͷΑ͏ʹϦιʔε
ϕʔεͷཁ݅Ͱ͋ΔͨΊݖݶཧΛϞσϧد
Γͷͱͯ͠ଊ͑ɺϦιʔε͝ͱʹೝՄ
݅Λఆ͍ٛͯ͘͠Ξϓϩʔνͱͯࣗવɻ
Slide 31
Slide 31 text
ʨ`≡ŋŐŋ≡´ʩὑ
Slide 32
Slide 32 text
Problem of Pundit (1)
‣ Modelຖʹ࡞͢ΔPolicyΫϥεʹControllerͷ
Actionʹඥͮ͘ϝιου(action + ?)Λ࣮͢Δͨ
ΊɺPunditΛ͏্ͰModel, Policy, Controller
͕1ର1ର1ͱ͍͏੍͕҉తʹੜ·ΕΔ
Model A
A Policy
Controller A
update
destroy
update?
destroy?
Slide 33
Slide 33 text
Problem of Pundit (2)
‣ جຊతʹArticleModelΛѻ͏ͷ
ArticlesControllerͳͷͰArticlePolicy͕͋Εࣄ
ΓΔ
‣ ͕͔͠͠ɺΞϓϦ͕ෳࡶʹͳ͍ͬͯ͘ͱಛఆͷϞ
σϧΛѻ͏Controller͕ෳଘࡏ͢Δ߹ɺͭ·Γ
1ͭͷϞσϧʹର͠ෳͷPolicy͕ඞཁʹͳΔ߹
͕͋Γɺ͜ͷ࣌Punditར༻ऀԿΒ͔ͷ͢Δ
ඞཁ͕͋Δɻ(ࠇຐज़ͰPunditΛὃ͢ɺϞϯΩʔ
ύονΛͯΔɺPolicyΫϥεΛ͢ΔͳͲ)
Slide 34
Slide 34 text
Problem of Pundit (3)
UserPolicy
show?
UsersController
show
Admin::UsersController
show
User
‣ UsersControllerͱAdmin::UsersControllerͷshowͷೝՄ݅
͕ҧ͏߹ɺUserPolicyͱAdmin::UserPolicy͕ཉ͘͠
ͳΔ͕1ͭͷModelʹରͯ͠1ͭͷPolicy͔͠ඥ
͚Δ͜ͱͰ͖ͳ͍
Slide 35
Slide 35 text
ʨ `≡ŋŐŋ≡´ʩ ᵪᴸᴸᴸᵪ
Slide 36
Slide 36 text
ʨ›`≡ŋŐŋ≡´ʩ›ớ ᵲᴸᴸᴸᵲ
Slide 37
Slide 37 text
ʨ `≡ŋŐŋ≡´ʩ ᵪᴸᴸᴸᵪ
Slide 38
Slide 38 text
Make a hypothesis for authorization
‣ ݖݶཧ͕ϞσϧدΓͷͳΒɺຊདྷೝՄର
ControllerͷActionͰͳ͘ActiveRecordͷ
ૢ࡞ܥϝιου(create, updateͳͲ)Ͱͳ͍͔
‣ ͔͠͠ɺͦΕͰొɺߋ৽ɺআೝՄՄೳ
͕ͩࢀরΛೝՄͰ͖ͦ͏ʹͳ͍
‣ ΓݖݶཧControllerدΓͷͳͷͰ
ͳ͍͔
Slide 39
Slide 39 text
https://github.com/kyuden/banken
Slide 40
Slide 40 text
Choose banken gem
Slide 41
Slide 41 text
Design concept of Banken
‣ Controllerຖʹ
Actionʹର͢ΔೝՄ݅Λఆٛ
Slide 42
Slide 42 text
Architecture with Banken
‣ ControllerຖʹLoyaltyΫϥεΛ࡞͠Action
ʹର͢ΔೝՄ݅Λఆ͍ٛͯ͘͠
A Loyalty
Controller A
update
destroy
update?
destroy?
Slide 43
Slide 43 text
How to use Banken
v
Slide 44
Slide 44 text
Description of Banken (1)
‣ Pundit likeͳAPIΛอͪͳ͕ΒPunditͷ
ModelدΓͷΛControllerدΓʹॻ͖
͑ͨgem
Slide 45
Slide 45 text
Description of Banken (2)
Admin::UsersLoyalty
show?
UsersController
Admin::UsersController
show
UsersLoyalty
show?
show
‣ ControllerຖʹLoyaltyΫϥεΛ࡞͠
Modelʹґଘ͠ͳ͍ͷͰɺલड़ͨ͠
PunditͷऑճආͰ͖Δ
Problem of Banken
‣ BankenʹΛModel͔ΒControllerدΓʹ
Ҡͨ͠ࣄͰPunditͰ͍͏ॴͷScopeػೳ͕ͳ͍
‣ Scopeػೳͱ͍͏ͷɺݖݶ͝ͱʹ༻͢Δ
named scopeΛΓସ͑Δػೳ
‣ ͕͔͠͠ɺnamed scopeͷΓସ͕͑ඞཁͳ
߹୯७ʹModelʹ࣮͢Εྑ͍ͷͰɺඞͣ͠
Scopeػೳ͕ඞཁ͔ͱ͍͑ͦΜͳ͜ͱͳ͍