makandra dev

...from prepended modules (Ruby 2.0+ feature) Methods from the object's class Methods from included modules Methods from the class hierarchy (superclass and its ancestors) Example

...have the following class hierarchy: class Superclass def action puts "Superclass" end end module IncludedModule def action puts "Included module" super end end module PrependedModule def action puts "Prepended module...

api.rubyonrails.org

ActiveSupport (since 4.1) includes test helpers to manipulate time, just like the Timecop gem: To freeze the current time, use freeze_time (ActiveSupport 5.2+): freeze_time To travel to a...

Using .includes or .eager_load with 1-n associations is dangerous. Always use .preload instead. Consider the following ActiveRecord query: BlogPost.eager_load( :comments :attachments, ).to_a

...the full cross product of the blog post × all its comments × all its attachments, including 1000 redundant copies of all the fields of the blog post. If you add a...

Sometimes you want to fetch associations for an ActiveRecord that you already loaded, e.g. when it has deeply nested associations...

Sometimes we write plain SQL queries in migrations so we don't have to mock ActiveRecord classes. These two migrations...

For performance improvements (and to remove the need for eager loading), the ActsAsTaggableOn gem supports caching your tag lists directly...

makandra dev
aaronlasseigne.com

All Rubyists should be familiar with the common definitions for include and extend. You include a module to add instance methods to a class and extend to add class methods...

...figure this out we’re going to start by discussing where methods are stored. include: Adds methods from the provided Module to the object extend: Calls include on the singleton...

RSpec is smart when using the include-matcher in combination with .not_to. One could assume that .not_to include(3, 4, 5) evaluates to: NOT( .to include...

...NOT include(4) && NOT include(5) ) Warning Using .not_to in combination with the include-matcher doesn't logically negate the final truth value. It instead negates the individual include...

has_many :attachments end Everything looks normal: Note.all.to_a.size # => 8 Note.all.ids.size # => 8 Then .includes leads to weird results: Note.all.includes(:attachments).to_a.size # => 8 Note.all.includes(:attachments).ids.size # => 12

...has 5 attachments, its id will be included 5 times. With .preload it works as expected: Note.all.preload(:attachments).to_a.size # => 8 Note.all.preload(:attachments).ids.size # => 8 Note I created a bug report...

makandra dev
thregr.org

To show your key inputs on screen, e.g. for a screencast or screen sharing, you can use screenkey.

...have no effect when your application is logging from inside a Sidekiq process. This includes custom as well as any framework logs, like query logging from ActiveRecord. Since Sidekiq Workers...

chain.add RailsLoggerSidekiqServerMiddleware end end Note that the 2nd argument is a Hash including all job options. If you need to add more information, you can easily do that...

reactarmory.com

The linked article shows how to exploit websites that include unsanitized user input in their CSS. Although the article often mentions React and CSS-in-JS libraries, the methods are...

Your account profile now links to a personal RSS feed. This RSS feed contains the newest public and private cards...

ctan.larsko.net

...into a new LaTeX document that you will compile to PDF, you can use \includegraphics for this. Although, be prepared to get tons of errors complaining about overfill hboxes and...

...package that takes care: \usepackage[final]{pdfpages} In order to insert a PDF, use: \includepdf{$filename} See the external link for more options...

...the Ubuntu packages shown above. Examples for common requirements Basic resizing In your uploader, include CarrierWave::Vips instead of CarrierWave::MiniMagick. You can now use basic resize macros like resize...

...to_fit or resize_to_fill: class DocumentUploader < CarrierWave::Uploader::Base include CarrierWave::Vips process :resize_to_fit => [1000, 500] version :thumbnail do process :resize_to_fill => [64, 64]

When a cookie includes an Expires attribute or an HTTP response includes caching headers like Expires or Cache-Control, their validity depends on the server's Date header...

Cookie Expires depends on the Date header or browser time When a cookie includes an Expires attribute, the browser evaluates the expiration date relative to a reference time:

blog.bigbinary.com

...on our card why you should be super careful with complex eager_load or includes queries. Thus, as a general guideline.includes or .eager_load with 1-n associations are dangerous...

...that this will not work when preloading polymorphic associations because of joining. ActiveRecord::Relation#includes includes will preload associations like preload ... User.includes(:posts).to_a Query: SELECT "users".* FROM "users...

...it 'validates presence' do record = User.new record.email = '' # invalid state record.validate expect(record.errors[:email]).to include("can't be blank") # check for presence of error record.email = 'foo@bar.com' # valid state record.validate

...record.errors[:email]).to_not include("can't be blank") # check for absence of error end end end Standard validations can be tested with less code The shoulda-matchers gem gives...

...array[:expected_values] AND #{quoted_column} <@ array[:expected_values]", expected_values:) end end ..and include their specs to your test suite: describe ApplicationRecord do describe '.where_array_matches!' do

...the same values' do expect(User.where_array_matches(:movie_ids, [1, 2, 3])).to include(matching_user) end it 'is order-independent' do expect(User.where_array_matches(:movie_ids...

...that we don't need allow_any_instance_of(ActionView::Base).to receive(:javascript_include_tag).and_return('script') allow_any_instance_of(ActionView::Base).to receive(:stylesheet_link_tag...

...route_with_tokens # Ensure that we render a global CSRF token expect(response.body).to include('csrf-token') # Ensure that we render a form CSRF token expect(response.body).to include('authenticity...

...as_trait block, it will be bound to the trait module, not the class including the trait. While this may seem unproblematic at first glance, it becomes a problem when...

...including traits in multiple classes, especially when using parameterized traits. Example (bad) Consider the following trait and class. module ExampleTrait as_trait do |audience| HELLO = "hello #{audience}" # don't do...

relishapp.com

Custom matcher for a single group If you're only going to include a matcher once, you can also use the matcher macro within an example group:

...to re-use a matcher in multiple groups, put it in a module and include it only where you need it. Since each describe block generates an anonymous class internally...

makandra dev

...klass pointing to a RClass that represents our User class and whose m_table includes, inter alia, our declared introduce method. This means that declaring a class Foo internally creates...

...adding namespaces to your code. Another common usage is multiple inheritance as you can include multiple modules with predefined functionality into your class. The same thing is not possible using...

github.com

...given.inspect} not to rhyme with #{expected.inspect}" given.rhymes_with? expected end end end ActiveSupport::TestCase.send :include, RhymeWithMatcher 4) Write a matcher class (for complex matchers) module Aegis module Matchers class CheckPermissions...

...old versions of Rails and RSpec you need to do this instead: ActiveSupport::TestCase.send :include, Aegis::Matchers