Skip to content

Commit

Permalink
chore: conform to linter
Browse files Browse the repository at this point in the history
  • Loading branch information
SilasPeters committed May 25, 2024
1 parent 8d42511 commit 9f1a972
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
35 changes: 17 additions & 18 deletions app/controllers/api/calendars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
require 'icalendar_helper'

# Controller for all calendar related endpoints
class Api::CalendarsController < ActionController::Base
# supply the personalised iCal feed from the user
def show
@member = Member.find_by(calendar_id: params[:calendar_id])
# member variable will be accessible in other methods as well now
# member variable will be accessible in other methods as well now

if not @member # No member with the specified hash was found
render json: { error: "Unkown hash" }, status: :not_found
unless @member # No member with the specified hash was found
render(json: { error: "Unkown hash" }, status: :not_found)
return
end

respond_to do |format|
format.ics {
send_data create_personal_calendar,
type: 'text/calendar',
disposition: 'attachment',
filename: "#{@member.first_name}_activities.ics"
}
format.ics do
send_data(create_personal_calendar,
type: 'text/calendar',
disposition: 'attachment',
filename: "#{ @member.first_name }_activities.ics")
end
end
end

def index
if current_user.nil?
render json: { error: "Not logged in" }, status: :forbidden
render(json: { error: "Not logged in" }, status: :forbidden)
return
end # TODO heb je overal dit soort error handling?
end

@member = Member.find(current_user.credentials_id)
render plain: "https://koala.svsticky.nl/api/calendar/pull/#{@member.calendar_id}" # TODO can this less hard-coded?
render(plain: url_for(action: 'show', calendar_id: @member.calendar_id), format: :ics)
end

# Not exposed to API directly, but through #show
Expand All @@ -39,13 +40,11 @@ def create_personal_calendar
# is enrolled as reservist
@reservist_activity_ids = @member.reservist_activities.ids
events = @member.activities.map do |a|
if @reservist_activity_ids.include? a.id
a.name = "[RESERVIST] #{a.name}"
end
IcalendarHelper.activityToEvent(a, @locale)
a.name = "[RESERVIST] #{ a.name }" if @reservist_activity_ids.include?(a.id)
IcalendarHelper.activity_to_event(a, @locale)
end

# Return the calendar
IcalendarHelper.createCalendar(events, @locale).to_ical
IcalendarHelper.create_calendar(events, @locale).to_ical
end
end
2 changes: 1 addition & 1 deletion app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def groups
# We cannot enforce that a member is created with a calendar_id, but we can enforce that
# when a member is created or saved, a calendar_id is set before validation.
before_validation on: [:save, :create] do
self.calendar_id = SecureRandom.uuid if self.calendar_id.blank?
self.calendar_id = SecureRandom.uuid if calendar_id.blank?
end

# Rails also has hooks you can hook on to the process of saving, updating or deleting. Here the join_date is automatically filled in on creating a new member
Expand Down
18 changes: 8 additions & 10 deletions lib/icalendar_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'icalendar' # https://github.com/icalendar/icalendar

#:nodoc:
# Includes all abstractions over iCalender representations
module IcalendarHelper
# Converts a sticky activity to an iCalendar event
def IcalendarHelper.activityToEvent(activity, locale)
def self.activity_to_event(activity, locale)
event = Icalendar::Event.new
event.uid = activity.id.to_s
event.dtstart = activity.start_date
Expand All @@ -15,9 +15,10 @@ def IcalendarHelper.activityToEvent(activity, locale)
end

# Combines zero or more Icalendar events into an iCalendar abstract object
def IcalendarHelper.createCalendar(events, locale)
def self.create_calendar(events, locale)
calendar = Icalendar::Calendar.new
calendar.x_wr_calname = case locale
calendar.x_wr_calname =
case locale
when :nl
"Sticky Activiteiten"
else
Expand All @@ -28,15 +29,12 @@ def IcalendarHelper.createCalendar(events, locale)
return calendar
# Returns the abstract icalendar object, not the ICS string ready to
# be stored in an ICS file. To convert this calendar into an ICS string,
# use `calendar.to_ical` or the `createFile` method below.
# use `calendar.to_ical` or the `create_file` method below.
end

# Stores the calendar to an *.ics file
def IcalendarHelper.createFile(calendar, path)
def self.create_file(calendar, path)
calendar_string = calendar.to_ical
File.open(path, 'w') do |file|
file.write(calendar_string)
end
File.write(path, calendar_string)
end
end

0 comments on commit 9f1a972

Please sign in to comment.