### Ruby: Install ActiveRecordCompose Gem Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Provides the necessary commands to install the `active_record_compose` gem using Bundler. This involves adding the gem to the Gemfile and then running the bundle command. ```ruby gem 'active_record_compose' ``` ```sh $ bundle ``` -------------------------------- ### Unified Error Handling in ActiveRecordCompose (Ruby) Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Demonstrates how validation errors from inner models are collected into the composed model. It shows an example of creating a UserRegistration model with invalid data and then accessing its full error messages. This helps in debugging and providing user feedback. ```ruby user_registration = UserRegistration.new( email: "foo@example.com", email_confirmation: "BAZ@example.com", age: 18, terms_of_service: true, ) user_registration.save # => false user_registration.errors.full_messages # => [ # "Name can't be blank", # "Firstname can't be blank", # "Lastname can't be blank", # "Email confirmation doesn't match Email" # ] ``` -------------------------------- ### Ruby: ActiveRecordCompose Attribute Delegation Example Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Illustrates the use of `delegate_attribute` within an ActiveRecordCompose model. This method allows direct access and manipulation of attributes from the composed underlying ActiveRecord models, and these delegated attributes are also included in the `#attributes` output of the composed object. ```ruby delegate_attribute :name, :email, to: :account delegate_attribute :firstname, :lastname, :age, to: :profile # They are also included in `#attributes`: registration.attributes # => { # "terms_of_service" => true, # "email" => nil, # "name" => "foo", # "age" => nil, # "firstname" => nil, # "lastname" => nil # } ``` -------------------------------- ### Dynamically Adding Models to ActiveRecordCompose (Ruby) Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Warns against adding models to the `models` array after validation has started, such as within `after_validation` or `before_save` callbacks. The example shows that models added dynamically will not have their validations run for the current save cycle, which is intentional behavior for advanced use cases. ```ruby class Example < ActiveRecordCompose::Model before_save { models << AnotherModel.new } end ``` -------------------------------- ### Callback Ordering with #persisted? in ActiveRecordCompose (Ruby) Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Explains how the `#persisted?` method determines which callbacks are fired during the save process, mirroring standard ActiveRecord behavior. It includes a ComposedModel example demonstrating the execution flow of before_save, before_create/update, after_create/update, and after_save callbacks based on the #persisted? result. ```ruby class ComposedModel < ActiveRecordCompose::Model before_save { puts "before_save" } before_create { puts "before_create" } before_update { puts "before_update" } after_create { puts "after_create" } after_update { puts "after_update" } after_save { puts "after_save" } def persisted? account.persisted? end end # When persisted? == false model = ComposedModel.new model.save # => before_save # => before_create # => after_create # => after_save # When persisted? == true model = ComposedModel.new def model.persisted?; true; end model.save # => before_save # => before_update # => after_update # => after_save ``` -------------------------------- ### Ruby: Usage of UserRegistration Form Object Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Shows two ways to use the `UserRegistration` composed form object: standalone script usage with `update!` and integration into a Rails controller using strong parameters. Both methods demonstrate saving composed data atomically. ```ruby # === Standalone script === registration = UserRegistration.new registration.update!( name: "foo", email: "bar@example.com", firstname: "taro", lastname: "yamada", age: 18, email_confirmation: "bar@example.com", terms_of_service: true, ) # === Or, in a Rails controller with strong parameters === class UserRegistrationsController < ApplicationController def create @registration = UserRegistration.new(user_registration_params) if @registration.save redirect_to root_path, notice: "Registered!" else render :new end end private def user_registration_params params.require(:user_registration).permit( :name, :email, :firstname, :lastname, :age, :email_confirmation, :terms_of_service ) end end ``` -------------------------------- ### Ruby: Create a Composed Form Object with ActiveRecordCompose Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Demonstrates how to create a composed form object `UserRegistration` using ActiveRecordCompose. It initializes associated models, defines attributes, validations, callbacks, and uses `delegate_attribute` for attribute delegation. This object manages multiple models within a single interface. ```ruby class UserRegistration < ActiveRecordCompose::Model def initialize(attributes = {}) @account = Account.new @profile = @account.build_profile super(attributes) models << account << profile end attribute :terms_of_service, :boolean validates :terms_of_service, presence: true validates :email, confirmation: true after_commit :send_email_message delegate_attribute :name, :email, to: :account delegate_attribute :firstname, :lastname, :age, to: :profile private attr_reader :account, :profile def send_email_message SendEmailConfirmationJob.perform_later(account) end end ``` -------------------------------- ### Ruby: Define ActiveRecord Models Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Defines two ActiveRecord models, Account and Profile, with associations and validations. These models serve as the base for composition in ActiveRecordCompose. ```ruby class Account < ApplicationRecord has_one :profile validates :name, :email, presence: true end class Profile < ApplicationRecord belongs_to :account validates :firstname, :lastname, :age, presence: true end ``` -------------------------------- ### Destroy Option for Models in ActiveRecordCompose (Ruby) Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Illustrates the 'destroy: true' option, which enables the deletion of a model on save instead of persisting it. It also shows how to use a conditional lambda for destroy operations, allowing for dynamic deletion based on specific criteria. ```ruby models.push(profile, destroy: true) models.push(profile, destroy: -> { profile_field_is_blank? }) ``` -------------------------------- ### I18n Support for Validation Errors (YAML) Source: https://github.com/hamajyotan/active_record_compose/blob/main/README.md Shows how to configure I18n support for ActiveRecord::RecordInvalid exceptions raised by ActiveRecordCompose. This allows for localized error messages when validation fails. Ensure the provided locale entries are present in your application's i18n configuration. ```yaml en: activemodel: errors: messages: record_invalid: 'Validation failed: %{errors}' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.