Merge pull request #166 from RickCarlino/master

Refactor in schedules controller
pull/167/head
Rick Carlino 2015-03-05 09:30:38 +07:00
commit 05304dd111
4 changed files with 28 additions and 33 deletions

View File

@ -1,54 +1,46 @@
controller = ($scope, Data, Calendar) ->
nope = (e) -> alert 'Doh!'; console.error e
$scope.clear = -> $scope.form = {} and $scope.drawCalendar()
clear = -> $scope.form = {}; drawCalendar()
$scope.repeats = [{show: 'Minutes', value: 'minutely'},
{show: 'Hours', value: 'hourly'},
{show: 'Days', value: 'daily'},
{show: 'Weeks', value: 'weekly'},
{show: 'Months', value: 'monthly'},
{show: 'Years', value: 'yearly'}]
Data.findAll('sequence', {}).catch(nope)
Data.findAll('schedule', {}).catch(nope)
Data.bindAll('sequence', {}, $scope, 'sequences')
Data.bindAll('schedule', {}, $scope, 'schedules')
$scope.submit = ->
# :(? Why is this? This might be a JSData bug?
class ScheduleFormAdapter
constructor: ({@_id, @repeat, @time_unit,
@start_time, @end_time, @sequence_id}) ->
Data
.create('schedule', (new ScheduleFormAdapter($scope.form)))
.catch(nope)
.then($scope.clear)
$scope.prettyDates = []
drawCalendar = -> $scope.prettyDates = Calendar.draw($scope.schedules)
$scope.$watchCollection 'schedules', drawCalendar
$scope.submit = -> Data.create('schedule', $scope.form).catch(nope).then clear
$scope.destroy = ->
if !!$scope.form._id
Data
.destroy('schedule', $scope.form._id)
.catch(nope)
.then($scope.clear)
Data.destroy('schedule', $scope.form._id).catch(nope).then clear
else
$scope.clear()
clear()
$scope.edit = (sched) -> $scope.form = sched
previousSchedule = (indx) ->
$scope.prettyDates[indx - 1] || {next_time: new Date(0)}
currentSchedule = (indx) -> $scope.prettyDates[indx]
$scope.showDate = (indx) ->
return yes if $scope.prettyDates.length is 1
before = previousSchedule(indx).next_time.getDay()
after = currentSchedule(indx).next_time.getDay()
if before is after then no else yes
$scope.pastEvent = (sched) -> sched.next_time < new Date()
$scope.nextEvent = ($index) ->
pastSchedule = $scope.prettyDates[$index - 1] || {next_time: new Date(0)}
previous = $scope.pastEvent(pastSchedule)
current = $scope.pastEvent($scope.prettyDates[$index])
previous = $scope.pastEvent(previousSchedule($index))
current = $scope.pastEvent(currentSchedule($index))
if previous is true and current is false then yes else no
$scope.prettyDates = []
$scope.drawCalendar = -> $scope.prettyDates = Calendar.draw($scope.schedules)
$scope.$watchCollection 'schedules', $scope.drawCalendar
last = {getDay: -> NaN}
$scope.showDate = (date) ->
# TODO: We don't need that `last` closure anymore, now that we are using
# Arrays instead of Objects. We can do `schedules[$index - 1]`
isSameDay = last.getDay() is date.getDay()
last = date
if isSameDay then no else yes
angular.module('FarmBot').controller "ScheduleController", [
'$scope'
'Data'

View File

@ -11,6 +11,12 @@ module Api
sorry "Can't find #{exc.klass}(s) with ID(s): #{exc.params}", 404
end
rescue_from Mongoid::Errors::InvalidFind do
sorry 'You most likely forgot to provide an `*_id` attribute in your '\
'request parameters. Examples of possible missing params: '\
'schedule_id, sequence_id, id, etc..', 400
end
private
def sorry(msg, status)

View File

@ -37,9 +37,6 @@ module Api
def sequence
@sequence ||= Sequence.find(params[:sequence_id])
rescue Mongoid::Errors::InvalidFind => e
# TODO: Proper error handler.
raise 'You need to add a sequence_id. I need to add a better error handler'
end
def schedule

View File

@ -8,7 +8,7 @@
.content-wrapper.calendar-wrapper
.row.day{'ng-repeat' => 'schedule in prettyDates'}
.small-2.columns
.div{ng_show: 'showDate(schedule.next_time)'}
.div{ng_show: 'showDate($index)'}
%h6.calendar-date {{ schedule.next_time | date: 'd' }}
%h6.calendar-month {{ schedule.next_time | date: 'MMM' }}
.small-10.columns.event{ng_class: '{past: pastEvent(schedule), next: nextEvent($index)}'}