Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Pull check-in admin changes from checkin-kiosk branch.
Browse files Browse the repository at this point in the history
I should have separated these out to begin with.
  • Loading branch information
seven1m committed Jul 4, 2014
1 parent a482e0c commit f4af12a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 34 deletions.
9 changes: 4 additions & 5 deletions app/controllers/administration/checkin/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ def destroy
end

def reorder
params["group"].to_a.each_with_index do |id, index|
t = GroupTime.find_by_group_id_and_checkin_time_id(id, params[:time_id])
t.update_attribute(:ordering, index+1)
end
render nothing: true
@group_time = GroupTime.find(params[:id])
@time = @group_time.checkin_time
@time.reorder_group(@group_time, params[:direction])
redirect_to administration_checkin_time_groups_path(@time)
end

private
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/administration/checkin/times_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def create
def edit
redirect_to administration_checkin_time_groups_path(params[:id])
end
alias_method :show, :edit

def update
@time = CheckinTime.find(params[:id])
if @time.update_attributes(time_params)
flash[:notice] = t('changes_saved')
else
add_errors_to_flash(@time)
end
redirect_to administration_checkin_time_groups_path(params[:id])
end

def destroy
@time = CheckinTime.find(params[:id])
Expand Down
41 changes: 32 additions & 9 deletions app/models/checkin_time.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class CheckinTime < ActiveRecord::Base
has_many :group_times, dependent: :destroy
has_many :groups, through: :group_times, order: 'group_times.ordering'
has_many :group_times, -> { order('group_times.ordering, group_times.id') }, dependent: :destroy
has_many :groups, through: :group_times

validates :campus, presence: true
validates :campus, presence: true, exclusion: ['!']

scope :recurring, -> { where(the_datetime: nil) }
scope :upcoming_singles, -> { where('the_datetime is not null and the_datetime >= now()') }
Expand All @@ -13,21 +13,19 @@ class CheckinTime < ActiveRecord::Base
}
scope :today, -> campus { for_date(Time.now, campus) }

self.skip_time_zone_conversion_for_attributes = [:the_datetime]

scope_by_site_id

def validate
validate do
if weekday
if time.nil?
errors.add_to_base('The time is not formatted correctly. Try something like "6:00 p.m."')
errors.add(:base, 'The time is not formatted correctly. Try something like "6:00 p.m."')
end
if the_datetime
errors.add_to_base('You cannot specify a specific date and time and a recurring time together.')
errors.add(:base, 'You cannot specify a specific date and time and a recurring time together.')
end
end
if not weekday and not the_datetime
errors.add_to_base('You must specify either a recurring date and time or a specific date and time.')
errors.add(:base, 'You must specify either a recurring date and time or a specific date and time.')
end
end

Expand Down Expand Up @@ -60,4 +58,29 @@ def time_to_s
Time.parse(time.to_s.sub(/(\d+)(\d{2})$/, '\1:\2')).to_s(:time)
end
end

def to_time
the_datetime || Time.parse(time_to_s)
end

def reorder_group(group, direction)
case direction
when 'up'
group.decrement!(:ordering) unless (group.ordering || 0) <= 1
when 'down'
group.increment!(:ordering) unless (group.ordering || 0) >= group_times.count
end
index = 1
group_times.where.not(id: group.id).each do |p|
index += 1 if index == group.ordering
p.ordering = index
p.save(validate: false)
index += 1
end
end


def self.campuses
distinct(:campus).pluck(:campus).sort
end
end
15 changes: 14 additions & 1 deletion app/models/group_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ class GroupTime < ActiveRecord::Base
belongs_to :group
belongs_to :checkin_time

validates_exclusion_of :section, in: ['!']
validates_exclusion_of :section, in: ['', '!']

scope_by_site_id

def section=(s)
self[:section] = nil unless s.present?
end

before_create :update_ordering
def update_ordering
if checkin_time and ordering.nil?
scope = checkin_time.group_times
scope = scope.where.not(id: id) unless new_record?
self.ordering = scope.maximum(:ordering).to_i + 1
end
end

def self.section_names
connection.select_values("select distinct section from group_times where section is not null and section != '' and site_id=#{Site.current.id} order by section")
end
Expand Down
28 changes: 28 additions & 0 deletions app/views/administration/checkin/groups/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
%td
= select_tag :section, options_for_select(checkin_group_sections, group_time.section), include_blank: true, onchange: "if(this.value != '!') $.ajax('#{administration_checkin_time_group_path(@time, group_time.group_id)}', {data: {section: this.value}, method: 'put'})", class: 'form-control can-create'
%td.actions
- unless group_time.ordering == 1
= link_to reorder_administration_checkin_group_path(group_time, direction: 'up'), data: { method: 'put' }, class: 'btn bg-gray text-blue' do
= icon 'fa fa-arrow-circle-up'
- unless group_time.ordering == @group_times.length
= link_to reorder_administration_checkin_group_path(group_time, direction: 'down'), data: { method: 'put' }, class: 'btn bg-gray text-blue' do
= icon 'fa fa-arrow-circle-down'
= link_to administration_checkin_time_group_path(@time, group_time.group_id), data: { method: 'delete', confirm: t('are_you_sure') }, class: 'btn btn-delete' do
= icon 'fa fa-trash-o'

