Skip to content

Preact Ruby Quickstart

bcollinsNR edited this page Oct 24, 2014 · 5 revisions

Installation

In your Gemfile:

gem 'preact', '~> 1.1.0'

Then do a bundle install to get the gem.

Now, obtain your Preact Project Code and API Secret from the API settings page. Then, in your ruby app directory, run the generator:

rails g preact your-code-1234 api-secret-xyzw

This will generate an initializer and a preact.yml config for you.

Configuration

current_user_getter

If you're using Devise/Warden for authentication, your application_controller will already have a current_user helper method and Preact will automatically use that to identify the logged-in user. If you are using a different authentication system, please see our current_user_getter config help.

current_account_getter

Preact also needs to know which account the current user is acting within. If you already have an application_controller method or variable which contains the account model, simply uncomment the following line in preact.yml and replace with the name of the method or instance variable. Remember to prefix an instance variable with the @ symbol.

current_account_getter: "@current_account"

The specified variable or method should return the account model for the current user.

Note: the model which represents the current account might be called Project, Company, Organization, Instance, Account or something else depending on what you call your customers and accounts. When we refer to your "account model" here, we're talking about whatever that model is called.

user.to_preact method

To send the appropriate User information to Preact about your users, you should add a to_preact instance method to your User model. The following 4 fields are the minimum required fields, but there are others you can send.

class User < ActiveRecord::Base
  def to_preact
    {
      name: self.name,
      email: self.email,
      uid: self.id,
      created_at: self.created_at.to_i
    }
  end
end

account.to_preact method

Similar to the User model, we need to know how to translate the fields on your account model to our standard Preact fields. If your account model is called Project, it might look something like this. The two required fields are name and id but there are other account fields you might want to include.

class Account < ActiveRecord::Base
  def to_preact
    {
      name: self.name,
      id: self.id,
      license_status: self.account_status
    }
  end
end

Testing it out

That should be all the setup required. Now you should boot up your local development environment and click through a few pages after logging in. If all is good, you'll start to see events showing up in the API debug view or in the API Error view.

You'll also see [Preact] ... debug output in your rails server log which may report errors if it's not logging data, and will show you the event parameters which are being transmitted.

If you have any trouble or further questions, please contact your Customer Success Manager or submit a ticket.