Giter Site home page Giter Site logo

starburst's Introduction

Starburst

Build Status Maintainability Test Coverage

Starburst allows you to show messages to logged in users within your Rails app. Once the user closes the message, they won't see it again.

You can target messages to particular groups of users, based on their database attributes or your own methods on the User class. For instance, you might send a message only to users on your premium plan.

Starburst remembers on the server who has closed which message. Therefore, a user who closes a message on their desktop won't see it again on their mobile device. Starburst doesn't use cookies, so a user won't see an announcement they've already read if they switch devices or clear their cookies.

Starburst is in production on Cook Smarts, where it has served announcements to thousands of users.

Announcement in ZURB Foundation

An announcement delivered by Starburst, on a Rails app using ZURB Foundation

Use cases

Use Starburst to share announcements with your users, like:

  • A new feature
  • Upcoming downtime
  • A coupon to upgrade to a premium plan

Users will see the message until they dismiss it, and then won't see it again.

A user will not see the next announcement until they acknowledge the previous one (i.e. users are shown one announcement at a time). Announcements are delivered earliest first. Be sure to schedule announcements so they appear only while they're relevant.

Requirements

Authentication

Starburst needs to know who is logged in to your app. If you are using Devise, Clearance, or another authentication library that sets a current_user method, you're all set.

If you use a different authentication system that does not set a current_user method, tell Starburst what method your library uses.

Ruby and Rails

Starburst works on Rails 4.2, 5.0, 5.1, 5.2, 6.0, and 6.1. It should work with the same Ruby runtime versions supported by each framework version.

Installation

Add Starburst to your Gemfile:

gem 'starburst'

Migrate your database:

$ rake starburst:install:migrations
$ rake db:migrate

Add the following line to your ApplicationController:

helper Starburst::AnnouncementsHelper

Add the following line to your routes file:

mount Starburst::Engine => '/starburst'

Add the following line to your application.js file:

//= require starburst/starburst

Getting started

Add an announcement partial to your app's layout

Starburst comes with pre-built announcement boxes for sites using ZURB Foundation and Bootstrap. It also includes an announcement box with no assigned styles.

Add one of the lines below your application layout view at app/views/layouts/application.html.erb, right above <%= yield =>. You can place the partials anywhere, of course; this is just the most common location.

Bootstrap

<%= render partial: 'announcements/starburst/announcement_bootstrap' %>

ZURB Foundation

<%= render partial: 'announcements/starburst/announcement_foundation' %>

Custom styles

<%= render partial: 'announcements/starburst/announcement' %>

Set your own styles. Use #starburst-announcement ID for the box, and the #starburst-close for the close button.

Add an announcement

Starburst doesn't have an admin interface yet, but you can add announcements through your own code.

Starburst::Announcement.create(body: 'Our app now features lots of balloons! Enjoy!')

This will present an announcement to every user of the app. Once they dismiss the announcement, they won't see it again.

Find out more about scheduling announcements and targeting them to specific users.

Scheduling announcements

You can schedule announcements as follows:

start_delivering_at - Do not deliver this announcement until this date.

stop_delivering_at - Do not show this announcement to anyone after this date, not even to users who have seen the message before but not acknowledged it.

Starburst::Announcement.create(
  body: 'Our app now features lots of balloons! Enjoy!',
  start_delivering_at: Date.current,
  stop_delivering_at: Date.current.advance(days: 10)
)

Targeting announcements

You can target announcements to particular users by setting the limit_to_users option.

The code below targets the announcement to users with a subscription field equal to gold.

Starburst::Announcement.create(
  body: '<a href="/upgrade">Upgrade to platinum</a> and save 10% with coupon code XYZ!',
  limit_to_users: [
    {
      field: 'subscription',
      value: 'gold'
    }
  ]
)

Advanced configuration

Base controller

By default, Starburst::AnnouncementsController will inherit from ApplicationController. If you need to change that setting in order to have access to the configured current_user_method, just change the base_controller setting:

Starburst.configuration do |config|
  config.base_controller = 'AuthenticatedController'
end

Current user

Most Rails authentication libraries (like Devise and Clearance) place the current user into the current_user method. If your authentication library uses a different method, create an initializer file for Starburst at config/initializers/starburst.rb and add the code below, replacing current_user with the name of the equivalent method in your authentication library.

Starburst.configuration do |config|
  config.current_user_method = :current_user
