I often see the use of || to set a default value for a variable that might be nil, null or...
GRASP (object-oriented design) Single Responsibility Principle Tell, Don't Ask Best practices for writing code comments Read the following chapters from our book Growing Rails Application...
...has_many :items validates_presence_of :recipient_address, :number end class Invoice::Item < ApplicationRecord belongs_to :invoice belongs_to :product validates_numericality_of :units end class Product < ApplicationRecord validates_presence...
Starting with Ruby 1.9, most #each methods can be called without a block, and will return an enumerator. This is...
...very nasty fails. Below are the basic changes you need to perform and some behavior you may eventually run into when upgrading your application. This aims to save you some...
...in your uploaders... def extension_white_list %w[jpg jpeg gif png] end must become def extension_allowlist %w[ jpg jpeg png gif ] end Also, every uploader must define allowlists...
...helper in many views, but in the index view I hide the label to better fit the layout. Here is the helper: module IconHelper def icon(*args, &block) options = args.extract...
CSS transitions are a simple animation framework that is built right into browsers. No need for Javascript here. They're...
It's not a good idea to leave data objects mutable. They should behave like integers. Ruby's Data would have already enforced that. Because I used a regular...
...attr}=" do |value| if @readonly raise 'Readonly' else super(value) end end end Weird behavior changes from composed_of The method composed_of accepts the option :mapping, which is required...
Expecting a primitive value as an argument to a method invocation is easy: expect(object).to receive(:foo).with('arg1...
...your commits are the changes. Never use merge. You've found some code that belongs to an earlier commit in your feature branch If you already have a "final" (i.e...
git rebase -i master Move the fixup directly below the commit it belongs to. Change the "pick" in front of the fixup commit to "f". Don't touch...
Whenever you create a table from a database migration, remember to add updated_at and created_at timestamps to that...
...the "space" character class For matching whitespaces in a regular expression, the most common and best-known shorthand expression is probably \s. It matches the following whitespace characters: " " (space)
You can use the config.x configuration in combination with config_for to configure global settings for your Rails 4.2+ application...
Do not pass times to date attributes. Always convert times to dates when your application uses time zones. Background
I recommend install Node.js using nvm. This way you can have multiple Node versions in your ~/.nvm. You also won...
Getting an entire test suite green can be a tedious task which involves frequent switches between the CLI that is...
...from using fixtures over FactoryBot, as fixtures load essential test data upfront at the beginning of the testsuite. Finding peace with fixtures While setting up the database upfront challenges the...
...idea of maintaining a perfectly clean database state at the beginning of each test, with some disciplined practices, handling fixtures becomes quite manageable. Let’s dive into how this works...
...the old versions you wish to remove. Keep at least one recent, working kernel besides the one you are currently running. Address Metapackage Conflict (GA vs. HWE)
...a publicly available service) Understand that Safari and mobile Safari (for iOS) usually lag behind other browsers when it comes to feature support. "Transpilation" and "polyfills" are both techniques to...
...ES versions at the top. English MDN Web API Reference MDN is maybe the best reference for JavaScript, HTML and CSS features. Google my-feature mdn is a quick way...
...holds your bash prompt. You might want to change it to serve your needs best. Here is how to: General non-printing escape sequences in your prompt have to be...
...Yourself (or DRY). In Ruby on Rails we keep our code DRY by sharing behavior by using inheritance, modules, traits or partials. When you reuse behavior you want to reuse...
...module that you include in the Cucumber world. This way the module's methods become available to all step definitions. Think of it as enhancing your Capybara API with app...
Ruby (business logic) HTML fragments (layouts and views) CSS/Sass/SCSS (styles) JavaScript (client-side behavior) Static media (images, fonts, videos) Except for the Ruby part, all these files are shipped...
...frees you from needing Webpack and Yarn. This is an experimental solution that could become the default in future Rails versions. It lacks any transpilation/transformation and bundling features. They reason...
...blocks or confusingly sometimes also procs Those with "method semantics", called lambdas lambdas They behave like Ruby method definitions: They are strict about their arguments. return means "exit the lambda...
...the lambda literal -> (since Ruby 1.9.1) test = ->(arg) do puts arg end blocks They behave like do-blocks or simply "segments of code": They try to be smart about their...
end end def allowed_mime_types %w(image/jpeg image/png) end end This will behave like the solution before: User#portrait will get an expected error message and the User...
Background information about session storage in Rails Rails has a default mechanism to store the session in the CookieStore. This...