No commit activity in last 3 years
No release in over 3 years
A View-Component framework for Rails applications.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

>= 0
>= 0
>= 0
>= 0

Runtime

 Project Readme

ActiveComponent

Gem Version Dependency Status Build Status Code Climate Coverage Status

A View-Component framework for Rails applications.

Usage

Add the gem to your gemfile

gem "active-component"

Or install it manually

$ gem install active-component

You will have accessible all the modules from the gem in your Rails application.

In order to create a new View-Component item in your application you can use a generator.

$ bundle exec rails generate component activity
      create  app/components/application_component.rb
      create  app/components/activity_component.rb
      create  app/templates/activity_component.mustache

You're going to be able to add methods to your component class and call them from the mustache template

class ActivityComponent < ActiveComponent::Base
  def name
    "Antonio"
  end

  def email
    "cavjzz@gmail.com"
  end

  def message
    "Has just signed up!"
  end
end

And in your template:

<li>
  <a href="mailto:{{ email }}">
    {{ name }}
  </a>
  <span>{{ message }}</span>
</li>

In your rails view

<%= render_component(:activity) %>

Features

Controller context from your component class

You can access the controller context from where the component is instanced

class ActivityComponent < ActiveComponent::Base
  def name
    controller.current_user.name
  end

  def message
    controller.activity.message
  end
end

Passing variables

You can pass variables in the object instance

class ActivityComponent < ActiveComponent::Base
  attr_accessor :name
end
<%= render_component(:activity, :name => current_user.name) %>

Collection Rendering

You can render component collections from the view or from another component

<%= render_component(:activity, :collection => activities, :as => :activity) %>

And pass local variables

<%= render_component(:activity, :collection => activities, :as => :activity, :locals => { :foo => "foo" }) %>

Where the :collection is an array of elements (database records, hashes, stings, etc), and the :as is the name of the instance attribute defined in your component class.

NOTICE In order to use the collection feature you have to define with attr_accessor the name of the attribute to be received by the component class.

Sinatra

You can add this gem to sinatra, with the following configuration in your app

require 'active_component'

class App < Sinatra::Base

  helpers ActiveComponent::Base

  get '/' do
    render_component(:activity)
  end

end

Future short term features

  • Dynamic instance variables, get rid of attr_accessor :foo
  • Support for more templating engines.
  • Dynamic attributes (get rid of attr accessible)

Inspired from Cells framework done by Apotonick.

This project rocks and uses MIT-LICENSE.