Rails remove time from datetime attribute and group - ruby-on-rails

I have a column DATENEW in my invoices table.
If in my view I use:
<td><%= invoice.DATENEW %></td>
it shows:
2015-02-16 11:38:03 UTC
I need to display only year month and day.
How can I do it?

You can use strftime to show only year, month and date.
<%= invoice.DATENEW.strftime('%Y-%m-%d') %>
And for grouping, first show us what you've tried.

These Time / DateTime methods also may come in handy:
at_beginning_of_month
at_beginning_of_day
at_beginning_of_year
at_beginning_of_week
at_beginning_of_hour
at_beginning_of_minute

Related

Multiple days of week in one attribute in database

I want to implement a feature in Rails using the option of multiple checkbox. User must be able to choose the days in which he want the emails, like Monday, Tuesday, etc. This must be a list of checkboxes with day name as label and day index like 0 for Sunday, 1 for Monday, like that.
Also these fields must be checked when the user next time come here to edit.
I don't want to create separate db field for each day. This can be an array of day index stored in one database field. I am using Rails version 4.
Date::DAYNAMES will give the list of week days. Date::DAYNAMES.each_with_index will give the list of days with its index. I want to know the best way to implement the same.
Try this,
<%=select_tag 'days[]', options_for_select(Date::DAYNAMES.zip((0..6).to_a),
[selected days array goes here]
), :multiple => true%>
For using checkbox
<ul>
<% Date::DAYNAMES.zip((0..6).to_a).each do |day| %>
<li>
<%= check_box_tag 'days[]', day[1], [selected days array].include?(day[1]) -%>
<%= h day[0] -%>
</li>
<% end %>
</ul>
Hope it's help you.

Organizing by rails datetime in view

I am creating a scheduling app in Rails and I am getting stuck while trying to organize the rooms by date. When I say a room, I essentially just mean a block in the schedule. A typical event might have a lunch (one room), then a networking section (another room). Here is how I am getting the rooms:
#rooms = Room.where(event_id: #current_event.id).order(:start_time)
So that returns the rooms that belong to an event, in order of the start time. In my view I loop through and display the rooms for the event, but I want to group them in the view by the date in case there is an event that is on multiple days.
Also :start_time is a datetime type in Ruby. In human speak, what I would do is look at the date portion and if the room date is not the same as the current group, I would print the new date and continue to group the rooms that fall on that day. Here is a trivial example in case I am not being clear:
Event: Staff Retreat
July 14th, 2015
-----------------------
12:30 PM
Team building Lunch Begins
------------------------
6:30 PM
Team building Dinner Begins
------------------------
July 15th, 2015
------------------------
9:30 AM
Team building Breakfast Begins
So having the grouping of rooms in the #rooms variable that is a datetime, what would be the best way to display a table like the above? I would think that in my loop I should check if the date was the same as the previous room, and if not print the date
- #rooms.each do |room|
room.start_time.strftime("%B %d, %Y")
room.start_time.strftime("%I:%M%p")
room.name
I am having trouble with the logistics because with the Model-View-Controller concept, I feel that sorting in the view may have the view do something it shouldn't have to. I am also struggling with how to do that in the view. I would think setting a variable in the controller that would hold the temporary date as I loop through would work, but it seems like that would start to get pretty messy though. What is the best way to group the various dates from the #rooms variable?
Can you try this. this will return you a hash where date is the key and value will contain all the #rooms related to that time.
#rooms = Room.where(event_id: #current_event.id)
#rooms = Hash[#rooms.sort_by{|o| o.start_time.to_date}.group_by {|room| room.start_time.to_date}.map{|l,m| [l, m.sort_by{|k| k.start_time}]}]
now you can traverse the rooms like this in the views. im putting code in erb format.
<% #rooms.each do |k, v| %>
<%= k %>
<% v.each do |room| %>
<%= room.created_at.start_time('%I:%M %p')%>
<%= #room.name or title what so ever. %>
<% end %>
<% end %>

How do I remove days from date range?

I have a calendaring application I am working on in Rails 3. I am gathering a 'to' and 'from' date from the user and creating a date, and creating events based on that range. Here is the code:
date_range = (params[:from_date]..params[:to_date]).to_a
date_range.each do |r|
params[:orientation][:class_date] = r
#orientation = Orientation.new(params[:orientation])
#orientation.save
end
This works like a charm. Now I want to add check boxes in the view for Monday - Friday so the user can specify which days the want a repeating event to be created for. My thinking is to expand on the logic above and create a date range with the 'to' and 'from' dates, then remove dates based on check boxes (days) the user checks. If the user checks 'Mon', 'Wed', 'Fri', I would create my date range, then remove all Thursday and Friday dates from the range. Can anyone suggest any methods in Ruby that might be handy for dealing with this?
You could add checkboxes in your form to ignore the selected date and then ignore each day in the loop:
# view
<% Date::DAYNAMES.each_with_index do |day_name, wday| %>
<%= check_box_tag "ignored_days[]", wday %>
<%= day_name %>
<% end %>
# controller
date_range.each do |r|
next if params[:ignored_days].include? r.wday.to_s #added edit here
params[:orientation][:class_date] = r
#orientation = Orientation.new(params[:orientation])
#orientation.save
end
Hope this helps!

Rails Date Select Parameters

I'm using a date select in a rails 3 form.
<%=f.date_select :date %>
I would like to restrict the dates so that you can only pick dates that fall on a Sunday. Is there any way of going about doing this?
I'm also trying to stop dates which have already passed from appearing.
Thanks for any help in advance!
Rails date_select field generates three dropdown to select the parts of the date. There is no chanche, that you modify for example the month, and the day will still be sunday.
You must write some js magic to enforce such a role, or find an already existing datepicker and limit it. Or alternatively, you let the user to select a week, and calculate the exact date of sunday from that.
Ok having studied this out a bit further I don't think this is possible due to the format of the date_select field. The closest I can get is
<%=f.date_select :date, start_year: Time.now.year %>
so that at least you can't select dates from previous years. I've implemented the restriction on days and months that have past by setting up the view to automatically delete records that aren't relevant:
<% if(service.date < Date.today) %>
<% service.destroy %>
<% end %>
Not perfect but does the job in my case.

Changing the timestamp of magic columns (Rails)

I want to change the default timestamp of the magic columns of rails (created_at, modified_at) instead of 2010-09-03 14:52:46 UTC I'd like to change it to September 10, 2010 at 02:52 PM. or at least parse that way.
Now, i know that i can do this right in the view, by manipulating the variable, but isnt there a more "railish way" where i can just do this in the model, and then call it as a method?
class YourModel < ActiveRecord::Base
def my_formated_date_time
created_at.strftime("%B %d, %Y at %I:%M %p")
end
end
Or you can use the localization format. Add this to your config/locale/your_locale.yml. IMHO: This would be the most railish way.
your_locale:
time:
formats:
default: "%B %d, %Y at %I:%M %p"
Then in your view do this
<%=l #your_model.created_at %>
<!-- short for: -->
<%= I18n.localize #your_model.created_at %>
Modifying the created_at date is fine, it will stay as you set it, and you can over-ride it even at the start, but updated_at or modified_at might prove tricky. Every time you save your model this is changed. If you don't want this behavior, convention suggests switching to a different name.
If you want to adjust how dates are displayed, you need to configure the default formatter.

Resources