2017-04-01から1ヶ月間の記事一覧

込みいった生成処理をファクトリメソッドに置き換える

元記事はこちら 翻訳 Bad Smell class InvoicesController < ApplicationController def create @invoice = Invoice.new(params[:invoice]) @invoice.address = current_user.address @invoice.phone = current_user.phone @invoice.vip = (@invoice.amount …

モデルのロジックをモデルに移動する

元記事はこちら 翻訳 Bad Smell class PostsController < ApplicationController def publish @post = Post.find(params[:id]) @post.update_attribute(:is_published, true) @post.approved_by = current_user if @post.created_at > Time.now - 7.days @po…

モデルに仮想的な属性を追加する

元記事はこちら 翻訳 Bad Smell <% form_for @user do |f| %> <%= text_field_tag :full_name %> <% end %> class UsersController < ApplicationController def create @user = User.new(params[:user]) @user.first_name = params([:full_name]).split(' '…

スコープを使ってアクセスする

元記事はこちら 翻訳 オブジェクトのオーナーとcurrent_userを比較することで、アクセス権をチェックしたいとおもったら、そんな冗長でイケてない書き方はせずに、スコープをつかてアクセスしましょう。 Bad Smell class PostsController < ApplicationContr…

Modelの関連付けを利用する

元記事はこちら 翻訳 Bad Smell class PostsController < ApplicationController def create @post = Post.new(params[:post]) @post.user_id = current_user.id @post.save end end この例では、user_idは明らかに@postに割り当てられます。大きな問題には…

検索処理をスコープに入れる

元記事はこちら 翻訳する BadSmell class PostsController < ApplicationController def index @published_posts = Post.find(:all, :conditions => { :state => 'published' }, :limit => 10, :order => 'created_at desc') @draft_posts = Post.find(:all,…

Rails Best Practiceを読む

転職してPHP->Ruby(Rails)に宗旨替えしました。 Railsの書き方にようやく慣れてきたものの、まだまだ書き方で指摘されることが多いので、 RailsのBestPracticeを読んで勉強することにしました。 1日1本目標に読んでいく。