Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
Replace custom time select with Rails version
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Groeneveld committed Dec 12, 2016
1 parent 95b1b00 commit 7500ef6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
35 changes: 0 additions & 35 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,4 @@ def tabindex
@tabindex += 1
end

def custom_time_select(object, method, attributes={}, options={})
return if object.nil?

attributes[:default] ||= ''
attributes[:name] ||= ''
attributes[:id] ||= ''

options[:minutes] ||= false
options[:military] ||= true

if options[:military]
range = 0..23
else
range = 0..11
end

result = object.try(:send, method)
result = result.hour if result.kind_of?(DateTime) || result.kind_of?(Time)
has_value = range.include?(result)

content_tag(:select, name: attributes[:name], id: attributes[:id]) do
result = "#{result}:00" if options[:minutes]
range.each do |e|
e = "#{e}:00" if options[:minutes]
if has_value && e == result
concat content_tag(:option, e, selected: :selected)
elsif e == options[:default]
concat content_tag(:option, e, selected: :selected)
else
concat content_tag(:option, e)
end
end
end
end

end
20 changes: 18 additions & 2 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,31 @@
<%= f.fields_for :schedule, build_schedule_for_user do |ff| %>
<div class="row">
<div class="small-5 medium-4 large-3 columns">
<%= custom_time_select @user.schedule, :start, { name: 'user[schedule_attributes][start]', id: 'user_schedule_attributes_start' }, minutes: true, default: '8:00' %>
<%
selected = '8:00'
unless ff.object.start.nil?
selected = "#{ff.object.start.hour}:00"
end
%>
<%= ff.select :start,
options_for_select((0..23).map { |n| "#{n}:00" }, selected),
label: false %>
</div>
<div class="small-2 medium-2 large-1 column">
<label class="inline text-center">
<%= t(:to) %>
</label>
</div>
<div class="small-5 medium-4 large-3 columns end">
<%= custom_time_select @user.schedule, :end, { name: 'user[schedule_attributes][end]', id: 'user_schedule_attributes_end' }, minutes: true, default: '17:00' %>
<%
selected = '18:00'
unless ff.object.end.nil?
selected = "#{ff.object.end.hour}:00"
end
%>
<%= ff.select :end,
options_for_select((0..23).map { |n| "#{n}:00" }, selected),
label: false %>
</div>
</div>
<label class="checkbox-helper">
Expand Down

0 comments on commit 7500ef6

Please sign in to comment.