acts_as_working_days 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,33 +4,81 @@ acts_as_working_days is a Rails plugin to add working days on models
4
4
 
5
5
  == Usage
6
6
 
7
- class Company < ActiveRecord::Base
8
-
9
- acts_as_working_days
10
-
11
- end
12
-
13
- #Sample test code
14
- def test_working_day
15
-
16
- kwik_e_mart = Company.new(:name => 'Kwik-E-Mart')
17
-
18
- #1 is Monday - Apu Kwik-E-Mart is only opened on Mondays, start 8h30, end 18h45
19
- kwik_e_mart.put_working_day(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
7
+ #app/models/company.rb
8
+ class Company < ActiveRecord::Base
20
9
 
21
- #Save model and working day
22
- kwik_e_mart.save
10
+ acts_as_working_days
23
11
 
24
- #0 is Sunday - 12h45 is closed
25
- assert_equal false, kwik_e_mart.working_day?(:week_day => 0, :hour => 12, :min => 0)
26
-
27
- #1 is Monday - 21h00 is closed
28
- assert_equal false, kwik_e_mart.working_day?(:week_day => 1, :hour => 21)
29
-
30
- #1 is Monday - 12h15 is opened
31
- assert_equal true, kwik_e_mart.working_day?(:week_day => 1, :hour => 12, :min => 15)
32
-
33
- end
12
+ end
13
+
14
+ #Sample test code
15
+ def test_working_day
16
+
17
+ kwik_e_mart = Company.new(:name => 'Kwik-E-Mart')
18
+
19
+ #1 is Monday, Apu Kwik-E-Mart is only opened on Mondays, start 8h30, end 18h45
20
+ kwik_e_mart.working_days.build(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
21
+
22
+ #Save model and working day
23
+ kwik_e_mart.save
24
+
25
+ #0 is Sunday, 12h45 is closed
26
+ assert_equal false, kwik_e_mart.working_day?(:week_day => 0, :hour => 12, :min => 0)
27
+
28
+ #1 is Monday, 21h00 is closed
29
+ assert_equal false, kwik_e_mart.working_day?(:week_day => 1, :hour => 21)
30
+
31
+ #1 is Monday, 12h15 is opened
32
+ assert_equal true, kwik_e_mart.working_day?(:week_day => 1, :hour => 12, :min => 15)
33
+
34
+ end
35
+
36
+ #app/views/company/new.html.erb
37
+ <% form_for @company do |f| %>
38
+ <%= f.error_messages %>
39
+ <h2>Company fields</h2>
40
+ <p>
41
+ <%= f.label :name, 'Company name' %><br/>
42
+ <%= f.text_field :name %>
43
+ </p>
44
+ <h2>Workings days</h2>
45
+ <table id="table-working-days">
46
+ <thead>
47
+ <tr>
48
+ <th>Day</th>
49
+ <th>Open</th>
50
+ <th>Close</th>
51
+ </tr>
52
+ </thead>
53
+ <tbody>
54
+ <% f.fields_for :working_days, @address.working_days_defaults do | working_day_form | %>
55
+ <tr>
56
+ <!-- I18n week_day.day_0...day_6, Sunday..Saturday -->
57
+ <td><%= t "week_day.day_#{working_day_form.object.week_day}" %></td>
58
+ <td>
59
+ <%= working_day_form.hidden_field :week_day %>
60
+ <%= working_day_form.select :start_hour, 0..23, {}, {:class => 'select'} %>
61
+ <%= working_day_form.select :start_min, 0..59, {}, {:class => 'select'} %>
62
+ </td>
63
+ <td>
64
+ <%= working_day_form.select :end_hour, 0..23, {}, {:class => 'select'} %>
65
+ <%= working_day_form.select :end_min, 0..59, {}, {:class => 'select'} %>
66
+ </td>
67
+ </tr>
68
+ <% end %>
69
+ </tbody>
70
+ <table>
71
+ <% end %>
72
+
73
+ #config/locales/en.yml
74
+ week_day:
75
+ day_0: Sunday
76
+ day_1: Monday
77
+ day_2: Tuesday
78
+ day_3: Wednesday
79
+ day_4: Thursday
80
+ day_5: Friday
81
+ day_6: Saturday
34
82
 
35
83
 
36
84
  == Installation
@@ -9,15 +9,16 @@ PKG_FILES = FileList[
9
9
  'lib/**/*',
10
10
  'rails/**/*',
11
11
  'tasks/**/*',
12
- 'test/**/*'
12
+ 'test/**/*',
13
+ 'app/**/*'
13
14
  ]
14
15
 
15
16
  spec = Gem::Specification.new do |s|
16
17
  s.name = "acts_as_working_days"
17
- s.version = "0.0.4"
18
+ s.version = "0.0.6"
18
19
  s.author = "Pablo Cantero"
19
20
  s.email = "pablo@pablocantero.com"
20
- s.homepage = "http://pablocantero.com/"
21
+ s.homepage = "https://github.com/phstc/acts_as_working_days"
21
22
  s.platform = Gem::Platform::RUBY
22
23
  s.summary = "acts_as_working_days is a Rails plugin to add working days on models"
23
24
  s.files = PKG_FILES.to_a
@@ -0,0 +1,27 @@
1
+ class WorkingDay < ActiveRecord::Base
2
+
3
+ validates_inclusion_of :week_day, :in => 0..6
4
+ validates_inclusion_of :start_hour, :in => 0..23
5
+ validates_inclusion_of :start_min, :in => 0..59
6
+ validates_inclusion_of :end_hour, :in => 0..23
7
+ validates_inclusion_of :end_min, :in => 0..59
8
+
9
+ validate :end_bigger_than_start
10
+
11
+ belongs_to :workable, :polymorphic => true
12
+
13
+ validates_associated :workable
14
+
15
+ validates_uniqueness_of :week_day, :scope => [:workable_id, :workable_type]
16
+
17
+ private
18
+
19
+ def end_bigger_than_start
20
+ if end_hour < start_hour
21
+ errors.add :end_hour, "can't be lower than start_hour"
22
+ elsif end_hour == start_hour && end_min < start_min
23
+ errors.add :end_min, "can't be lower than start_min"
24
+ end
25
+ end
26
+
27
+ end
@@ -4,10 +4,10 @@ class ActsAsWorkingDaysMigration < ActiveRecord::Migration
4
4
  create_table :working_days, :force => true do |t|
5
5
  #Date time columns
6
6
  t.integer :week_day
7
- t.integer :start_hour
8
- t.integer :start_min
9
- t.integer :end_hour
10
- t.integer :end_min
7
+ t.integer :start_hour, :default => 0
8
+ t.integer :start_min, :default => 0
9
+ t.integer :end_hour, :default => 23
10
+ t.integer :end_min, :default => 59
11
11
  #Polymorphic columns
12
12
  t.integer :workable_id
13
13
  t.string :workable_type
@@ -8,6 +8,7 @@ module ActsAsWorkingDays
8
8
  def acts_as_working_days
9
9
  # adding relationship to self class
10
10
  has_many :working_days, :as => :workable
11
+ accepts_nested_attributes_for :working_days, :allow_destroy => true
11
12
  send :include, InstanceMethods
12
13
  end
13
14
  end
@@ -28,20 +29,29 @@ module ActsAsWorkingDays
28
29
  return !result.empty?
29
30
  end
30
31
 
31
- def put_working_day(params = {:week_day => Time.now.wday, :start_hour => 0, :start_min => 0, :end_hour => 23, :end_min => 59})
32
- working_day = nil
33
- working_days.each do | wd |
34
- if wd.week_day == params[:week_day]
35
- working_day = wd
36
- break
37
- end
38
- end
39
- if working_day.nil?
40
- working_days.build(params)
41
- else
42
- working_day.update_attributes(params)
32
+ def working_days_defaults
33
+ defaults = []
34
+ 7.times do | wday |
35
+ defaults << (working_days.find_by_week_day(wday, :order => 'week_day ASC') || WorkingDay.new(:week_day => wday))
43
36
  end
37
+ return defaults
44
38
  end
39
+
40
+ # def put_working_day(params = {:week_day => Time.now.wday, :start_hour => 0, :start_min => 0, :end_hour => 23, :end_min => 59})
41
+ # working_day = nil
42
+ # working_days.each do | wd |
43
+ # if wd.week_day == params[:week_day]
44
+ # working_day = wd
45
+ # break
46
+ # end
47
+ # end
48
+ # if working_day.nil?
49
+ # working_days.build(params)
50
+ # else
51
+ # working_day.update_attributes(params)
52
+ # end
53
+ # end
54
+
45
55
  end
46
56
  end
47
57
 
@@ -4,10 +4,10 @@ class ActsAsWorkingDaysMigration < ActiveRecord::Migration
4
4
  create_table :working_days, :force => true do |t|
5
5
  #Date time columns
6
6
  t.integer :week_day
7
- t.integer :start_hour
8
- t.integer :start_min
9
- t.integer :end_hour
10
- t.integer :end_min
7
+ t.integer :start_hour, :default => 0
8
+ t.integer :start_min, :default => 0
9
+ t.integer :end_hour, :default => 23
10
+ t.integer :end_min, :default => 59
11
11
  #Polymorphic columns
12
12
  t.integer :workable_id
13
13
  t.string :workable_type
@@ -18,55 +18,70 @@ class ActsAsWorkingDaysTest < Test::Unit::TestCase
18
18
  assert_equal [], Company.all
19
19
  end
20
20
 
21
- def test_put_working_day
22
- the_leftorium = Company.new(:name => 'The Leftorium')
23
- assert_equal [], the_leftorium.working_days
24
- working_day = the_leftorium.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
25
- the_leftorium.save
26
- assert_equal [working_day], the_leftorium.working_days
27
- assert_equal 0, working_day.week_day
28
- assert_equal 8, working_day.start_hour
29
- assert_equal 30, working_day.start_min
30
- assert_equal 18, working_day.end_hour
31
- assert_equal 45, working_day.end_min
32
- end
21
+ # def test_put_working_day
22
+ # the_leftorium = Company.new(:name => 'The Leftorium')
23
+ # assert_equal [], the_leftorium.working_days
24
+ # working_day = the_leftorium.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
25
+ # the_leftorium.save
26
+ # assert_equal [working_day], the_leftorium.working_days
27
+ # assert_equal 0, working_day.week_day
28
+ # assert_equal 8, working_day.start_hour
29
+ # assert_equal 30, working_day.start_min
30
+ # assert_equal 18, working_day.end_hour
31
+ # assert_equal 45, working_day.end_min
32
+ # end
33
33
 
34
- def test_put_updating_a_working_day
35
- moes_tavern = Company.new(:name => 'Moes Tavern')
36
- working_day = moes_tavern.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
37
- moes_tavern.save
38
- #Updating working day
39
- moes_tavern.put_working_day(:week_day => 0, :start_hour => 10, :start_min => 13, :end_hour => 19, :end_min => 23)
40
- moes_tavern.save
41
- working_day = moes_tavern.working_days.first
42
- assert_equal 0, working_day.week_day
43
- assert_equal 10, working_day.start_hour
44
- assert_equal 13, working_day.start_min
45
- assert_equal 19, working_day.end_hour
46
- assert_equal 23, working_day.end_min
47
- end
34
+ # def test_put_updating_a_working_day
35
+ # moes_tavern = Company.new(:name => 'Moes Tavern')
36
+ # working_day = moes_tavern.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
37
+ # moes_tavern.save
38
+ # #Updating working day
39
+ # moes_tavern.put_working_day(:week_day => 0, :start_hour => 10, :start_min => 13, :end_hour => 19, :end_min => 23)
40
+ # moes_tavern.save
41
+ # working_day = moes_tavern.working_days.first
42
+ # assert_equal 0, working_day.week_day
43
+ # assert_equal 10, working_day.start_hour
44
+ # assert_equal 13, working_day.start_min
45
+ # assert_equal 19, working_day.end_hour
46
+ # assert_equal 23, working_day.end_min
47
+ # end
48
48
 
49
49
  def test_true_working_day
50
50
  moes_tavern = Company.new(:name => 'Moes Tavern')
51
- moes_tavern.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
51
+ #moes_tavern.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
52
+ moes_tavern.working_days.build(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
52
53
  moes_tavern.save
53
54
  assert_equal true, moes_tavern.working_day?(:week_day => 0, :hour => 12, :minute => 0)
54
55
  end
55
56
 
56
- def test_uniqueness_week_day_per_model
57
- nuclear_power_plant = Company.new(:name => 'Springfield Nuclear Power Plant')
58
- working_day = nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
59
- nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 9, :start_min => 15, :end_hour => 19, :end_min => 30)
60
- nuclear_power_plant.save
61
- assert_equal [working_day], nuclear_power_plant.working_days
62
- nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 6, :start_min => 0, :end_hour => 16, :end_min => 15)
63
- nuclear_power_plant.save
64
- assert_equal [working_day], nuclear_power_plant.working_days
57
+ # def test_uniqueness_week_day_per_model
58
+ # nuclear_power_plant = Company.new(:name => 'Springfield Nuclear Power Plant')
59
+ # working_day = nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
60
+ # nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 9, :start_min => 15, :end_hour => 19, :end_min => 30)
61
+ # nuclear_power_plant.save
62
+ # assert_equal [working_day], nuclear_power_plant.working_days
63
+ # nuclear_power_plant.put_working_day(:week_day => 0, :start_hour => 6, :start_min => 0, :end_hour => 16, :end_min => 15)
64
+ # nuclear_power_plant.save
65
+ # assert_equal [working_day], nuclear_power_plant.working_days
66
+ # end
67
+
68
+ def test_working_days_defaults
69
+ kwik_e_mart = Company.new(:name => 'Kwik-E-Mart')
70
+ assert_equal 7, kwik_e_mart.working_days_defaults.size
71
+ assert_equal nil, kwik_e_mart.working_days_defaults.first.id
72
+ #working_day = kwik_e_mart.put_working_day(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
73
+ working_day = kwik_e_mart.working_days.build(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
74
+ kwik_e_mart.save
75
+ assert_equal working_day, kwik_e_mart.working_days_defaults.first
76
+ 7.times do | wday |
77
+ assert_equal wday, kwik_e_mart.working_days_defaults[wday].week_day
78
+ end
65
79
  end
66
80
 
67
81
  def test_false_working_day
68
82
  kwik_e_mart = Company.new(:name => 'Kwik-E-Mart')
69
- kwik_e_mart.put_working_day(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
83
+ #kwik_e_mart.put_working_day(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
84
+ kwik_e_mart.working_days.build(:week_day => 1, :start_hour => 8, :start_min => 30, :end_hour => 18, :end_min => 45)
70
85
  kwik_e_mart.save
71
86
  assert_equal false, kwik_e_mart.working_day?(:week_day => 0, :hour => 12, :min => 0)
72
87
  assert_equal false, kwik_e_mart.working_day?(:week_day => 1, :hour => 21)
@@ -6,4 +6,19 @@ class WorkingDayTest < Test::Unit::TestCase
6
6
  def test_working_day
7
7
  assert_kind_of WorkingDay, WorkingDay.new
8
8
  end
9
+
10
+ def test_end_hour_lower_than_start_hour
11
+ working_day = WorkingDay.new(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 7, :end_min => 45)
12
+ assert !working_day.valid?
13
+ working_day.end_hour = 10
14
+ assert working_day.valid?
15
+ end
16
+
17
+ def test_end_min_lower_than_start_min
18
+ working_day = WorkingDay.new(:week_day => 0, :start_hour => 8, :start_min => 30, :end_hour => 8, :end_min => 15)
19
+ assert !working_day.valid?
20
+ working_day.end_min = 45
21
+ assert working_day.valid?
22
+ end
23
+
9
24
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_working_days
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pablo Cantero
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-19 00:00:00 -02:00
18
+ date: 2010-11-20 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,6 +30,8 @@ extra_rdoc_files:
30
30
  files:
31
31
  - acts_as_working_days-0.0.1.gem
32
32
  - acts_as_working_days-0.0.3.gem
33
+ - acts_as_working_days-0.0.4.gem
34
+ - acts_as_working_days-0.0.5.gem
33
35
  - acts_as_working_days.gemspec
34
36
  - init.rb
35
37
  - install.rb
@@ -49,8 +51,9 @@ files:
49
51
  - test/schema.rb
50
52
  - test/test_helper.rb
51
53
  - test/working_day_test.rb
54
+ - app/models/working_day.rb
52
55
  has_rdoc: true
53
- homepage: http://pablocantero.com/
56
+ homepage: https://github.com/phstc/acts_as_working_days
54
57
  licenses: []
55
58
 
56
59
  post_install_message: