Some ways to DRY in Rails

Embed Size (px)

Citation preview

Seminar

Some ways to DRY in Rails

Presenter: Bui Trung Kien Framgia Inc

Content

Model

Controller

Views

Model

Use 'scope'scope :admin, ->{where(admin: true)}

Use 'module'

app/model/concern/set_user.rbmodule SetUser //code goes hereend

app/model/user.rbclass Admin < ActiveRecord::Base include SetUser

Controller

Use 'before_action'

app/controllers/users_controller.rbclass UsersController < ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy]...privatedef set_user @user = User.find params[:id]end

Controller

Use respond_with and respond_to

class UsersController < ApplicationController respond_to :html, :json, :xml

def index @users = User.all respond_with @usersend

Controller

Move business code logic into model methods.

Move view logic code into helper methods.And then try to DRY it up.

View

Use 'partial'.

Use nested layout.

THANKS FOR WATCHING

CLIQUE PARA EDITAR O FORMATO DO TEXTO DO TTULO

Clique para editar o formato do texto da estrutura de tpicos2. Nvel da estrutura de tpicos3. Nvel da estrutura de tpicos4. Nvel da estrutura de tpicos5. Nvel da estrutura de tpicos6. Nvel da estrutura de tpicos7. Nvel da estrutura de tpicos