:current_user def connect self.current_user = find_user! end private def find_user! User.find_by(id: cookies.signed[:user_id]) or raise UnauthorizedConnectionError end end end
end class ApplicationController < ActionController::Base # Please, don't do it in real code! before_action :set_current_user_globally def set_current_user_globally OmgGlobal.current_user = current_user end end class Post < ActiveRecord::Base after_save :create_version private def create_version Version.create(model: self.class.to_s, attributes: attributes, who_did_it: OmgGlobal.current_user.id) end end
No more garbage in logs Started GET "/assets/public/home.js?body=1" for 127.0.0.1 at 2016-09-27 19:31:46 +0200 Started GET "/assets/bootstrap/collapse.js?body=1" for 127.0.0.1 at 2016-09-27 19:32:04 +0200 Started GET "/assets/font-awesome/fontawesome-webfont.woff2?v=4.5.0" for 127.0.0.1 at 2016-09-27 19:32:04 +0200 Started GET "/assets/public/locales.js?body=1" for 127.0.0.1 at 2016-09-27 19:32:04 +0200
false class Product < ActiveRecord::Base before_save :set_availability_status def set_availability_status self.available = somehow_compute_availability_status_which_returns_false throw(:abort) end end
:send_notification def publish! update!(published_at: Time.current) end private def send_notification notifications.create(message: content, author: author) end end
:send_notification def publish! update!(published_at: Time.current) end def publish_without_notification! Notification.suppress do publish! end end private def send_notification notifications.create(message: content, author: author) end end
permitted? @parameters.to_h else slice(*self.class.always_permitted_parameters).permit!.to_h end end def user_params params.permit!.to_h end def user_params params.to_unsafe_h end
:orders, comment: "Orders table" do |t| t.string :description, comment: "Description of the order" t.integer :owner_id, comment: "Owner of the order" t.timestamps end add_index :orders, :owner_id, name: "index_name", comment: "Index for owner_id" end end
comment: "Orders table" do |t| t.string "description", comment: "Description of the order" t.integer "owner_id", comment: "Owner of the order" t.index ["owner_id"], name: "index_name",using: :btree, comment: "Index for owner_id" end end