end

Targeting by methods rather than fields

With targeting, you can limit which users will see a particular announcement. Out of the box, Starburst allows you to limit by fields in the database. However, your User model may have methods that don't directly map to database fields.

For instance, your User model might have an instance method free? that returns true if the user is on a free plan and false if they are on a paid plan. The actual field in the database may be called something different.

You can target based on methods that are not in the database, but you must specify those methods in a Starburst initializer. Create an initializer for Starburst at config/initializers/starburst.rb and add the code below:

Starburst.configuration do |config|
  config.user_instance_methods = %i[free?]
end

user_instance_methods is an array, so you can specify more than one method. All of the methods will be available for targeting, as if they were fields.

Upgrading

From 0.9.x to 1.0

This guidance applies only to users of Starburst who are upgrading from 0.9.x to 1.0.

IMPORTANT: This version introduces a uniqueness constraint on the AnnouncementView table. Before installing, you must find and clear out duplicate announcement views in the table:

  1. In console on both the development and production environments, run this to find duplicate announcement views:
Starburst::AnnouncementView.select(%i[announcement_id user_id]).group(:announcement_id, :user_id).having('COUNT(*) > 1').count
  1. Delete duplicate announcement views as necessary so you have only one of each. Use the destroy method on each individual view
  2. Run rake starburst:install:migrations and then rake db:migrate on your development environment, then push to production

Contributors

Thanks to @jhenkens for fixes, performance improvements, and ideas for showing announcements to non-logged-in users.

starburst's People

Contributors

chamnap avatar csm123 avatar danieldraper avatar dependabot-preview[bot] avatar erichmachado avatar mikdiet avatar yogodoshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

starburst's Issues

Feedback and some possible 'polish' enhancements

First, I wanted to pop in and say keep up the great work! I haven't gotten far enough along with my current projects to start implementing this type of functionality, but I will be looking for options like this in the near future and definitely keeping an eye on this to fill the role.

As I'm sure you're all very aware, the default flash notice behavior and styling is almost always inadequate (definitely for consumer facing projects). A great way to add some 'polish' to help keep our more 'design-challenged' dev colleagues from pissing off designers more than usual ;) could be to add some additional styling and behavior options.

As an example, I've used Snackbar.js in a project recently to address both concerns with flash notices and it's been a big hit all around. Sadly, I'm swamped and will have to be that guy and just offer this feedback/suggestion issue without a gist or small pull request to contribute for now. :(

However! If this ends up being what I use to fill this role in any current projects, I'll be sure to pay it forward and offer a contribution (gist or PR) on whatever is most appropriate by then. Again, thanks for helping make the Rails ecosystem awesome. :)

Performance for finding announcement for current user (and more)

I have been looking at rolling my own sitewide announcements for a site recently, and saw this gem linked from Stackoverflow and thought I'd at least check it out. I apologize for not breaking these up - I'm feeling lazy today.

In looking at your code for selecting announcements for a specific user, the process of reducing all possible announcements based on user attributes seems potentially very costly, in the rare case that there are a lot of tightly targeted active announcements. I can't think of an overall better way while maintaining that feature, but I did notice a few things that might be worthwhile to implement.

It might be worth considering checking the announcement attributes in batches, rather than loading all into memory at once.

I would also lazily create the user_as_array variable upon finding the first announcement that is targeted.

In your scope for ordering by start_delivery_at, I would perform a second sort based on creation date for those announcements where thestart_delivery_at is NULL, or where two have an equal date, but that would just be my preference

Lastly, and unrelated, have you thought about using cookies or the session to handle hiding announcements for non-current users? I'd been thinking of doing that and then merging the cookie into the database of viewed announcements upon signin.

How to configure starburst without devise?

I am using an open source Rails app. In the

class ApplicationController < ActionController::Base
  def current_user
    #...
    #find the current user form the session.
    #...
    @current_user
  end
  helper_method :current_user
end

The user is keep getting the notification even if he dismiss it.
The logs:

Started POST "/starburst/announcements/1/mark_as_read" for ::1 at 2020-04-17 13:26:29 +0300
Processing by Starburst::AnnouncementsController#mark_as_read as JS
  Parameters: {"utf8"=>"✓", "commit"=>"", "id"=>"1"}
  Starburst::Announcement Load (0.3ms)  SELECT  "starburst_announcements".* FROM "starburst_announcements" WHERE "starburst_announcements"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/starburst-9dd6d5c77ada/app/controllers/starburst/announcements_controller.rb:6
