This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test_helper' | |
| class AnswerTest < ActiveSupport::TestCase | |
| def setup | |
| Answer.before_save do | |
| instance_variable_set :@first_callback, true | |
| end | |
| @answer = Answer.create! | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test_helper' | |
| class QuestionTest < ActiveSupport::TestCase | |
| include ActionView::Helpers::TranslationHelper | |
| test "title is required" do | |
| question = Question.new.tap(&:valid?) | |
| assert_includes question.errors[:title], | |
| t('errors.messages.blank') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module FloatDivision | |
| refine Fixnum do | |
| def /(other) | |
| self.to_f / other | |
| end | |
| end | |
| end | |
| using FloatDivision |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Song < ActiveRecord::Base | |
| def length=(length) | |
| return unless length =~ /\A\d+:\d{2}\z/ | |
| minutes, seconds = length.split(':').map(&:to_i) | |
| write_attribute(:length, minutes * 60 + seconds) | |
| end | |
| def length | |
| return unless length = read_attribute(:length) | |
| "%02d:%02d" % length.divmod(60) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateUsers < ActiveRecord::Migration | |
| def change | |
| create_table :users do |t| | |
| t.string :username | |
| t.string :email | |
| t.string :password_digest | |
| t.index :username, unique: true | |
| t.index :email, unique: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| before_save(if: :username_changed?) { username.downcase! } | |
| before_save 'username.downcase!', if: :username_changed? | |
| before_save ->{ username.downcase! }, if: :username_changed? | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| protect_from_forgery with: :exception | |
| private | |
| def signed_in? | |
| !!session[:user_id] | |
| end | |
| helper_method :signed_in? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Time.now.strftime("%I:%M%p") #=> "09:06PM" | |
| Time.now.strftime("%H:%M") #=> "21:06" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # config/initializers/contraints.rb | |
| module Constraints | |
| class Subdomain | |
| def self.matches?(request) | |
| request.subdomain.present? && request.subdomain != 'www' | |
| end | |
| end | |
| class SignedIn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module FormHelper | |
| def login_form | |
| find('#new_session') | |
| end | |
| end |