ActsAsWorkingDays¶ ↑
acts_as_working_days is a Rails plugin to add working days to your models
Usage¶ ↑
Adding acts_as_working_days¶ ↑
# app/models/company.rb class Company < ActiveRecord::Base acts_as_working_days end
Verifying a working day¶ ↑
kwik_e_mart = Company.new(:name => 'Kwik-E-Mart') # Verifies if kwik_e_mart is opened now (Time.now) kwik_e_mart.working_day? # Verifies if kwik_e_mart is opened on Sundays kwik_e_mart.working_day?(:week_day => 0) # Verifies if kwik_e_mart is opened on Mondays 21h30 kwik_e_mart.working_day?(:week_day => 1, :hour => 21, :min => 30)
week_day is an integer representing the day of the week, 0..6, with Sunday == 0. (See www.ruby-doc.org/core-2.1.1/Time.html#method-i-wday)
Setting working days¶ ↑
kwik_e_mart = Company.new(:name => 'Kwik-E-Mart') # Set kwik_e_mart is opened on Mondays from 8h30 to 18h45 kwik_e_mart.working_days.build(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45) # Set kwik_e_mart is opened on Saturdays from 10h00 to 16h00 kwik_e_mart.working_days.build(:week_day => 6, :start_hour => 10, :start_min => 0, :end_hour => 16, :end_min => 0) # retuns true kwik_e_mart.working_day?(:week_day => 1) # returns true kwik_e_mart.working_day?(:week_day => 1, :hour => 18) # return true kwik_e_mart.working_day?(:week_day => 1, :hour => 12, :min => 30) # return false kwik_e_mart.working_day?(:week_day => 1, :hour => 21, :min => 30)
Creating html form to set the working days¶ ↑
# app/views/company/new.html.erb <% form_for @company do |f| %> <%= f.error_messages %> <h2>Company fields</h2> <p> <%= f.label :name, 'Company name' %><br/> <%= f.text_field :name %> </p> <h2>Workings days</h2> <table id="table-working-days"> <thead> <tr> <th>Day</th> <th>Open</th> <th>Close</th> </tr> </thead> <tbody> <% f.fields_for :working_days, @address.working_days_defaults do | working_day_form | %> <tr> <!-- I18n week_day.day_0...day_6, Sunday..Saturday --> <td><%= t "week_day.day_#{working_day_form.object.week_day}" %></td> <td> <%= working_day_form.hidden_field :week_day %> <%= working_day_form.select :start_hour, 0..23, {}, {:class => 'select'} %> <%= working_day_form.select :start_min, 0..59, {}, {:class => 'select'} %> </td> <td> <%= working_day_form.select :end_hour, 0..23, {}, {:class => 'select'} %> <%= working_day_form.select :end_min, 0..59, {}, {:class => 'select'} %> </td> </tr> <% end %> </tbody> <table> <% end %>
Creating I18n file to translate numeric week day to string¶ ↑
#config/locales/en.yml week_day: day_0: Sunday day_1: Monday day_2: Tuesday day_3: Wednesday day_4: Thursday day_5: Friday day_6: Saturday
Setting rails application time zone¶ ↑
Don’t forget to set the time zone in your Rails application enviroment
# config/enviroment.rb Rails::Initializer.run do |config| #... config.time_zone = 'Brasilia' #... end
To list all supported time zones
$ rake time:zones:all
(See wiki.rubyonrails.org/howtos/time-zones)
Installation¶ ↑
Rails 2.3.x¶ ↑
Plugin¶ ↑
script/plugin install git://github.com/phstc/acts_as_working_days
Gem ¶ ↑
# config/environment.rb: config.gem 'acts_as_working_days'
Post Installation¶ ↑
-
script/generate acts_as_working_days_migration
-
rake db:migrate
Rails 3.0¶ ↑
Plugin¶ ↑
script/plugin install git://github.com/phstc/acts_as_working_days
Post Installation¶ ↑
-
rails generate acts_as_working_days:migration
-
rake db:migrate
Testing¶ ↑
rake test
Copyright © 2010 Pablo Cantero, released under the MIT license