Completed 422 Unprocessable Entity in 4ms (Views: 0.5ms | ActiveRecord: 0.3ms)

I have tried to specify the current_user in the initializer and get the same results.

Starburst.configuration do |config|
  config.current_user_method = "current_user"
end

Thanks.

Error when dismissing Announcement

Seems that we get a missing field error from Post Gres

PG::UndefinedColumn: ERROR: column starburst_announcement_views.user does not exist
LINE 1: ...ews".* FROM "starburst_announcement_views" WHERE "starburst...
^
: SELECT "starburst_announcement_views".* FROM "starburst_announcement_views" WHERE "starburst_announcement_views"."user" = 7 AND "starburst_announcement_views"."announcement" = 12 LIMIT 1
Completed 500 Internal Server Error in 19ms

ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column starburst_announcement_views.user does not exist
LINE 1: ...ews".* FROM "starburst_announcement_views" WHERE "starburst...

I assume its the controller needs updating as teh field name is user_id not user?

Active announcement

What would be the easiest way to know which announcement is currently active, if you display all the announcements? Thx

Runtime error for ActiveRecordBase::sanitize call in Starburst

Using Rails 6.1.4.4 and ruby '2.6.9' with the latest (1.0.3) Starburst gem, I get the following run time error

undefined method `sanitize' for #Class:0x00007fd49fa3ddd0
Did you mean? sanitize_sql

In models/starburst/announcement.rb
scope :unread_by, lambda {|current_user|
joins("LEFT JOIN starburst_announcement_views ON
starburst_announcement_views.announcement_id = starburst_announcements.id AND
starburst_announcement_views.user_id = #{Announcement.sanitize(current_user.id)}")
.where("starburst_announcement_views.announcement_id IS NULL AND starburst_announcement_views.user_id IS NULL")
}

According to this sanitize was removed and replaced.

Sanitize removed from Rails 5.1.1

Hi,

i am trying to update my app to Rails 5.1.1 - but announcement seems to be using "sanitize" which is removed from 5.1.1 - and I am getting errors.

Is it possible you change this so that the gem can work with 5.1.1?

Thx

Getting 'false' for respond_to?(Starburst.current_user_method) in Starburst::AnnouncementController

mark_as_read fails because of this. If I remove this check, everything seems to work but I'm not sure that's a good idea.

I wonder if I missed a step because in my installation I didn't create a new ApplicationController as per instruction:

"Add the following line to your ApplicationController (app/controllers/starburst/application_controller.rb)"

I just added it to my own ApplicationController as seems to be the case in the dummy app.

The announcement won't show in the view

Can you help me figure out why this error is showing please?

SQL (1.0ms) INSERT INTO "starburst_announcements" ("body", "created_at", "updated_at") VALUES (?, ?, ?) ["body", "Welcome to SnapLife"], ["created_at", "2015-04-05 23:38:24.839451"], ["updated_at", "2015-04-05 23:38:24.839451"] commit transaction

Code in View
<%= current_biz_user.welcome_to_snaplife %>

Output

Starburst::Announcement:0x77a0fd8

Method in BizUser Model Folder
def welcome_to_snaplife
Starburst::Announcement.create(:body => "Welcome to SnapLife")
end

Starburst.configuration do |config|
config.current_user_method = "current_user"
config.user_instance_methods = ["welcome_to_snaplife"]
end

I have two user models btw. I don't know if that's causing the problem while object is presenting in the view. How can I add both normal "current_user" and "current_biz_user" in the config file without breaking your solution?

Contributing to gem/updates/roadmap?

@erichmachado

Hello - we use this gem in our application and I'd like to help maintain it. I pushed up a PR for supporting rails 7, but it looks like the travis.ci implementation is broken.

I guess you'd want to move this to github actions or something similar? I could attempt to do that as well.

What other things are you aiming to fix/update before releasing the next version?

Installer for ActiveAdmin

Hi Corey, I saw that on the roadmap there is an item "Administrative interface for adding and editing announcements" but what I really needed right now was to allow ActiveAdmin to manage the announcements.

What do you think of a installer to config starburst's announcements to be manageable by ActiveAdmin? Would you accept the pull request if I implement it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.