Expand All @@ -44,3 +50,25 @@
#group_results
.form-group.push-down
= button_tag t('checkin.groups.index.add_group.button'), class: 'btn btn-success'

.box.box-warning
.box-header
%h3.box-title= t('checkin.times.edit.heading')
.box-body
= form_for @time, url: administration_checkin_time_path(@time) do |form|
- if @time.weekday
.form-group
= form.select :weekday, Date::DAYNAMES.with_indexes, {}, class: 'form-control'
.form-group
= form.text_field :time, value: @time.time_to_s, size: 8, class: 'form-control', placeholder: t('checkin.times.recurring.time')
- else
.form-group
= form.text_field :the_datetime, value: @time.to_s, size: 15, class: 'form-control', placeholder: t('checkin.times.single.datetime')
.form-group
= form.select :campus, options_for_select([[t('checkin.times.edit.campus.new'), '!']] + CheckinTime.campuses, @time.campus), {}, class: 'form-control can-create'
.form-group
= form.button t('checkin.times.edit.save.button'), class: 'btn btn-success'
.pull-right
= link_to administration_checkin_time_path(@time), data: { method: 'delete', confirm: t('are_you_sure') }, class: 'btn btn-delete' do
= icon 'fa fa-trash-o'

2 changes: 1 addition & 1 deletion app/views/administration/checkin/times/_times.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
%td= time.campus
%td= t('checkin.times.table.groups.count', count: time.groups.count)
%td.actions
= link_to administration_checkin_time_path(time), data: { method: 'delete', confirm: 'Are you sure?' }, class: 'btn btn-delete btn-xs' do
= link_to administration_checkin_time_path(time), data: { method: 'delete', confirm: t('are_you_sure') }, class: 'btn btn-delete btn-xs' do
= icon 'fa fa-trash-o'
28 changes: 14 additions & 14 deletions app/views/administration/checkin/times/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

%h3
= t('checkin.times.recurring.heading')
= link_to_function "$(this.parentNode).hide();$('#new_recurring_time').show()", class: 'btn btn-success btn-xs' do
= link_to '#', class: 'btn btn-success btn-xs', data: { toggle: '#new_recurring_time' } do
= icon 'fa fa-plus'
= t('checkin.times.recurring.new.button')

- if @recurring_times.any?
= render partial: 'times', object: @recurring_times
- else
%p= t('none')

= form_for :checkin_time, url: administration_checkin_times_path, html: { class: 'form-inline', style: 'display:none;', id: 'new_recurring_time' }, method: 'post' do |form|
.form-group
= form.select :weekday, Date::DAYNAMES.with_indexes, {}, class: 'form-control'
= form.text_field :time, size: 8, class: 'form-control', placeholder: t('checkin.times.recurring.time')
= form.text_field :campus, size: 15, class: 'form-control', placeholder: t('checkin.times.recurring.campus')
= form.select :campus, options_for_select([[t('checkin.times.edit.campus.new'), '!']] + CheckinTime.campuses), { include_blank: true }, class: 'form-control can-create'
= form.button t('checkin.times.recurring.add.button'), class: 'btn btn-success'

- if @recurring_times.any?
= render partial: 'times', object: @recurring_times
- else
%p= t('none')

%h3
= t('checkin.times.single.heading')
= link_to_function "$(this.parentNode).hide();$('#new_single_time').show()", class: 'btn btn-success btn-xs' do
= link_to '#', class: 'btn btn-success btn-xs', data: { toggle: '#new_single_time' } do
= icon 'fa fa-plus'
= t('checkin.times.single.new.button')

- if @single_times.any?
= render partial: 'times', object: @single_times
- else
%p= t('none')

= form_for :checkin_time, url: administration_checkin_times_path, html: { class: 'form-inline', style: 'display:none;', id: 'new_single_time' }, method: 'post' do |form|
%p
= form.text_field :the_datetime, size: 15, class: 'form-control', placeholder: t('checkin.times.single.datetime')
= form.text_field :campus, size: 15, class: 'form-control', placeholder: t('checkin.times.single.campus')
= form.select :campus, options_for_select([[t('checkin.times.edit.campus.new'), '!']] + CheckinTime.campuses), { include_blank: true }, class: 'form-control can-create'
= form.button t('checkin.times.single.add.button'), class: 'btn btn-success'

- if @single_times.any?
= render partial: 'times', object: @single_times
- else
%p= t('none')
5 changes: 5 additions & 0 deletions config/locales/en/checkin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ en:
other: "%{count} destinations"
edit:
button: "edit"
heading: "Edit Check-in Time"
campus:
new: "[new campus]"
save:
button: "Save Changes"
recurring:
heading: "Recurring Times"
new:
Expand Down
6 changes: 2 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@
root to: 'dashboards#show'
resource :dashboard
resources :groups do
collection do
put :batch
post :reorder
end
put :batch, on: :collection
put :reorder, on: :member
end
resources :times do
resources :groups
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20140630233013_update_ordering_on_checkin_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class UpdateOrderingOnCheckinGroups < ActiveRecord::Migration
def change
Site.each do
CheckinTime.all.each do |time|
time.group_times.all.each_with_index do |group, index|
group.ordering = index + 1
group.save(validate: false)
end
end
end
end
end

0 comments on commit f4af12a

Please sign in to comment.