Module: IndexAsCalendar::DSL
- Defined in:
- lib/index_as_calendar/dsl.rb
Instance Method Summary collapse
-
#index_as_calendar(options = {}, &block) ⇒ Object
Initializes activeadmin index as calendar.
Instance Method Details
#index_as_calendar(options = {}, &block) ⇒ Object
Initializes activeadmin index as calendar
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/index_as_calendar/dsl.rb', line 7 def index_as_calendar( ={}, &block ) = { :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 :fullCalendarOptions => nil, # fullCalendar options to be sent upon initialization :default => false # Set this index view as default } = .deep_merge() # Defines controller for event_mapping model items to events controller do def event_mapping( items, ) events = items.map do |item| if ![:block].blank? instance_exec(item, &[:block]) else { :id => item.id, :title => item.to_s, :start => ([:start_date].blank? or item.send([:start_date]).blank?) ? Date.today.to_s : item.send([:start_date]), :end => ([:end_date].blank? or item.send([:end_date]).blank?) ? nil : item.send([:end_date]) } end end end end # Setup AJAX if [:ajax] # Setup fullCalendar to use AJAX calls to retrieve event data index as: :calendar, default: [:default] do |context| context[:fullCalendarOptions] = [:fullCalendarOptions] events = { url: "#{collection_path()}/index_as_events.json", type: 'GET', data: params } end # Defines collection_action to get events data collection_action :index_as_events, :method => :get do items = [:model] || end_of_association_chain items = items.send(params[:scope]) if params[:scope].present? items = items.includes([:includes]) unless [:includes].blank? items = items.where([:start_date] => params[:start].to_date...params[:end].to_date).ransack(params[:q]).result events = event_mapping(items, ) respond_to do |format| format.json { render :json => events } end end # Return events to be used during partial render else index as: :calendar, default: [:default] do |context| context[:fullCalendarOptions] = [:fullCalendarOptions] events = self.controller.event_mapping(context[:collection], ) end end end |