Rails 7.1 features
Tags: rails, tutorial
Published: 2022-12-02 13:57:32

There are many new features and improvements in Rails 7.1 that can be useful for developers. Here are a few tips and highlights of what's new in Rails 7.1:

1. The ApplicationRecord class now includes a **descendants** method, which returns an array of all subclasses of the class. This can be useful for performing operations on all subclasses of a class, such as calling a method on each subclass.

2. Rails 7.1 includes a new **active_storage_validations** gem, which provides validations for Active Storage attachments. This can be useful for ensuring that attachments meet certain criteria, such as file size and content type.

3. Rails 7.1 now allows you to define custom matchers for **assert_redirected_to** and **assert_performed_jobs** in RSpec. This can make it easier to test redirects and background jobs in your applications.

4. The **ActionController::Parameters** class now includes a **permit**! method, which allows you to permit all attributes of a hash at once. This can be useful for cases where you want to allow all attributes to be passed to a controller action.

5. Rails 7.1 includes a new **ActiveSupport::Testing::TimeHelpers** module, which provides methods for manipulating time in tests. This can be useful for testing time-dependent code, such as timeouts and expiration dates.

Rails 7.1 includes many new features and improvements that can make development easier and more efficient. These are just a few examples of what's new in Rails 7.1, and there are many other new features and improvements that you can explore and take advantage of in your applications.

Here are a few examples of how you can use some of these new features in your Rails applications:

1. You can use the descendants method of the ApplicationRecord class to perform operations on all subclasses of a class. For example, you might have a base Animal class and several subclasses for different types of animals, such as Dog, Cat, and Fish. You can use the descendants method to call a method on each of these subclasses, such as a breathe method that each animal class must implement:

```ruby
class Animal < ApplicationRecord
  def self.breathe
    raise NotImplementedError, "Must be implemented by subclasses"
  end
end

class Dog < Animal
  def self.breathe
    puts "Bark!"
  end
end

class Cat < Animal
  def self.breathe
    puts "Meow!"
  end
end

class Fish < Animal
  def self.breathe
    puts "Gulp!"
  end
end

# Call the breathe method on each Animal subclass
Animal.descendants.each { |klass| klass.breathe }
# Output: Bark!
#         Meow!
#         Gulp!
```

n this example, the descendants method is used to call the breathe method on each Animal subclass, which prints a different message for each animal.

2. You can use the active_storage_validations gem to validate attachments in your Active Storage models. For example, you might have an Image model that has an image attachment. You can use the validates method to add validations for the attachment, such as minimum and maximum size and content type:

```ruby
class Image < ApplicationRecord
  has_one_attached :image

  # Validate the image attachment
  validates :image, active_storage_validations: true
end
```

In this example, the validates method is used to add validations for the image attachment, using the active_storage_validations gem. This ensures that any images attached to the Image model meet the specified criteria.

3. You can use custom matchers in RSpec to test redirects and background jobs in your Rails application. For example, you might have a create action in a PostsController that creates a new Post and then redirects to the show action for the Post. You can use a custom matcher to test that the create action correctly redirects to the show action:

```ruby
# In posts_controller_spec.rb

# Define a custom matcher for redirects to the show action
RSpec::Matchers.define :redirect_to_show_action do |expected|
  match do |actual|
    expect(actual).to redirect_to(post_path(expected))
  end
end

# Test the create action
describe "POST #create" do
  it "redirects to the show action" do
    post :create, params: { post: { title: "Hello, world!" } }
    expect(response).to redirect_to_show_action(Post.last)
  end
end
```