Closed23

Rails

ohki_suguru.phpohki_suguru.php

RubyとPHPとの違い

  • インスタンス変数と普通の変数で書き方が異なる
  • プライベートメソッドが、実は継承後のクラスでも使える
  • 厳密比較である .eql? がPHPほど利用されない
  • === は厳密比較演算子として利用されているわけではない Do not use eql? when using == will do. The stricter comparison semantics provided by eql? are rarely needed in practice. という別のものに利用されるため避けてねとRubocopに指摘される
  • Rubyのオブジェクトのプロパティを追加する際には、キーにシンボルを利用する。hoge[:fuga]といった感じ。
    • hoge[:fuga]でもhoge['fuga']でもRailsのControllerでは問題ないが、他の箇所ではスタンダードなRubyを利用しているため、両者が区別される。
  • 変数 ||= 指定値 という書き方はnilの際に指定値を代入するというやつ。nilガードという呼び方があるらしい
    • PHPで言ったら 変数 = その前の変数 ?? 指定値 が↑と同じ考え方の実装方法となる

Ruby

https://docs.ruby-lang.org/ja/latest/doc/spec=2fvariables.html

https://style.potepan.com/articles/36186.html

https://zenn.dev/itoo/articles/refactorilng-ruby

https://zenn.dev/kanoe/articles/352d78902c83e168db66

PlayGround

https://try.ruby-lang.org/playground/#code=a+%3D+'Welcome+' p+a&engine=opal

Rails

https://railsdoc.com/rails_base

with Auth0

https://abillyz.com/vclbuff/studies/126
https://rso.hateblo.jp/entry/2020/01/27/161442
https://community.auth0.com/t/additional-properties-not-allowed-cant-create-user-using-management-api/7308/3
https://auth0.com/docs/troubleshoot/product-lifecycle/past-migrations

一部アクションのスキップなどミドルウェアまわり

skip_before_action

公式ドキュメント
複数メソッド指定の際のやり方

credential

https://qiita.com/NaokiIshimura/items/2a179f2ab910992c4d39

RSpecの書き方まわり

  • RSpecはメソッドを中心にdescribeをテストしていく。
  • context内側で前提条件を書いていく
  • 共通化とか結構できるけど、テストに関してバグを含むのがよろしくない
    • Rubyのテストは共通化をしすぎると、書き方が複雑になりがち
    • バグを仕込まないために愚直に書いていくようにしていくのが良さそう
  • よくRspecのcontextやitで利用される英単語についてはこちらを見ると良さそう

https://www.betterspecs.org/
https://thiagoa.github.io/some-notes-on-testing-and-pundit-testing/
https://qiita.com/k-o-u/items/48881200d0be92a4e6f6
https://tyablog.net/2020/04/26/stub-mock-spy-introduction-in-spock-testing-framework/
RSpecの(describe/context/example/it)の使い分け
jwtで認証を実装する時のrspecテスト
https://qiita.com/ostk0069/items/469df28954cea9e25509
https://qiita.com/johnslith/items/c0b2a9b8ce8770e5d317
https://zenn.dev/murakamiiii/articles/eddd69f3dcde18
https://qiita.com/YumaInaura/items/223a7d55af385e1b9503

リクエストの取り扱いについて

https://qiita.com/mochio/items/45b9172a50a6ebb0bee0

Resourceの出しわけ

alba
ActiveModelSerializers-zenn
active_model_serializers
albaの循環参照対策

Railsの場合のcreate, update, saveの違い

https://qiita.com/luccafort/items/677020b86f3dc240529bG

transactionとrollback

https://qiita.com/ytnk531/items/a0db31ee4311425a3933
https://qiita.com/huydx/items/d946970d130b7dabe7ec
https://blog.furu07yu.com/entry/rails-transaction
https://qiita.com/pandama09396862/items/5bc9bd265009982dd3f5
https://qiita.com/luccafort/items/5c1f4a7fcf7979f7c727

includes周り

https://tech.high-link.co.jp/entry/ActiveRecord-includes-rock

ohki_suguru.phpohki_suguru.php

DEPRECATED系

DEPRECATION WARNING: config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. To append new attachables to the Active Storage association, prefer using `attach`. Using association setter would result in purging the existing attached attachments and replacing them with new ones.
ohki_suguru.phpohki_suguru.php

認可

  • policyとmiddlewareの使い分け->認可の機能がLaravelと異なり、デフォルトでは、Railsにはない。
  • どのライブラリ使いますか?というので、書き方が変わるが、auth-logic
  • 認可: punditを使うのが割と一般的なWEBアプリだったらちょうど良いかも。
    • authorizeで引数のモデルに近いpolicyで認可のチェックをおこなってくれる(userだったらuser_policy)
    • 細かく制御をしていくためのものなので、下手に共通化をしない方針
    • プロジェクトによって
      • pundit: 二番手くらい
        • Controllerごとにやろう
        • 一般的にちょうど良い
      • cancancan: 結構多い
        • 認可が大味
        • 1個のファイルに権限をまとめよう->難しい
      • banken: メドピア
        • https://github.com/kyuden/banken
        • データのリソースごとに権限をまとめたい
        • 細かくやる必要があり、一般的なサービスにはやり過ぎになることが多いかも
このスクラップは2023/06/28にクローズされました