rails-scheduler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ env:
5
+ - DB=mysql
6
+ before_script:
7
+ - "mysql -e 'create database scheduler_test;'"
@@ -0,0 +1,28 @@
1
+ //= require jquery
2
+
3
+ var Scheduler = Scheduler || {};
4
+
5
+ Scheduler.form = {};
6
+
7
+ Scheduler.form.init = function(form, startDate) {
8
+ Scheduler.form.current = form;
9
+
10
+ Scheduler.form.showFrequency(form.find("select.scheduler-frequency").val());
11
+
12
+ form.on("change", "select.scheduler-frequency", function(){
13
+ Scheduler.form.showFrequency($(this).val());
14
+ });
15
+
16
+ if (startDate) {
17
+ // check day of week
18
+ dayOfWeek = (startDate.getDay() + 6) % 7; // monday is 0 in Rails
19
+ form.find('#event_interval_days_' + dayOfWeek).attr('checked','checked');
20
+ }
21
+ }
22
+
23
+ Scheduler.form.showFrequency = function(value) {
24
+ form = Scheduler.form.current;
25
+
26
+ form.find("#scheduler-options").children("div").hide();
27
+ form.find("#scheduler-frequency-" + value ).show();
28
+ }
@@ -0,0 +1,34 @@
1
+ <div class="scheduler_form">
2
+ <%= f.label :frequency %>
3
+ <%= f.select :frequency, Scheduler.frequency_options, {}, :class => 'scheduler-frequency' %>
4
+
5
+ <div id="scheduler-options">
6
+ <div id="scheduler-frequency-0">
7
+ <%= t 'scheduler.frequency.one_off.form.message' %>
8
+ </div>
9
+
10
+ <div id="scheduler-frequency-1">
11
+ Comming soon..
12
+ </div>
13
+
14
+ <div id="scheduler-frequency-2">
15
+ <span id="scheduler-weekly-week">
16
+ <%= raw t('scheduler.frequency.weekly.form.every_week',
17
+ :form_field => f.number_field(:interval, :size => 1)) %>
18
+ </span>
19
+
20
+ <div id="scheduler-weekly-days">
21
+ <% 7.times do |d| %>
22
+ <%= check_box_tag "#{ f.object_name }[interval_days][]",
23
+ d,
24
+ f.object.interval_days.include?(d),
25
+ :id => "event_interval_days_#{ d }" %>
26
+ <span><%= I18n.l Date.today.beginning_of_week + d, :format => "%a" %></span>
27
+ <% end %>
28
+ </div>
29
+ </div>
30
+
31
+ <div id="scheduler-frequency-3">
32
+ Comming soon..
33
+ </div>
34
+ </div>
@@ -0,0 +1,17 @@
1
+ es:
2
+ scheduler:
3
+ frequency:
4
+ one_off:
5
+ form:
6
+ message: "La actividad no se volverá a repetir"
7
+ option: "ninguna"
8
+ daily:
9
+ form:
10
+ option: "diaria"
11
+ weekly:
12
+ form:
13
+ every_week: "Cada %{form_field} semana(s) en los siguientes días:"
14
+ option: "semanal"
15
+ monthly:
16
+ form:
17
+ option: "mensual"
@@ -5,9 +5,9 @@ class AddScheduler < ActiveRecord::Migration
5
5
  t.datetime :end_at
6
6
  t.date :start_date
7
7
  t.date :end_date
8
- t.integer :frequency
8
+ t.integer :frequency, :default => 0
9
9
  t.integer :interval
10
- t.integer :interval_flag
10
+ t.integer :interval_flag, :default => 0
11
11
  end
12
12
  end
13
13
 
@@ -1,9 +1,10 @@
1
- require "scheduler/version"
1
+ require 'scheduler/version'
2
+ require 'recurrence'
2
3
 
3
4
  module Scheduler
4
5
  autoload :ActiveRecord, 'scheduler/active_record'
5
6
  autoload :Model, 'scheduler/model'
6
- # Your code goes here...
7
7
  end
8
8
 
9
+ require 'scheduler'
9
10
  require 'scheduler/engine'
data/lib/scheduler.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Scheduler
2
+ FREQUENCIES = [ :one_off, :daily, :weekly, :monthly ]
3
+
4
+ class << self
5
+ def frequency_options
6
+ FREQUENCIES.each_with_index.map{ |value, index|
7
+ [ I18n.t("scheduler.frequency.#{ value }.form.option"), index ]
8
+ }
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,4 @@
1
- module Schedule
1
+ module Scheduler
2
2
  class Engine < ::Rails::Engine #:nodoc:
3
3
 
4
4
  initializer "scheduler.active_record" do
@@ -5,14 +5,68 @@ module Scheduler
5
5
  included do
6
6
  validates_presence_of :start_at, :end_at
7
7
 
8
- before_validation :fill_start_date
8
+ before_validation :fill_scheduler_dates
9
+
10
+ validates_presence_of :interval,
11
+ :if => lambda { |record|
12
+ [ :weekly ].include? record.frequency_sym
13
+ }
14
+
15
+ validates_numericality_of :interval_flag,
16
+ :greater_than => 0,
17
+ :if => lambda { |record|
18
+ [ :weekly ].include? record.frequency_sym
19
+ }
20
+
21
+ scope :between, lambda { |start_time, end_time|
22
+ where('start_date <= ?', end_time).
23
+ where(arel_table[:end_date].eq(nil).or(arel_table[:end_date].gteq(start_time)))
24
+ }
9
25
  end
