Skip to content

Commit

Permalink
94
Browse files Browse the repository at this point in the history
  • Loading branch information
kobaltz committed Aug 21, 2017
1 parent d3f15b0 commit 4f248b4
Show file tree
Hide file tree
Showing 32 changed files with 311 additions and 109 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -29,3 +29,5 @@ source 'https://rails-assets.org' do
gem 'rails-assets-fullcalendar'
gem 'rails-assets-momentjs'
end

gem 'ice_cube'
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -70,6 +70,7 @@ GEM
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.6)
ice_cube (0.16.2)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
Expand Down Expand Up @@ -187,6 +188,7 @@ DEPENDENCIES
byebug
coffee-rails (~> 4.1.0)
faker
ice_cube
jbuilder (~> 2.0)
jquery-rails
listen (~> 3.0.5)
Expand Down
3 changes: 0 additions & 3 deletions app/assets/javascripts/events.coffee

This file was deleted.

6 changes: 4 additions & 2 deletions app/assets/javascripts/full_calendar.js
Expand Up @@ -12,8 +12,10 @@ initialize_calendar = function() {
selectHelper: true,
editable: true,
eventLimit: true,
events: '/events.json',

eventSources: [
'/events.json',
'/recurring_events.json'
],
select: function(start, end) {
$.getScript('/events/new', function() {
$('#event_date_range').val(moment(start).format("MM/DD/YYYY HH:mm") + ' - ' + moment(end).format("MM/DD/YYYY HH:mm"))
Expand Down
3 changes: 0 additions & 3 deletions app/assets/javascripts/visitors.coffee

This file was deleted.

3 changes: 0 additions & 3 deletions app/assets/stylesheets/events.scss

This file was deleted.

89 changes: 0 additions & 89 deletions app/assets/stylesheets/scaffolds.scss

This file was deleted.

3 changes: 0 additions & 3 deletions app/assets/stylesheets/visitors.scss

This file was deleted.

45 changes: 45 additions & 0 deletions app/controllers/recurring_events_controller.rb
@@ -0,0 +1,45 @@
class RecurringEventsController < ApplicationController
before_action :set_recurring_event, only: [:show, :edit, :update, :destroy]

def index
@recurring_events = RecurringEvent.all
end

def show
end

def new
@recurring_event = RecurringEvent.new
end

def edit
end

def create
@recurring_event = RecurringEvent.new(recurring_event_params)
@recurring_event.save
end

def update
if params[:event]
@recurring_event.update(anchor: params[:event][:start])
else
@recurring_event.update(recurring_event_params)
end
end

def destroy
@recurring_event.destroy
end

private

def set_recurring_event
@recurring_event = RecurringEvent.find(params[:id])
end

def recurring_event_params
params.require(:recurring_event).permit(:title, :anchor, :frequency, :color)
end
end

2 changes: 2 additions & 0 deletions app/helpers/recurring_events_helper.rb
@@ -0,0 +1,2 @@
module RecurringEventsHelper
end
43 changes: 43 additions & 0 deletions app/models/recurring_event.rb
@@ -0,0 +1,43 @@
# == Schema Information
#
# Table name: recurring_events
#
# id :integer not null, primary key
# title :string
# anchor :date
# frequency :integer default(0)
# color :string
# created_at :datetime not null
# updated_at :datetime not null
#

class RecurringEvent < ApplicationRecord
enum frequency: { weekly: 0, biweekly: 1, monthly: 2, annually: 3 }

validates :anchor, presence: true
validates :frequency, presence: true

def schedule
@schedule ||= begin
schedule = IceCube::Schedule.new(now = anchor)
case frequency
when 'weekly'
schedule.add_recurrence_rule IceCube::Rule.weekly(1)
when 'biweekly'
schedule.add_recurrence_rule IceCube::Rule.weekly(2)
when 'monthly'
schedule.add_recurrence_rule IceCube::Rule.monthly(1)
when 'annually'
schedule.add_recurrence_rule IceCube::Rule.yearly(1)
end
schedule
end
end

def events(start_date, end_date)
start_frequency = start_date ? start_date.to_date : Date.today - 1.year
end_frequency = end_date ? end_date.to_date : Date.today + 1.year
schedule.occurrences_between(start_frequency, end_frequency)
end

end
2 changes: 1 addition & 1 deletion app/views/events/_event.json.jbuilder
@@ -1,6 +1,6 @@
date_format = event.all_day_event? ? '%Y-%m-%d' : '%Y-%m-%dT%H:%M:%S'

json.id event.id
json.id "event_#{event.id}"
json.title event.title
json.start event.start.strftime(date_format)
json.end event.end.strftime(date_format)
Expand Down
4 changes: 4 additions & 0 deletions app/views/events/_form.html.erb
Expand Up @@ -9,6 +9,10 @@

<div class="form-actions">
<%= f.button :submit %>
<%= link_to 'New Recurring Event',
new_recurring_event_path,
class: 'btn btn-default',
remote: true if @event.new_record? %>
<%= link_to 'Delete',
event,
method: :delete,
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/destroy.js.erb
@@ -1,2 +1,2 @@
$('.calendar').fullCalendar('removeEvents', [<%= @event.id %>])
$('.calendar').fullCalendar('removeEvents', '<%= "event_#{@event.id}" %>')
$('.modal').modal('hide');
1 change: 1 addition & 0 deletions app/views/events/new.js.erb
@@ -1,3 +1,4 @@
$('.modal-backdrop').hide();
$('#remote_container').html('<%= j render "new" %>');
$('#new_event').modal('show');

2 changes: 1 addition & 1 deletion app/views/events/update.js.erb
@@ -1,4 +1,4 @@
$('.calendar').fullCalendar('removeEvents', [<%= @event.id %>]);
$('.calendar').fullCalendar('removeEvent', '<%= "event_#{@event.id}" %>');
$('.calendar').fullCalendar(
'renderEvent',
$.parseJSON("<%=j render(@event, format: :json).html_safe %>"),
Expand Down
13 changes: 13 additions & 0 deletions app/views/recurring_events/_edit.html.erb
@@ -0,0 +1,13 @@
<div class="modal fade" id="edit_recurring_event">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Edit Recurring Event</h4>
</div>
<div class="modal-body">
<%= render 'form', recurring_event: @recurring_event %>
</div>
</div>
</div>
</div>
22 changes: 22 additions & 0 deletions app/views/recurring_events/_form.html.erb
@@ -0,0 +1,22 @@
<%= simple_form_for @recurring_event, remote: true do |f| %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :anchor, input_html: { class: "form-control input-sm" } %>
<%= f.input :frequency, as: :select, collection: RecurringEvent.frequencies.keys, input_html: { class: "form-control input-sm" } %>
<%= f.input :color, as: :select, collection: [['Black','black'], ['Green','green'], ['Red','red']] %>
</div>

<div class="form-actions">
<%= f.button :submit %>
<%= link_to 'New Event',
new_event_path,
class: 'btn btn-default',
remote: true if @recurring_event.new_record? %>
<%= link_to 'Delete',
@recurring_event,
method: :delete,
class: 'btn btn-danger',
data: { confirm: 'Are you sure?' },
remote: true unless @recurring_event.new_record? %>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/recurring_events/_new.html.erb
@@ -0,0 +1,13 @@
<div class="modal fade" id="new_recurring_event">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Create New Recurring Event</h4>
</div>
<div class="modal-body">
<%= render 'form', recurring_event: @recurring_event %>
</div>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/recurring_events/_recurring_event.json.jbuilder
@@ -0,0 +1,13 @@
events = recurring_event.events(params[:start], params[:end])
json.array! events do |event|
json.id "recurring_#{recurring_event.id}"
json.title recurring_event.title
json.start event.strftime('%Y-%m-%d')
json.end (event + 1.day).strftime('%Y-%m-%d')

json.color recurring_event.color unless recurring_event.color.blank?
json.allDay true

json.update_url recurring_event_path(recurring_event, method: :patch)
json.edit_url edit_recurring_event_path(recurring_event)
end
6 changes: 6 additions & 0 deletions app/views/recurring_events/create.js.erb
@@ -0,0 +1,6 @@
$('.calendar').fullCalendar(
'renderEvents',
$.parseJSON("<%=j render(@recurring_event, format: :json).html_safe %>"),
true
);
$('.modal').modal('hide');
2 changes: 2 additions & 0 deletions app/views/recurring_events/destroy.js.erb
@@ -0,0 +1,2 @@
$('.calendar').fullCalendar('removeEvents', '<%= "recurring_#{@recurring_event.id}" %>');
$('.modal').modal('hide');
3 changes: 3 additions & 0 deletions app/views/recurring_events/edit.js.erb
@@ -0,0 +1,3 @@
$('#remote_container').html('<%= j render "edit" %>');
$('#edit_recurring_event').modal('show');

3 changes: 3 additions & 0 deletions app/views/recurring_events/index.json.jbuilder
@@ -0,0 +1,3 @@
json.partial! @recurring_events,
partial: 'recurring_events/recurring_event',
as: :recurring_event
4 changes: 4 additions & 0 deletions app/views/recurring_events/new.js.erb
@@ -0,0 +1,4 @@
$('.modal-backdrop').hide();
$('#remote_container').html('<%= j render "new" %>');
$('#new_recurring_event').modal('show');

7 changes: 7 additions & 0 deletions app/views/recurring_events/update.js.erb
@@ -0,0 +1,7 @@
$('.calendar').fullCalendar('removeEvents', '<%= "recurring_#{@recurring_event.id}" %>');
$('.calendar').fullCalendar(
'renderEvents',
$.parseJSON("<%= j render(@recurring_event, format: :json).html_safe %>"),
true
);
$('.modal').modal('hide');
3 changes: 1 addition & 2 deletions config/routes.rb
@@ -1,6 +1,5 @@
Rails.application.routes.draw do
resources :recurring_events
resources :events
root 'visitors#index'

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
14 changes: 14 additions & 0 deletions db/migrate/20170819005825_create_recurring_events.rb
@@ -0,0 +1,14 @@
# rails g model recurring_event title anchor:date frequency:integer color

class CreateRecurringEvents < ActiveRecord::Migration[5.0]
def change
create_table :recurring_events do |t|
t.string :title
t.date :anchor
t.integer :frequency, limit: 1, default: 0
t.string :color

t.timestamps
end
end
end

0 comments on commit 4f248b4

Please sign in to comment.