Skip to content

Latest commit

 

History

History
87 lines (66 loc) · 2.5 KB

README.md

File metadata and controls

87 lines (66 loc) · 2.5 KB

activeadmin index as calendar

Gem Version

ActiveAdmin plugin that adds calendar index to ActiveAdmin resources. It uses fullCalendar JQuery plugin to render resource collection to a calendar view.

Installation:

Add this line to your application's Gemfile:

gem 'activeadmin-index_as_calendar', github: 'bys-control/activeadmin-index_as_calendar'

In app/assets/javascripts/active_admin.js add

//= require index_as_calendar/application

In app/assets/stylesheets/active_admin.css.scss add

@import "index_as_calendar";

Finally, run bundle install

Usage

Basic usage

To render default calendar for your AA resource do

ActiveAdmin.register Invoices do

  index_as_calendar
  
end

This will render a default calendar view with default event options:

events = collection.map do |item|
  {
    id: item.id,
    title: item.to_s,
    start: item.created_at.blank
  }
end

Customization

ActiveAdmin.register Invoices do

  index_as_calendar ({:ajax => true, :includes => [:partner]}) do |item|
    {
      id: item.id,
      title: item.partner.name,
      start: item.issue_date,
      url: "#{admin_invoice_path(item)}",
      tooltip: {
       title: "Due date #{item.due_date.to_s}",
       text: item.balance.blank? ? nil : item.balance.to_s
      },
      color: item.balance>0 ? (item.due_date.blank? ? 'green' : (item.due_date <= DateTime.now.beginning_of_day ? 'red' : 'olive' )) : 'green',
      textColor: 'white'
    }
  end
end

The helper function index_as_calendar receives a hash with configuration parameters and a code block that should return a hash with the mappings of model fields to event fields. Rendered events can be configured as specified in fullCalendar documentation.

Configuration parameters defaults to:

default_options = {
  :ajax => true,   # Use AJAX to fetch events. Set to false to send data during render.
  :model => nil,   # Model to be used to fetch events. Defaults to ActiveAdmin resource model.
  :includes => [], # Eager loading of related models
  :start_date => :created_at, # Field to be used as start date for events
  :end_date => nil, # Field to be used as end date for events
  :block => block # Block with the model<->event field mappings
}