Snippets

Takumi IINO app-components

Created by Takumi IINO

アプリケーション作る時の要素

Awesome

restful

API

アクセスコントロール

ruby

  • cancancan
    • subjectへの操作許可を定義。
    • Rails用
  • pundit
    • PolicyFinderの探し方に気をつける必要がある
      • Pundit.authorize(user, record, query)でrecordは一個しか渡せない。
    • verify_authorizedは非常に便利
    • scopeとかpermit_attributesとかあるけど無理して使う必要はない。
    • 分散して定義することが前提
  • stffn/declarative_authorization
  • six
    • gitlabhq用。シンプルで納得がいく
    • 汎用
    • allowed(object, subject)からabilityを返す
    • allowed?(object, abiliby, subject)でabilityの許可を調べる。
    • 定義
  • action_access
    • Rails用
  • Redmine::AccessControl
    • ability名と操作可能なactionを定義
    • redmineのためのACL
    • 定義
  • RailsCast #386 authorization from scratch
    • objectをベースにpermissionを定義
    • Rails用
    • 許可するアクションを記載していく
    • 引数としてsubjectも取れる
    • permit_paramsもかけるが、これはちょっと使いにくい
      • 許可するパラメータはformとかに持たせたいので。
    • railscasts/386-authorization-from-scratch-part-2

レシピ

同じsubjectを複数のcontrollerから操作するが、それが複数の権限から参照される場合。

# とりあえず継承して名前を変更しておく
class Subject < ActiveRecord
  class AsHoge < self
  end
end

# cancancan
class Ability
  include CanCan::Ability
  def initialize(*args)
    can :manage, Subject::AsHoge
  end
end

# pundit
class Subject::AsHoge
  def self.policy_class
    'SubjectHogePolicy'
  end
end
SubjectHogePolicy = Struct.new(:user, :record) do
  def create?
    true
  end
end

# six はsubjectに対してalitityを自由に設定できる。
ability = :show_subject_hoge
abilities.allowed(user, ability, subject)

CanCanCanのようなsubjectにたいしての権限しか設定できないものは辛い。punditはRecord#policy_classでPolicyClassを指定できる。

状態遷移マシン

ruby

ファイルアップロード

ruby

ユーザー関連

routes

# signup
GET     /users/new                       users#new
POST    /users                           users#create

# 疎通確認
GET     /users/confirmation?token        users/confirmation#show
# 疎通確認再送信
GET     /users/confirmation/new          users/confirmation#new
POST    /users/confirmation              users/confirmation#create
GET     /users/confirmation/sent         users/confirmation#sent

# 設定系
GET     /settings                        settings#show
GET     /settings#

# パスワードリセット
GET     /users/passwords/new             users/passwords#new
POST    /users/passwords                 users/passwords#create
GET     /users/passwords/sent            users/passwords#sent
GET     /users/passwords/edit            users/passwords#edit
PUT     /users/passwords                 users/passwords#update
PATCH   /users/passwords                 users/passwords#update

# パスワード変更
GET     /(profile|me)/passwords/edit
PUT     /(profile|me)/passwords/
GET     /settings/passwords/edit
PUT     /settings/passwords/

# 詳細
GET     /(profile|me)
GET     /(profile|me)/account/edit
PUT     /(profile|me)/account
PATCH   /(profile|me)/account

# 退会
GET     /(profile|me)/cancle #new
DELETE  /(profile|me)/cancle

# ログイン/ログアウト
GET     /sign_in                         sessions#new
POST    /sign_in                         sessions#create
DELETE  /logout                          sessions#destroy

# omniauth
GET     /users/auth/:provider            omniauth_callbacks#:provier
GET     /users/auth/:provider/callback   omniauth_callbacks#callback

簡易プラグインシステム

ruby プラグイン登録

単機能なプラグインの場合はまあこれでいい

module App
  module Plugin
    @plugins = {}

    class << self
      def register name, plugin
        @plugins[name] = plugin
      end

      # インスタンスを返す場合、クラスを返す場合、などイロイロある
      def get name
        @plugins[name]
      end
    end
  end
end

module App
  module Plugin
    class Hoge
    end
    register :hoge, Hoge
  end
end

# plugin load

ruby 横断的なプラグイン

discourseの例を参考に。 - DiscoursePluginRegistory - プラグイン毎のデータ(assetsなどを含む)を登録する。登録されたデータはいろんなところで利用される。 - DiscoursePlugin) - プラグイン用のヘルパー集。 - DiscourseEvent - 主にプラグイン用のevent emitter。

プラグイン読み込み、プラグインがデータ&処理登録、必要な場所で読み出しの流れが基本。

service layer

navigation

ruby

CLI

testing

rails

ruby refactoring

css

BEM

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.