Skip to content

roonglit/engine_stimulus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rails Engine Stimulus Integration Examples

This repository demonstrates two different approaches for integrating Stimulus JavaScript controllers in Rails engines, each with distinct trade-offs and use cases.

πŸš€ Quick Overview

Engine Strategy Best For
blogh_isolate Isolated Setup Independent modules, microservices, reusable components
blogh_main_app_layout Main App Integration Feature extensions, admin panels, content management

πŸ“ Repository Structure

engine_stimulus/
β”œβ”€β”€ blogh_isolate/           # πŸ”’ Isolated Stimulus engine
β”‚   β”œβ”€β”€ app/javascript/blogh/    # Engine-specific JS structure
β”‚   β”œβ”€β”€ config/importmap.rb      # Custom importmap configuration
β”‚   └── app/helpers/             # Custom importmap helper
β”‚
β”œβ”€β”€ blogh_main_app_layout/   # 🏠 Main app integrated engine  
β”‚   β”œβ”€β”€ app/javascript/controllers/blogh/  # Main app JS structure
β”‚   └── app/views/               # Uses main app layout
β”‚
└── README.md               # This file

πŸ”’ Approach 1: Isolated Engine (blogh_isolate)

What It Is

A completely self-contained Rails engine with its own Stimulus setup, importmap, and JavaScript bundle.

Key Features

  • βœ… Own importmap configuration
  • βœ… Custom blogh_importmap_tags helper
  • βœ… Independent asset compilation
  • βœ… Engine-specific layout
  • βœ… No main app JavaScript dependencies

JavaScript Architecture

// Engine creates its own Stimulus application
import { Application } from "@hotwired/stimulus"
const application = Application.start()

// Controllers loaded from engine namespace
eagerLoadControllersFrom("controllers", application)

When to Choose

  • 🎯 Microservice Architecture: Engine can be deployed independently
  • 🎯 Reusable Components: Same engine used across multiple apps
  • 🎯 Version Independence: Engine JS updates don't affect main app
  • 🎯 Team Isolation: Different teams work on engine vs main app
  • 🎯 Complex UI: Engine has sophisticated JavaScript requirements

Trade-offs

  • βž• Complete isolation - no conflicts with main app
  • βž• Independent deployment - can be distributed as gem
  • βž• Clear boundaries - easy to understand ownership
  • βž– More setup complexity - custom importmap configuration required
  • βž– Larger bundle size - duplicate dependencies
  • βž– Manual integration - requires custom helper usage

🏠 Approach 2: Main App Integration (blogh_main_app_layout)

What It Is

A lightweight Rails engine that leverages the main application's existing Stimulus setup and layout.

Key Features

  • βœ… Uses main app's importmap
  • βœ… Shares main app's Stimulus instance
  • βœ… Inherits main app styling
  • βœ… Automatic controller discovery
  • βœ… Single asset bundle

JavaScript Architecture

// Controllers automatically discovered by main app
// app/javascript/controllers/blogh/blogh_controller.js
// Registered as "blogh" controller in main app's Stimulus

When to Choose

  • 🎯 Feature Extensions: Adding functionality to existing app
  • 🎯 Admin Panels: Management interfaces for main app
  • 🎯 Content Management: Editorial tools within main app
  • 🎯 Consistent UX: Want engine to match main app exactly
  • 🎯 Simple Integration: Quick setup with minimal configuration

Trade-offs

  • βž• Simple setup - no custom JavaScript configuration
  • βž• Consistent UX - automatically matches main app
  • βž• Shared dependencies - smaller overall bundle size
  • βž• Automatic integration - works with main app's importmap
  • βž– Coupled to main app - cannot function independently
  • βž– Shared fate - main app JS changes affect engine
  • βž– Less isolation - potential for conflicts

πŸ€” Decision Matrix

Use this matrix to choose the right approach:

Requirement Isolated Main App
Independent deployment βœ… ❌
Quick setup ❌ βœ…
Consistent styling ❌ βœ…
Version independence βœ… ❌
Reusable across apps βœ… ❌
Small bundle size ❌ βœ…
Team isolation βœ… ❌
Simple maintenance ❌ βœ…

πŸš€ Getting Started

Option 1: Try the Isolated Engine

cd blogh_isolate
bundle install
cd test/dummy
rails server
# Visit http://localhost:3000/blogh

Option 2: Try the Main App Integration

cd blogh_main_app_layout  
bundle install
cd test/dummy
rails server
# Visit http://localhost:3000/blogh

πŸ“ Implementation Details

Isolated Engine Key Code

# config/importmap.rb
pin_all_from Blogh::Engine.root.join("app/javascript/blogh/controllers"), 
             under: "controllers", 
             to: "blogh/controllers"

# Custom helper
def blogh_importmap_tags(entry_point = "blogh/application")
  importmap = Blogh.configuration.importmap
  # Generate engine-specific importmap...
end

Main App Integration Key Code

# Main app's config/importmap.rb automatically includes:
pin_all_from "app/javascript/controllers", under: "controllers"
# This picks up controllers/blogh/ directory from engine

# Engine controller naming:
# controllers/blogh/post_controller.js β†’ "blogh--post" 

πŸ”§ Development Workflow

For Isolated Engine

  1. Develop engine in isolation
  2. Test with dummy app
  3. Package as gem
  4. Install in main app
  5. Use blogh_importmap_tags helper

For Main App Integration

  1. Develop engine alongside main app
  2. Controllers automatically discovered
  3. Test integration continuously
  4. Deploy together

🌟 Real-World Examples

Isolated Engine Use Cases

  • E-commerce Engine: Product catalog with advanced filtering
  • CMS Engine: Content management with rich editor
  • Analytics Dashboard: Complex data visualization
  • Chat Engine: Real-time messaging with WebSocket

Main App Integration Use Cases

  • Admin Interface: User management for main app
  • Blog Module: Simple blog functionality
  • Comment System: User comments on main app content
  • Settings Panel: Application configuration interface

🚧 Migration Between Approaches

From Main App β†’ Isolated

  1. Create engine importmap configuration
  2. Set up isolated JavaScript structure
  3. Implement custom importmap helper
  4. Create engine-specific layout
  5. Update controller references

From Isolated β†’ Main App

  1. Move controllers to main app structure
  2. Remove custom importmap configuration
  3. Update layout to use main app's
  4. Remove custom helper usage
  5. Update controller naming

πŸ“š Further Reading

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

πŸ“„ License

This project is available as open source under the terms of the MIT License.


Choose your approach and start building! Both examples provide complete, working implementations you can learn from and adapt to your needs.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published