
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
This gem was inspired by the Site-Wide Announcements (revised) episode of RailsCasts. If you don't have a premium account you can see the original episode.
--no-bootstrap option when you run the installerensure_admin_user which ensures only admin users can create/edit/delete
announcemetscurrent_user, which is also a helper_methodcurrent_user responds to #has_role?(<role>)Add it to your Gemfile:
gem "user_announcements"
From the command line:
$ bundle install
See the installer options:
$ rails generate user_announcements:install --help
Run the installer and run migrations:
$ rails generate user_announcements:install
$ rake db:migrate
class ApplicationController < ActionController::Base
  protect_from_forgery
  
  private
  
  def ensure_admin_user
    current_user.has_role?('admin')
  end
  
  def current_user
    @user ||= User.find(session[:user_id])
  end
  helper_method :current_user
  
end
class User < ActiveRecord::Base
  
  def has_role?(role)
    return true if role.blank?
    return true if role == admin && self.admin?
    # ... more elaborate role checking code here?
    false
  end
  
end
http://<your app>/admin/announcements
Add the helper method to your layout:
#../layouts/application.html.erb
<body>
  <%= user_announcements %>
  ...
Now visit some page that uses that layout to see the announcement.
Non-admin users can see current and past announcements, including ones they have hidden, by visiting:
http://<your app>/announcements
The following url helpers are available:
# admin manages announcements
admin_announcements_path
# user manages own announcements
hidden_announcements_path
There are several configuration settings found in ../config/initializers/user_announcements.rb.
# note: all options accept lambdas
UserAnnouncements.config do |config|
  using_bootstrap = true
  
  if using_bootstrap
    config.bootstrap = true
    config.bootstrap_datetime_picker = true
    config.styles = [['Yellow', ''], ['Red', 'alert-error'], ['Green', 'alert-success'], ['Blue', 'alert-info']]
  else
    config.bootstrap = false
    config.bootstrap_datetime_picker = false
    config.styles = [['Yellow', 'yellow'], ['Red', 'red'], ['Green', 'green'], ['Blue', 'blue']]
  end
  # Announcement defaults
  config.default_active = true
  config.default_starts_at = lambda { Time.now.in_time_zone }
  config.default_ends_at = lambda { 1.week.from_now.in_time_zone.end_of_day }
  config.default_style = ''
  # config.default_roles = ['admin']
  # Roles
  # Setting config.roles will show roles on the Announcment detail form and cause
  # roles to be considered in showing announcements to users
  # config.roles = []
  # config.roles = ['', 'admin']
  # config.roles = [ ['Public', ''], ['Administrator', 'admin'] ]
  # config.roles = lambda { MyRoleClass.all.map { |role| [role.name, role.id] } }  
  
end
Don't forget to restart your Rails server after changes to the config file.
When the user_announcements:install command is run app/assets/stylesheets/user_announcements.css
is created.
When you change the CSS pay attention to the selectors -- some are for when you are configured
with config.bootstrap = true; most are for when config.bootstrap = false.
FAQs
Unknown package
We found that user_announcements demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.