### Alba Inertia Naming Convention Examples Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Follows standard Rails conventions for mapping controllers and actions to their expected resource names. For example, `CoursesController#index` expects `CoursesIndexResource` or `CoursesIndexSerializer`. ```ruby # Controller: CoursesController # Action: index # Expected Resource: CoursesIndexResource or CoursesIndexSerializer # Controller: Admin::UsersController # Action: show # Expected Resource: Admin::UsersShowResource or Admin::UsersShowSerializer ``` -------------------------------- ### Serialization with `.to_inertia` Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Use the `.to_inertia` method on a resource instance to get lazy procs and Inertia prop objects suitable for Inertia.js rendering. ```ruby resource = CoursesIndexResource.new(courses: @courses) resource.to_inertia # => { "courses" => , "stats" => } ``` -------------------------------- ### Inertia vs JSON Serialization in Alba Source: https://context7.com/skryukov/alba-inertia/llms.txt Use `to_inertia` for Inertia.js rendering to get lazy procs and prop objects. Use `as_json` for standard API responses to get immediately evaluated JSON data. Both methods are defined within `ApplicationResource` subclasses. ```ruby class UserResource < ApplicationResource attributes :id, :name, :email attribute :profile_stats, inertia: :defer do |user| { posts_count: user.posts.count, followers: user.followers.count } end has_many :recent_posts, serializer: PostResource, inertia: :optional end user = User.find(1) resource = UserResource.new(user) # For Inertia.js rendering - returns lazy procs and prop objects inertia_props = resource.to_inertia # => { # "id" => #, # "name" => #, # "email" => #, # "profile_stats" => #, # "recent_posts" => # # } # For standard JSON API responses - evaluates all values immediately json_data = resource.as_json # => { # "id" => 1, # "name" => "John Doe", # "email" => "john@example.com", # "profile_stats" => { "posts_count" => 42, "followers" => 100 }, # "recent_posts" => [{ "id" => 1, "title" => "Hello" }, ...] # } ``` -------------------------------- ### Serialization with `.as_json` Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Use the `.as_json` method for standard JSON serialization, suitable for Typelizer or API endpoints. ```ruby resource = CoursesIndexResource.new(courses: @courses) resource.as_json # => { "courses" => [...], "stats" => 42 } ``` -------------------------------- ### Configure Alba::Inertia Defaults Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Configure default behaviors for Alba::Inertia, such as the default rendering mode and whether props are lazy by default. ```ruby Alba::Inertia.configure do |config| # Render with Alba resource class by default config.default_render = true # Wrap all props in lambdas by default config.lazy_by_default = true end ``` -------------------------------- ### Integrate Alba::Inertia::Controller in Controllers Source: https://context7.com/skryukov/alba-inertia/llms.txt Include `Alba::Inertia::Controller` in your base controller to enable automatic resource detection and Inertia rendering. This module provides the `render_inertia` method for explicit rendering. ```ruby class InertiaController < ApplicationController include Alba::Inertia::Controller end ``` ```ruby class CoursesController < InertiaController # Auto-detects CoursesIndexResource and passes instance variables def index @courses = Course.all @current_category_id = params[:category_id] # Implicit render_inertia is called via default_render end def show @course = Course.find(params[:id]) # Explicit render with custom component render_inertia "Courses/Show" end def create @course = Course.new(course_params) if @course.save redirect_to courses_path else # Pass validation errors render_inertia inertia: { errors: @course.errors } end end def update @course = Course.find(params[:id]) if @course.update(course_params) redirect_to @course else # Custom serializer with locals render_inertia( serializer: CourseEditResource, locals: { errors: @course.errors, custom: 'props' } ) end end end # Naming convention: # CoursesController#index => CoursesIndexResource or CoursesIndexSerializer # Admin::UsersController#show => Admin::UsersShowResource or Admin::UsersShowSerializer ``` -------------------------------- ### Configure Alba::Inertia Global Settings Source: https://context7.com/skryukov/alba-inertia/llms.txt Customize default rendering, lazy evaluation, and missing serializer behavior in `config/initializers/alba_inertia.rb`. Supports options like :ignore, :log, :raise, or a callable for `on_missing_serializer`. ```ruby # config/initializers/alba_inertia.rb Alba::Inertia.configure do |config| # Render with Alba resource class by default config.default_render = true # Wrap all props in lambdas by default for lazy evaluation config.lazy_by_default = true # How to handle missing serializer/resource classes # Options: :ignore (default), :log, :raise, or a callable (proc/lambda) config.on_missing_serializer = :log # Custom logger (defaults to Rails.logger) config.logger = Rails.logger end ``` -------------------------------- ### Custom Serializer Selection in `render_inertia` Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Specify a custom serializer to be used with the `render_inertia` method. ```ruby render_inertia(serializer: CustomResource) ``` -------------------------------- ### Include Alba::Inertia::Controller in Controllers Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Include the Alba::Inertia::Controller module in your application controllers to leverage Inertia features. ```ruby class InertiaController < ApplicationController include Alba::Inertia::Controller end ``` -------------------------------- ### Include Alba::Inertia::Resource in ApplicationResource Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Include the Alba::Inertia::Resource module in your base resource class to enable Inertia integration. ```ruby class ApplicationResource include Alba::Resource # ... helper Alba::Inertia::Resource end ``` -------------------------------- ### Define Inertia Props with `inertia_prop` Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Alternatively, define Inertia props using the `inertia_prop` method for a more explicit declaration. ```ruby class CoursesIndexResource < ApplicationResource has_many :courses, serializer: CourseResource inertia_prop :courses, optional: true attribute :stats inertia_prop :stats, defer: { merge: true, group: 'analytics' } end ``` -------------------------------- ### Custom Props with `render_inertia` Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Pass custom props to the `render_inertia` method using the `locals` option. ```ruby render_inertia(locals: { custom: 'props'}) ``` -------------------------------- ### Pass Context to Serializers with Serializer Params Source: https://context7.com/skryukov/alba-inertia/llms.txt Pass context to serializers using the `serializer_params` option in `render_inertia` or override `inertia_serializer_params` in `ApplicationController` for default parameters across all renders. ```ruby class ApplicationController < ActionController::Base include Alba::Inertia::Controller private # Default params for all Inertia renders def inertia_serializer_params { current_user: current_user, locale: I18n.locale } end end ``` ```ruby class CoursesController < ApplicationController def index @courses = Course.all # Additional params merged with defaults (takes precedence) render_inertia(serializer_params: { show_drafts: admin? }) end end ``` ```ruby class CoursesIndexResource < ApplicationResource attributes :id, :title # Access params in attribute blocks attribute :can_edit do |course| params[:current_user]&.can_edit?(course) end attribute :localized_title do |course| course.title_for_locale(params[:locale]) end # Conditional inclusion based on params attribute :draft_count, inertia: :optional do |_| params[:show_drafts] ? Course.drafts.count : nil end end ``` -------------------------------- ### Configure Infinite Scrolling with Scroll Props Source: https://context7.com/skryukov/alba-inertia/llms.txt Configure infinite scrolling for `has_many` associations using `inertia: :scroll`. Supports auto-detection of pagination metadata or custom extractors via symbols, Procs, or hashes. ```ruby class PostsIndexResource < ApplicationResource # Auto-detect pagination metadata (scroll_meta, pagy, or Kaminari collection) has_many :posts, serializer: PostResource, inertia: :scroll # Explicit symbol metadata extractor has_many :comments, serializer: CommentResource, inertia: { scroll: :pagination } # Proc metadata extractor has_many :notifications, serializer: NotificationResource, inertia: { scroll: ->(obj) { obj.notification_meta } } # Hash with wrapper and pagination options has_many :items, serializer: ItemResource, inertia: { scroll: { scroll: :meta, wrapper: 'data', page_name: 'page', current_page: 1, previous_page: nil, next_page: 2 } } # Deferred scroll for loading after initial render has_many :feed, serializer: FeedResource, inertia: { scroll: :pagination, defer: true, group: 'infinite' } end ``` ```ruby class PostsController < InertiaController def index @pagy, @posts = pagy(Post.all) # Resource auto-detects @pagy for scroll metadata end end ``` -------------------------------- ### Controller Integration with Inertia Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Integrate Alba::Inertia::Controller into your Rails controllers to automatically serialize resources for Inertia.js. ```ruby class CoursesController < InertiaController def index @courses = Course.all @current_category_id = params[:category_id] # Auto-detects CoursesIndexResource and passes instance variables end def show @course = Course.find(params[:id]) # With a custom component render_inertia "Courses/Show" end def create @course = Course.new(course_params) if @course.save redirect_to courses_path else # With errors render_inertia inertia: { errors: user.errors } end end end ``` -------------------------------- ### Add Alba::Inertia to Gemfile Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Include the necessary gems in your Gemfile to use Alba and Inertia Rails integration. ```ruby gem "alba" gem "inertia_rails" gem "alba-inertia" ``` -------------------------------- ### Inheritance of Inertia Metadata Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Metadata defined in parent resources is inherited by child resources. Child resources can override parent metadata. ```ruby class BaseResource < ApplicationResource attribute :created_at, inertia: :optional end class CourseResource < BaseResource attributes :id, :title # Inherits created_at with optional: true end class ExtendedCourseResource < CourseResource inertia_prop :created_at, defer: true # Override parent's optional: true end ``` -------------------------------- ### Pass Serializer Params in Controller Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Pass context like the current user to serializers using the `serializer_params` option in `render_inertia`. Access these params within your resource using the `params` method. ```ruby class CoursesController < InertiaController def index @courses = Course.all render_inertia(serializer_params: { current_user: current_user }) end end ``` ```ruby class CoursesIndexResource < ApplicationResource attributes :id, :title attribute :can_edit do |course| params[:current_user]&.can_edit?(course) end end ``` -------------------------------- ### Define Inertia Props with Inline Options Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Define Inertia props directly within the resource class using the `inertia:` option for various prop types like optional, deferred, merge, scroll, and once. ```ruby class CoursesIndexResource < ApplicationResource # Simple attributes attributes :id, :title # Optional prop (loaded only when requested) has_many :courses, serializer: CourseResource, inertia: :optional # Deferred prop (loaded in separate request) has_many :students, serializer: StudentResource, inertia: :defer # Deferred with options attribute :stats, inertia: { defer: { group: 'analytics', merge: true } } do |object| expensive_calculation(object) end # Merge prop (for partial reloads) has_many :comments, serializer: CommentResource, inertia: { merge: { match_on: :id } } # Scroll prop with auto-detection. # Checks object for `scroll_meta` and `pagy` attributes, or object being a Kaminari collection. has_many :items, inertia: :scroll # Scroll prop with explicit metadata has_many :items, inertia: { scroll: :meta } has_many :items, inertia: { scroll: ->(obj) { obj.meta } } has_many :items, inertia: { scroll: ->(obj) { obj.meta }, wrapper: 'data' } # Once prop has_many :plans, inertia: :once has_many :plans, inertia: { once: { key: 'active_plans', expires_in: 1.hour, fresh: ->(obj) { obj.fresh? } } } end ``` -------------------------------- ### Inherit Inertia Metadata from Parent Resources Source: https://context7.com/skryukov/alba-inertia/llms.txt Inertia metadata is inherited from parent resources, allowing for DRY configurations. Child classes can override or add new deferred attributes. ```ruby class BaseResource < ApplicationResource # Common timestamp handling across all resources attribute :created_at, inertia: :optional attribute :updated_at, inertia: :optional end class CourseResource < BaseResource attributes :id, :title, :description # Inherits created_at and updated_at with optional: true end class ExtendedCourseResource < CourseResource # Override parent's optional: true with defer: true inertia_prop :created_at, defer: true # Add new deferred attribute attribute :enrollment_count, inertia: :defer do |course| course.enrollments.count end end ``` -------------------------------- ### Define Inertia Props in Alba Resource Source: https://context7.com/skryukov/alba-inertia/llms.txt Include `Alba::Inertia::Resource` in your Alba resource classes to use Inertia-specific options like `:optional`, `:defer`, `:merge`, `:scroll`, `:once`, and `:always` on attributes and associations. ```ruby class ApplicationResource include Alba::Resource # Add Inertia support as a helper helper Alba::Inertia::Resource end class CoursesIndexResource < ApplicationResource # Simple attributes (wrapped in lazy procs by default) attributes :id, :title # Optional prop - loaded only when explicitly requested via partial reload has_many :courses, serializer: CourseResource, inertia: :optional # Deferred prop - loaded in separate request after initial page load has_many :students, serializer: StudentResource, inertia: :defer # Deferred with merge and grouping options attribute :stats, inertia: { defer: { group: 'analytics', merge: true } } do |object| expensive_calculation(object) end # Merge prop - for partial reloads with ID matching has_many :comments, serializer: CommentResource, inertia: { merge: { match_on: :id } } # Scroll prop with auto-detection (checks for scroll_meta, pagy, or Kaminari) has_many :items, inertia: :scroll # Once prop - loaded once and cached in session has_many :plans, inertia: :once # Once prop with options has_many :settings, inertia: { once: { key: 'user_settings', expires_in: 1.hour } } # Always prop - always included in responses attribute :flash_messages, inertia: :always do flash.to_h end # Cache prop - cached with a key attribute :cached_data, inertia: { cache: 'data_v1' } end resource = CoursesIndexResource.new(courses: @courses) resource.to_inertia # => { "courses" => , "stats" => } resource.as_json # => { "courses" => [...], "stats" => 42 } ``` -------------------------------- ### Set Default Serializer Params in Application Controller Source: https://github.com/skryukov/alba-inertia/blob/main/README.md Override `inertia_serializer_params` in your `ApplicationController` to provide default parameters for all Inertia renders. These defaults are merged with `serializer_params` from `render_inertia`, with the latter taking precedence. ```ruby class ApplicationController < ActionController::Base include Alba::Inertia::Controller private def inertia_serializer_params { current_user: current_user, locale: I18n.locale } end end ``` -------------------------------- ### CamelCase Key Transformation with to_inertia Source: https://context7.com/skryukov/alba-inertia/llms.txt Alba's key transformation, like `transform_keys :lower_camel`, is supported in `to_inertia` output, enabling camelCase keys for JavaScript frontends. This is useful for maintaining consistency between backend data structures and frontend expectations. ```ruby class UserProfileResource < ApplicationResource transform_keys :lower_camel attributes :first_name, :last_name, :email_address attribute :phone_number, inertia: :optional end resource = UserProfileResource.new(user) resource.to_inertia # => { # "firstName" => #, # "lastName" => #, # "emailAddress" => #, # "phoneNumber" => # # } ``` -------------------------------- ### Declare Inertia Metadata Separately with inertia_prop Source: https://context7.com/skryukov/alba-inertia/llms.txt Use the `inertia_prop` method to define Inertia metadata for existing attributes or associations, allowing for a cleaner separation of concerns. This method supports options like `optional`, `defer`, `merge`, and `cache`. ```ruby class DashboardResource < ApplicationResource # Define attribute normally has_many :courses, serializer: CourseResource # Then add Inertia metadata separately inertia_prop :courses, optional: true # Regular attribute with separate metadata attribute :stats do |dashboard| calculate_stats(dashboard) end inertia_prop :stats, defer: { merge: true, group: 'analytics' } # Merge with custom options attribute :metadata inertia_prop :metadata, merge: { match_on: :id, prepend: 'meta_' } end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.