10
26
 
11
27
  module InstanceMethods
28
+ def frequency_sym
29
+ Scheduler::FREQUENCIES[frequency]
30
+ end
31
+
32
+ # Get [ 0, 1, 6 ] (monday, tuesday, sunday) from byte
33
+ def interval_days
34
+ 7.times.map{ |i|
35
+ interval_flag[i] > 0 ?
36
+ i :
37
+ nil
38
+ }.compact
39
+ end
40
+
41
+ def interval_days_sym
42
+ interval_days.map{ |d|
43
+ Date::DAYS_INTO_WEEK.invert[d]
44
+ }
45
+ end
46
+
47
+ # Convert [ 0, 1, 6 ] (monday, tuesday, sunday) to byte
48
+ def interval_days= days
49
+ self.interval_flag =
50
+ days.map{ |d| 2 ** d.to_i }.inject(0, &:+)
51
+ end
52
+
53
+ def recurrence
54
+ case frequency_sym
55
+ when :weekly
56
+ Recurrence.new({ :every => :week,
57
+ :starts => start_date.to_date,
58
+ :until => end_date.try(:to_date),
59
+ :interval => interval,
60
+ :on => interval_days_sym })
61
+ end
62
+ end
63
+
12
64
  protected
13
65
 
14
- def fill_start_date
66
+ def fill_scheduler_dates
15
67
  self.start_date ||= start_at
68
+
69
+ self.end_date = end_at if frequency == 0
16
70
  end
17
71
  end
18
72
  end
@@ -1,3 +1,3 @@
1
1
  module Scheduler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_dependency "rails", "~> 3.1.0"
23
+ s.add_dependency "recurrence", '~> 1.2.0'
23
24
 
24
25
  s.add_development_dependency "mysql2"
25
26
  s.add_development_dependency "factory_girl"
@@ -1,6 +1,6 @@
1
1
  # Be sure to restart your server when you modify this file.
2
2
 
3
- Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
4
 
5
5
  # Use the database for sessions instead of the cookie-based default,
6
6
  # which shouldn't be used to store highly confidential information
@@ -5,7 +5,7 @@
5
5
 
6
6
  # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
7
  ActiveSupport.on_load(:action_controller) do
8
- wrap_parameters format: [:json]
8
+ wrap_parameters :format => [:json]
9
9
  end
10
10
 
11
11
  # Disable root element in JSON by default.
@@ -8,4 +8,26 @@ class EventTest < ActiveSupport::TestCase
8
8
  test "fills start_date" do
9
9
  assert Factory(:event).start_date.present?
10
10
  end
11
+
12
+ test "interval_days" do
13
+ e = Factory(:event)
14
+
15
+ battery = {
16
+ 0 => [],
17
+ 1 => [0],
18
+ 2 => [1],
19
+ 3 => [0, 1],
20
+ 4 => [2],
21
+ 5 => [0, 2],
22
+ 127 => [0, 1, 2, 3, 4, 5, 6]
23
+ }
24
+
25
+ battery.each_pair do |flag, days|
26
+ e.interval_flag = flag
27
+ assert e.interval_days.eql?(days)
28
+
29
+ e.interval_days = days
30
+ assert e.interval_flag.eql?(flag)
31
+ end
32
+ end
11
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-04 00:00:00.000000000Z
12
+ date: 2011-12-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &79996150 !ruby/object:Gem::Requirement
16
+ requirement: &69827090 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *79996150
24
+ version_requirements: *69827090
25
+ - !ruby/object:Gem::Dependency
26
+ name: recurrence
27
+ requirement: &69822940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *69822940
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: mysql2
27
- requirement: &79995940 !ruby/object:Gem::Requirement
38
+ requirement: &69821950 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *79995940
46
+ version_requirements: *69821950
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: factory_girl
38
- requirement: &79995710 !ruby/object:Gem::Requirement
49
+ requirement: &69821140 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *79995710
57
+ version_requirements: *69821140
47
58
  description: ! 'Scheduler support: daily, weekly and montly repetitions'
48
59
  email:
49
60
  - atapiador@dit.upm.es
@@ -52,12 +63,17 @@ extensions: []
52
63
  extra_rdoc_files: []
53
64
  files:
54
65
  - .gitignore
66
+ - .travis.yml
55
67
  - Gemfile
56
68
  - MIT-LICENSE
57
69
  - README.rdoc
58
70
  - Rakefile
71
+ - app/assets/javascripts/scheduler.js
72
+ - app/views/scheduler/_form.html.erb
73
+ - config/locales/es.yml
59
74
  - db/migrate/20111204155637_add_scheduler.rb
60
75
  - lib/rails-scheduler.rb
76
+ - lib/scheduler.rb
61
77
  - lib/scheduler/active_record.rb
62
78
  - lib/scheduler/engine.rb
63
79
  - lib/scheduler/migrations.rb
@@ -117,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
133
  version: '0'
118
134
  segments:
119
135
  - 0
120
- hash: -384905603
136
+ hash: -654998783
121
137
  required_rubygems_version: !ruby/object:Gem::Requirement
122
138
  none: false
123
139
  requirements:
@@ -126,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
142
  version: '0'
127
143
  segments:
128
144
  - 0
129
- hash: -384905603
145
+ hash: -654998783
130
146
  requirements: []
131
147
  rubyforge_project:
132
148
  rubygems_version: 1.8.10