Skip to content

A simple plugin that makes storing duration as total seconds easier

Notifications You must be signed in to change notification settings

jaran/has_duration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

**Not fully tested yet**

A simple plugin that makes storing duration as total seconds easier.  For situations where you need to store durations such as total time spent running the marathon etc.

has_duration :column_name where column_name is the column you want to store the total number of seconds in

Then in your table you need to define the following extra columns: column_name_hours, column_name_minutes, column_name_seconds

Example:

Migration:

create_table :workouts do |t|
  t.column :duration, :integer #stores the total duration in seconds
  t.column :duration_hours, :integer #stores the inputed hours
  t.column :duration_minutes, :integer #stores the inputed minutes
  t.column :duration_seconds, :integer #stores the inputed seconds
end

Model:

class Workout
  has_duration :duration, :allow_nil => false
end

View:

<% form_for(@workout) do |f| %>
  Duration: <%=f.text_field :duration_hours %> : <%= f.text_field :duration_minutes %> : <% f.text_field :duration_seconds %> (HH:MM:SS)
  <br/>
  <%= f.submit %>
<% end %>

Controller:

def create
  @workout = Workout.new(params[:workout])
  if @workout.save # calculates the total seconds based on the parameters and stores it in the duration column
    ...
  end
end

The plugin validates the numericality of each column. For minutes and seconds it also makes sure its less than 59

TODO:
- Pass on more validates_numericality_of options
- Look into possibility of not needing the the extra columns

About

A simple plugin that makes storing duration as total seconds easier

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages