Rails Best Practice 與 Refactor

rails_best_practices是一個很不錯的Gem套件,它匯集了一些調整Rails Performace的小技巧,並且透過指令 bundle exec rails_best_practice .的方式就能夠列出你的問題,跳轉可以看到我噴了哪些東西XD

可以透過gem rails_best_practices來安裝,官方網站在 https://github.com/railsbp/rails_best_practices

之後再慢慢列出解決的方式,這時真的發現有Git真好:P

./app/controllers/rainbow_seed/shares_controller.rb:103 - use model association (for like)
./app/controllers/comments_controller.rb:18 - move model logic into model (@comment use_count > 4)
./app/controllers/pocket_snapper/users_controller.rb:52 - move model logic into model (@user use_count > 4)
./app/controllers/rainbow_seed/comments_controller.rb:18 - move model logic into model (@comment use_count > 4)
./app/controllers/rainbow_seed/comments_controller.rb:18 - move model logic into model (format use_count > 4)
./app/controllers/users_controller.rb:52 - move model logic into model (@user use_count > 4)
./db/schema.rb:30 - always add db index (comments => [commentable_id, commentable_type])
./app/controllers/rainbow_seed/news_controller.rb:18,39,61,77 - use before_filter for show,edit,update,destroy
./app/views/comments/_form.html.erb:1 - replace instance variable with local variable
./app/views/comments/_form.html.erb:2 - replace instance variable with local variable
./app/views/comments/_form.html.erb:4 - replace instance variable with local variable
./app/views/comments/_form.html.erb:7 - replace instance variable with local variable
./app/views/rainbow_seed/news/_form.html.erb:1 - replace instance variable with local variable
./app/views/rainbow_seed/news/_form.html.erb:3 - replace instance variable with local variable
./app/views/rainbow_seed/news/_form.html.erb:6 - replace instance variable with local variable
./app/views/rainbow_seed/news/_news_item.html.erb:8 - replace instance variable with local variable
./app/views/rainbow_seed/shares/_form.html.erb:1 - replace instance variable with local variable
./app/views/rainbow_seed/shares/_form.html.erb:2 - replace instance variable with local variable
./app/views/rainbow_seed/shares/_form.html.erb:4 - replace instance variable with local variable
./app/views/rainbow_seed/shares/_form.html.erb:7 - replace instance variable with local variable
./app/views/rainbow_seed/shares/_form.html.erb:20 - replace instance variable with local variable
./app/views/shares/_form.html.erb:1 - replace instance variable with local variable
./app/views/shares/_form.html.erb:2 - replace instance variable with local variable
./app/views/shares/_form.html.erb:4 - replace instance variable with local variable
./app/views/shares/_form.html.erb:7 - replace instance variable with local variable
./app/views/users/_form.html.erb:1 - replace instance variable with local variable
./app/views/users/_form.html.erb:2 - replace instance variable with local variable
./app/views/users/_form.html.erb:4 - replace instance variable with local variable
./app/views/users/_form.html.erb:7 - replace instance variable with local variable
./app/mailers/user_mailer.rb:9 - use multipart/alternative as content_type of email
./app/mailers/user_mailer.rb:15 - use multipart/alternative as content_type of email
./app/views/rainbow_seed/news/edit.html.erb:4 - simplify render in views
./app/views/rainbow_seed/news/index.html.erb:8 - simplify render in views
./app/views/rainbow_seed/news/new.html.erb:4 - simplify render in views
./app/views/rainbow_seed/shares/list.html.erb:11 - simplify render in views
./app/views/rainbow_seed/shares/show.html.erb:22 - simplify render in views
./app/views/shares/show.html.erb:23 - simplify render in views
./app/controllers/comments_controller.rb:27 - simplify render in controllers
./app/controllers/pocket_snapper/users_controller.rb:62 - simplify render in controllers
./app/controllers/pocket_snapper/users_controller.rb:86 - simplify render in controllers
./app/controllers/rainbow_seed/comments_controller.rb:28 - simplify render in controllers
./app/controllers/rainbow_seed/comments_controller.rb:29 - simplify render in controllers
./app/controllers/rainbow_seed/news_controller.rb:53 - simplify render in controllers
./app/controllers/rainbow_seed/news_controller.rb:69 - simplify render in controllers
./app/controllers/rainbow_seed/registrations_controller.rb:19 - simplify render in controllers
./app/controllers/rainbow_seed/shares_controller.rb:71 - simplify render in controllers
./app/controllers/rainbow_seed/shares_controller.rb:86 - simplify render in controllers
./app/controllers/users_controller.rb:62 - simplify render in controllers
./app/controllers/users_controller.rb:86 - simplify render in controllers
./app/helpers/comments_helper.rb:1 - remove empty helpers
./app/helpers/home_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/green_mates_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/more_green/carbon_footprint_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/more_green/green_tip_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/more_green/invitation_cards_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/more_green/wall_paper_helper.rb:1 - remove empty helpers
./app/helpers/rainbow_seed/top5_helper.rb:1 - remove empty helpers
./app/helpers/sessions_helper.rb:1 - remove empty helpers
./app/helpers/users_helper.rb:1 - remove empty helpers
./config/routes.rb:62 - restrict auto-generated routes (:only => [:show, :edit, :update, :index])
./config/routes.rb:67 - restrict auto-generated routes (:only => [:create, :index])
./config/routes.rb:73 - restrict auto-generated routes (:only => [:create, :index])
./app/models/ckeditor/attachment_file.rb:9 - remove unused methods (Ckeditor::AttachmentFile#url_thumb)
./app/models/ckeditor/picture.rb:10 - remove unused methods (Ckeditor::Picture#url_content)
./app/models/rainbow_seed/share.rb:56 - remove unused methods (RainbowSeed::Share#popular_in_date)
./app/models/rainbow_seed/share.rb:71 - remove unused methods (RainbowSeed::Share#image_url)
./app/models/rainbow_seed/share.rb:75 - remove unused methods (RainbowSeed::Share#comments_count)
./app/controllers/application_controller.rb:5 - remove unused methods (ApplicationController#devise_layout)
./app/controllers/pocket_snapper/confirmations_controller.rb:7 - remove unused methods (PocketSnapper::ConfirmationsController#after_confirmation_path_for)
./app/controllers/pocket_snapper/passwords_controller.rb:10 - remove unused methods (PocketSnapper::PasswordsController#after_sending_reset_password_instructions_path_for)
./app/controllers/pocket_snapper/registrations_controller.rb:5 - remove unused methods (PocketSnapper::RegistrationsController#after_inactive_sign_up_path_for)
./app/controllers/pocket_snapper/users_controller.rb:130 - remove unused methods (PocketSnapper::UsersController#find_user)
./app/controllers/pocket_snapper_controller.rb:10 - remove unused methods (PocketSnapperController#resend_password)
./app/controllers/rainbow_seed/confirmations_controller.rb:6 - remove unused methods (RainbowSeed::ConfirmationsController#after_confirmation_path_for)
./app/controllers/rainbow_seed/passwords_controller.rb:15 - remove unused methods (RainbowSeed::PasswordsController#after_sending_reset_password_instructions_path_for)
./app/controllers/rainbow_seed/users_controller.rb:54 - remove unused methods (RainbowSeed::UsersController#update_password)
./app/controllers/users_controller.rb:130 - remove unused methods (UsersController#find_user)
./app/models/ckeditor/asset.rb:1 - protect mass assignment
./app/models/ckeditor/attachment_file.rb:1 - protect mass assignment
./app/models/ckeditor/picture.rb:1 - protect mass assignment
./app/models/comment.rb:1 - protect mass assignment
./app/models/news.rb:1 - protect mass assignment
./app/models/pocket_snapper/point.rb:1 - protect mass assignment
./app/models/rainbow_seed/data.rb:1 - protect mass assignment
./app/models/rainbow_seed/share/history.rb:1 - protect mass assignment
./app/models/rainbow_seed/share/like.rb:1 - protect mass assignment
./app/models/rainbow_seed/share.rb:1 - protect mass assignment
./app/models/setting.rb:1 - protect mass assignment
./app/models/share_comment.rb:1 - protect mass assignment
./app/models/user_observer.rb:1 - protect mass assignment
./app/models/ckeditor/attachment_file.rb:5 - remove trailing whitespace
./app/models/comment.rb:4 - remove trailing whitespace
./app/models/news.rb:2 - remove trailing whitespace
./app/models/rainbow_seed/data.rb:3 - remove trailing whitespace
./app/models/rainbow_seed/share.rb:7 - remove trailing whitespace
./app/models/setting.rb:2 - remove trailing whitespace
./app/models/user.rb:6 - remove trailing whitespace
./app/models/user_observer.rb:2 - remove trailing whitespace
./app/mailers/user_mailer.rb:19 - remove trailing whitespace
./app/helpers/rainbow_seed/more_green_helper.rb:8 - remove trailing whitespace
./app/helpers/rainbow_seed/users_helper.rb:7 - remove trailing whitespace
./app/helpers/rainbow_seed_helper.rb:2 - remove trailing whitespace
./app/helpers/shares_helper.rb:2 - remove trailing whitespace
./app/controllers/application_controller.rb:4 - remove trailing whitespace
./app/controllers/comments_controller.rb:2 - remove trailing whitespace
./app/controllers/pocket_snapper/confirmations_controller.rb:2 - remove trailing whitespace
./app/controllers/pocket_snapper/passwords_controller.rb:2 - remove trailing whitespace
./app/controllers/pocket_snapper/registrations_controller.rb:2 - remove trailing whitespace
./app/controllers/pocket_snapper/sessions_controller.rb:2 - remove trailing whitespace
./app/controllers/pocket_snapper/users_controller.rb:4 - remove trailing whitespace
./app/controllers/pocket_snapper_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/comments_controller.rb:2 - remove trailing whitespace
./app/controllers/rainbow_seed/confirmations_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/home_controller.rb:11 - remove trailing whitespace
./app/controllers/rainbow_seed/more_green_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/passwords_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/registrations_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/sessions_controller.rb:3 - remove trailing whitespace
./app/controllers/rainbow_seed/shares_controller.rb:6 - remove trailing whitespace
./app/controllers/rainbow_seed/top5_controller.rb:6 - remove trailing whitespace
./app/controllers/rainbow_seed/users_controller.rb:2 - remove trailing whitespace
./app/controllers/rainbow_seed_controller.rb:2 - remove trailing whitespace
./app/controllers/users_controller.rb:4 - remove trailing whitespace
./config/deploy.rb:39 - remove trailing whitespace
./config/environments/development.rb:17 - remove trailing whitespace
./config/environments/production.rb:60 - remove trailing whitespace
./config/routes.rb:2 - remove trailing whitespace
./db/migrate/20120118031257_create_users.rb:16 - remove trailing whitespace
./db/migrate/20120119070509_create_comments.rb:13 - remove trailing whitespace
./db/migrate/20120204094252_create_settings.rb:10 - remove trailing whitespace
./db/migrate/20120307173052_create_ckeditor_assets.rb:7 - remove trailing whitespace
./db/migrate/20120309092459_create_rainbow_seed_share_likes.rb:11 - remove trailing whitespace
./lib/custom_authentication_failure.rb:1 - remove trailing whitespace
./app/views/comments/_form.html.erb:13 - remove trailing whitespace
./app/views/layouts/rainbow_seed.html.erb:10 - remove trailing whitespace
./app/views/pocket_snapper/passwords/new.html.erb:3 - remove trailing whitespace
./app/views/pocket_snapper/registrations/new.html.erb:4 - remove trailing whitespace
./app/views/pocket_snapper/resend_password.html.erb:4 - remove trailing whitespace
./app/views/pocket_snapper/sessions/new.html.erb:5 - remove trailing whitespace
./app/views/pocket_snapper/signed_up_successful.html.erb:4 - remove trailing whitespace
./app/views/pocket_snapper/thanks.html.erb:4 - remove trailing whitespace
./app/views/pocket_snapper/users/passwords/new.html.erb:17 - remove trailing whitespace
./app/views/pocket_snapper/users/registrations/new.html.erb:5 - remove trailing whitespace
./app/views/pocket_snapper/users/sessions/new.html.erb:5 - remove trailing whitespace
./app/views/rainbow_seed/green_mates/index.html.erb:3 - remove trailing whitespace
./app/views/rainbow_seed/home/about.html.erb:7 - remove trailing whitespace
./app/views/rainbow_seed/home/contact.html.erb:10 - remove trailing whitespace
./app/views/rainbow_seed/home/index.html.erb:75 - remove trailing whitespace
./app/views/rainbow_seed/more_green/green_tips.html.erb:8 - remove trailing whitespace
./app/views/rainbow_seed/more_green/invitation_cards/card_mailer/content.html.erb:7 - remove trailing whitespace
./app/views/rainbow_seed/more_green/show_invitation_cards_info_form.html.erb:17 - remove trailing whitespace
./app/views/rainbow_seed/shares/_comments_list.html.erb:3 - remove trailing whitespace
./app/views/rainbow_seed/shares/index.html.erb:21 - remove trailing whitespace
./app/views/rainbow_seed/shares/show.html.erb:48 - remove trailing whitespace
./app/views/rainbow_seed/top5/index.html.erb:21 - remove trailing whitespace
./app/views/rainbow_seed/users/registrations/new.html.erb:18 - remove trailing whitespace
./app/views/shares/show.html.erb:33 - remove trailing whitespace
./app/models/ckeditor/attachment_file.rb:8 - remove tab, use spaces instead
./app/models/ckeditor/picture.rb:5 - remove tab, use spaces instead
./app/mailers/rainbow_seed/more_green/invitation_cards/card_mailer.rb:8 - remove tab, use spaces instead
./app/controllers/rainbow_seed/more_green_controller.rb:19 - remove tab, use spaces instead
./app/controllers/rainbow_seed/top5_controller.rb:9 - remove tab, use spaces instead
./config/environments/development.rb:40 - remove tab, use spaces instead
./db/migrate/20120307173052_create_ckeditor_assets.rb:9 - remove tab, use spaces instead
./app/views/layouts/application.html.erb:14 - remove tab, use spaces instead
./app/views/layouts/more_green.html.erb:6 - remove tab, use spaces instead
./app/views/layouts/rainbow_seed.html.erb:45 - remove tab, use spaces instead
./app/views/pocket_snapper/registrations/new.html.erb:7 - remove tab, use spaces instead
./app/views/pocket_snapper/users/passwords/new.html.erb:20 - remove tab, use spaces instead
./app/views/pocket_snapper/users/registrations/new.html.erb:8 - remove tab, use spaces instead
./app/views/rainbow_seed/home/index.html.erb:78 - remove tab, use spaces instead
./app/views/rainbow_seed/home/mobile.html.erb:4 - remove tab, use spaces instead
./app/views/rainbow_seed/more_green/green_tips.html.erb:18 - remove tab, use spaces instead
./app/views/rainbow_seed/more_green/invitation_cards/card_mailer/content.html.erb:13 - remove tab, use spaces instead
./app/views/rainbow_seed/more_green/show_invitation_cards_info_form.html.erb:7 - remove tab, use spaces instead
./app/views/rainbow_seed/news/_news_item.html.erb:3 - remove tab, use spaces instead
./app/views/rainbow_seed/shares/_form.html.erb:22 - remove tab, use spaces instead
./app/views/rainbow_seed/shares/index.html.erb:6 - remove tab, use spaces instead
./app/views/rainbow_seed/top5/get_top.html.erb:16 - remove tab, use spaces instead
./app/views/rainbow_seed/top5/index.html.erb:4 - remove tab, use spaces instead
./app/views/shares/_comments_list.html.erb:4 - remove tab, use spaces instead
./app/views/shares/_form.html.erb:23 - remove tab, use spaces instead
./app/views/users/forget_password.html.erb:3 - remove tab, use spaces instead

拿第一個問題點來說,原本我的code是

  like = current_rainbow_seed_user.likes.build
  like.share_id = params[:id]
  like.save

解法在: http://rails-bestpractices.com/posts/2-use-model-association
因此調整為

  like = current_rainbow_seed_user.likes.build({share_id: params[:id].to_i})
  like.save

就可以了

http://rails-bestpractices.com中搜尋一下就可以找到相關的範例解法
感謝ihower大大的model association啊XD!!

Comments

comments powered by Disqus