unavailability 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +98 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/unavailable_date_generator.rb +14 -0
- data/lib/unavailability/unavailable_date.rb +30 -0
- data/lib/unavailability/unavailable_dates/add.rb +63 -0
- data/lib/unavailability/unavailable_dates/migration.rb +11 -0
- data/lib/unavailability/unavailable_dates/remove.rb +76 -0
- data/lib/unavailability/version.rb +3 -0
- data/lib/unavailability.rb +39 -0
- data/unavailability.gemspec +35 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0b33f3fd5eb1d2a405c7ffc002b106abcbc43f1ae6b34bafd5c67cd0380b5583
|
4
|
+
data.tar.gz: 5afe3b74453861c2804305bb30b48a9505d8995b681540d9e72c3473592d7370
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b53bd70db6f3014f0afda00c3c3e55f2109708f969927da20c917114eb13b62459840f66136a8504e1ecbb9d0d46f608b1fdcc686f34f8a3a69f6658c04e42f2
|
7
|
+
data.tar.gz: ac0c200dd3f924c47ccc34771541c04ddd0ad0f811a7b527ecf3087e95744c633e80ecae9bd54290352eaf3f339923a8c7862d4580d60040c76f3bd051f7fbff
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at khiet.le@ehochef.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
unavailability (0.1.1)
|
5
|
+
activerecord (~> 5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (5.2.1.1)
|
11
|
+
actionview (= 5.2.1.1)
|
12
|
+
activesupport (= 5.2.1.1)
|
13
|
+
rack (~> 2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
+
actionview (5.2.1.1)
|
18
|
+
activesupport (= 5.2.1.1)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
+
activemodel (5.2.1.1)
|
24
|
+
activesupport (= 5.2.1.1)
|
25
|
+
activerecord (5.2.1.1)
|
26
|
+
activemodel (= 5.2.1.1)
|
27
|
+
activesupport (= 5.2.1.1)
|
28
|
+
arel (>= 9.0)
|
29
|
+
activesupport (5.2.1.1)
|
30
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
+
i18n (>= 0.7, < 2)
|
32
|
+
minitest (~> 5.1)
|
33
|
+
tzinfo (~> 1.1)
|
34
|
+
arel (9.0.0)
|
35
|
+
builder (3.2.3)
|
36
|
+
byebug (10.0.2)
|
37
|
+
concurrent-ruby (1.1.3)
|
38
|
+
crass (1.0.4)
|
39
|
+
diff-lcs (1.3)
|
40
|
+
erubi (1.7.1)
|
41
|
+
i18n (1.1.1)
|
42
|
+
concurrent-ruby (~> 1.0)
|
43
|
+
loofah (2.2.3)
|
44
|
+
crass (~> 1.0.2)
|
45
|
+
nokogiri (>= 1.5.9)
|
46
|
+
method_source (0.9.2)
|
47
|
+
mini_portile2 (2.3.0)
|
48
|
+
minitest (5.11.3)
|
49
|
+
nokogiri (1.8.5)
|
50
|
+
mini_portile2 (~> 2.3.0)
|
51
|
+
rack (2.0.6)
|
52
|
+
rack-test (1.1.0)
|
53
|
+
rack (>= 1.0, < 3)
|
54
|
+
rails-dom-testing (2.0.3)
|
55
|
+
activesupport (>= 4.2.0)
|
56
|
+
nokogiri (>= 1.6)
|
57
|
+
rails-html-sanitizer (1.0.4)
|
58
|
+
loofah (~> 2.2, >= 2.2.2)
|
59
|
+
railties (5.2.1.1)
|
60
|
+
actionpack (= 5.2.1.1)
|
61
|
+
activesupport (= 5.2.1.1)
|
62
|
+
method_source
|
63
|
+
rake (>= 0.8.7)
|
64
|
+
thor (>= 0.19.0, < 2.0)
|
65
|
+
rake (10.5.0)
|
66
|
+
rspec (3.8.0)
|
67
|
+
rspec-core (~> 3.8.0)
|
68
|
+
rspec-expectations (~> 3.8.0)
|
69
|
+
rspec-mocks (~> 3.8.0)
|
70
|
+
rspec-core (3.8.0)
|
71
|
+
rspec-support (~> 3.8.0)
|
72
|
+
rspec-expectations (3.8.2)
|
73
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
+
rspec-support (~> 3.8.0)
|
75
|
+
rspec-mocks (3.8.0)
|
76
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
77
|
+
rspec-support (~> 3.8.0)
|
78
|
+
rspec-support (3.8.0)
|
79
|
+
sqlite3 (1.3.13)
|
80
|
+
thor (0.20.3)
|
81
|
+
thread_safe (0.3.6)
|
82
|
+
tzinfo (1.2.5)
|
83
|
+
thread_safe (~> 0.1)
|
84
|
+
|
85
|
+
PLATFORMS
|
86
|
+
ruby
|
87
|
+
|
88
|
+
DEPENDENCIES
|
89
|
+
bundler (~> 1.16)
|
90
|
+
byebug (~> 10.0.2)
|
91
|
+
railties (~> 5.0)
|
92
|
+
rake (~> 10.0)
|
93
|
+
rspec (~> 3.0)
|
94
|
+
sqlite3 (~> 1.3.11)
|
95
|
+
unavailability!
|
96
|
+
|
97
|
+
BUNDLED WITH
|
98
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Khiet Le
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
![travis ci](https://travis-ci.org/khiet/unavailability.svg?branch=master)
|
2
|
+
|
3
|
+
# Unavailability
|
4
|
+
|
5
|
+
Unavailability simply adds a capability to manage availabilities.
|
6
|
+
I extracted this gem from the work I am doing for [ehochef.com](https://ehochef.com) where I have to allow a user to make their day either _available_ or _unavailable_ via a calendar (I use [simple_calendar](https://github.com/excid3/simple_calendar) for UI).
|
7
|
+
|
8
|
+
Since we want to keep a user's availability _available_ by default, managing i.e. adding/removing "unavailability" rather than "availability" makes sense to me; hence, the name of the gem and the logic followed in the code.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "unavailability"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
bundle install
|
22
|
+
```
|
23
|
+
|
24
|
+
Generate a migration file to create the `unavailable_dates` table:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
rails generate unavailable_date
|
28
|
+
```
|
29
|
+
|
30
|
+
Run the migration:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
rails db:migrate
|
34
|
+
```
|
35
|
+
|
36
|
+
Edit a Model as the following:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# Since it is a Polymorphic association, it can be added to any number of Models
|
40
|
+
class User < ApplicationRecord
|
41
|
+
include Unavailability
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
Check if the model is available for a Date:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
user.available_for_date?(Date.today)
|
51
|
+
```
|
52
|
+
|
53
|
+
Return all available Users:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
User.available_for_date(Date.today)
|
57
|
+
```
|
58
|
+
|
59
|
+
Make a Date range unavailable:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
user.make_available(from: Date.parse('2050-01-10'), to: Date.parse('2050-01-15'))
|
63
|
+
```
|
64
|
+
|
65
|
+
Make a Date range available:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
user.make_unavailable(from: Date.parse('2050-01-08'), to: Date.parse('2050-01-08'))
|
69
|
+
```
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/unavailability. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
78
|
+
|
79
|
+
## Code of Conduct
|
80
|
+
|
81
|
+
Everyone interacting in the Unavailability project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/unavailability/blob/master/CODE_OF_CONDUCT.md).
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "unavailability"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
class UnavailableDateGenerator < ActiveRecord::Generators::Base
|
4
|
+
source_root File.expand_path('../../unavailability/unavailable_dates', __FILE__)
|
5
|
+
|
6
|
+
desc 'Generate a migration file to create the `unavailable_dates` table'
|
7
|
+
|
8
|
+
# https://github.com/norman/friendly_id/blob/master/lib/generators/friendly_id_generator.rb#L7
|
9
|
+
argument :name, type: :string, default: 'random_name'
|
10
|
+
|
11
|
+
def generate_migration
|
12
|
+
migration_template 'migration.rb', 'db/migrate/create_unavailable_dates.rb'
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Unavailability
|
2
|
+
class UnavailableDate < ActiveRecord::Base
|
3
|
+
belongs_to :dateable, polymorphic: true
|
4
|
+
|
5
|
+
validates :from, :to, :dateable, presence: true
|
6
|
+
# validates :dateable, uniqueness: { scope: [:from, :to] }
|
7
|
+
|
8
|
+
scope :overlapping, ->(from, to) do
|
9
|
+
where(from_overlapped_by(to: to)).where(to_overlapped_by(from: from))
|
10
|
+
end
|
11
|
+
|
12
|
+
scope :overlapping_including_nabour, ->(from, to) do
|
13
|
+
overlapping(from.to_date - 1, to.to_date + 1)
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def to_overlapped_by(from:)
|
18
|
+
table[:to].gteq(from)
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_overlapped_by(to:)
|
22
|
+
table[:from].lteq(to)
|
23
|
+
end
|
24
|
+
|
25
|
+
def table
|
26
|
+
Unavailability::UnavailableDate.arel_table
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Unavailability
|
2
|
+
module UnavailableDates
|
3
|
+
class Add
|
4
|
+
def initialize(dateable, from, to)
|
5
|
+
@dateable = dateable
|
6
|
+
@from = from
|
7
|
+
@to = to
|
8
|
+
|
9
|
+
raise ArgumentError.new('from has to be a Date') unless @from.is_a?(Date)
|
10
|
+
raise ArgumentError.new('to has to be a Date') unless @to.is_a?(Date)
|
11
|
+
end
|
12
|
+
|
13
|
+
def unavailable_dates
|
14
|
+
dateable.unavailable_dates
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
if overlappings.empty?
|
19
|
+
unavailable_dates.create(
|
20
|
+
from: from,
|
21
|
+
to: to
|
22
|
+
)
|
23
|
+
else
|
24
|
+
overlappings.each do |unavailability|
|
25
|
+
update(unavailability, from, to)
|
26
|
+
end
|
27
|
+
|
28
|
+
merge(overlappings)
|
29
|
+
end
|
30
|
+
|
31
|
+
dateable
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :dateable, :from, :to
|
37
|
+
|
38
|
+
def overlappings
|
39
|
+
unavailable_dates.overlapping_including_nabour(from, to)
|
40
|
+
end
|
41
|
+
|
42
|
+
def update(unavailability, from, to)
|
43
|
+
if from < unavailability.from
|
44
|
+
if to > unavailability.to
|
45
|
+
unavailability.update(from: from)
|
46
|
+
unavailability.update(to: to)
|
47
|
+
else
|
48
|
+
unavailability.update(from: from)
|
49
|
+
end
|
50
|
+
elsif to > unavailability.to
|
51
|
+
unavailability.update(to: to)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def merge(overlappings)
|
56
|
+
from = overlappings.map(&:from).min
|
57
|
+
to = overlappings.map(&:to).max
|
58
|
+
overlappings.each(&:destroy)
|
59
|
+
unavailable_dates.create(from: from, to: to)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Unavailability
|
2
|
+
module UnavailableDates
|
3
|
+
class Remove
|
4
|
+
def initialize(dateable, from, to)
|
5
|
+
@dateable = dateable
|
6
|
+
@from = from
|
7
|
+
@to = to
|
8
|
+
|
9
|
+
raise ArgumentError.new('from has to be a Date') unless @from.is_a?(Date)
|
10
|
+
raise ArgumentError.new('to has to be a Date') unless @to.is_a?(Date)
|
11
|
+
end
|
12
|
+
|
13
|
+
def unavailable_dates
|
14
|
+
dateable.unavailable_dates
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
if overlappings.empty?
|
19
|
+
# do nothing
|
20
|
+
else
|
21
|
+
overlappings.each do |unavailability|
|
22
|
+
update(unavailability, from, to)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
dateable
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :dateable, :from, :to
|
32
|
+
|
33
|
+
def overlappings
|
34
|
+
unavailable_dates.overlapping(from, to)
|
35
|
+
end
|
36
|
+
|
37
|
+
def update(unavailability, from, to)
|
38
|
+
if from <= unavailability.from &&
|
39
|
+
to >= unavailability.to
|
40
|
+
|
41
|
+
unavailability.destroy
|
42
|
+
else
|
43
|
+
if from < unavailability.from
|
44
|
+
unavailability.update(from: to + 1)
|
45
|
+
elsif to > unavailability.to
|
46
|
+
unavailability.update(to: from - 1)
|
47
|
+
else # middle
|
48
|
+
if from == unavailability.from
|
49
|
+
if to < unavailability.to
|
50
|
+
unavailability.update(from: to + 1)
|
51
|
+
end
|
52
|
+
elsif to == unavailability.to
|
53
|
+
if from > unavailability.from
|
54
|
+
unavailability.update(to: from - 1)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
to < unavailability.to
|
58
|
+
split(unavailability, from, to)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def split(unavailability, from, to)
|
65
|
+
left_from = unavailability.from
|
66
|
+
left_to = from - 1
|
67
|
+
right_from = to + 1
|
68
|
+
right_to = unavailability.to
|
69
|
+
|
70
|
+
unavailability.destroy
|
71
|
+
unavailable_dates.create(from: left_from, to: left_to)
|
72
|
+
unavailable_dates.create(from: right_from, to: right_to)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "unavailability/version"
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
require 'unavailability/unavailable_date'
|
6
|
+
require 'unavailability/unavailable_dates/add'
|
7
|
+
require 'unavailability/unavailable_dates/remove'
|
8
|
+
|
9
|
+
module Unavailability
|
10
|
+
def self.included(base)
|
11
|
+
base.class_eval do
|
12
|
+
has_many :unavailable_dates, as: :dateable, dependent: :destroy, class_name: 'Unavailability::UnavailableDate'
|
13
|
+
|
14
|
+
scope :available_for_date, ->(date) do
|
15
|
+
user_table = arel_table
|
16
|
+
u_table = Unavailability::UnavailableDate.arel_table
|
17
|
+
u_condition = u_table[:dateable_id].eq(user_table[:id]).and(u_table[:from].lteq(date)).and(u_table[:to].gteq(date))
|
18
|
+
|
19
|
+
where(Unavailability::UnavailableDate.where(u_condition).exists.not)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def available_for_date?(date)
|
25
|
+
unavailable_dates.each do |unavailable_date|
|
26
|
+
return false if (unavailable_date.from..unavailable_date.to).cover?(date)
|
27
|
+
end
|
28
|
+
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def make_unavailable(from:, to:)
|
33
|
+
Unavailability::UnavailableDates::Add.new(self, from, to).call
|
34
|
+
end
|
35
|
+
|
36
|
+
def make_available(from:, to:)
|
37
|
+
Unavailability::UnavailableDates::Remove.new(self, from, to).call
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "unavailability/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unavailability"
|
8
|
+
spec.version = Unavailability::VERSION
|
9
|
+
spec.authors = ["Khiet Le"]
|
10
|
+
spec.email = ["khiet.le@ehochef.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Manage availabilities of a Model}
|
13
|
+
spec.homepage = "https://github.com/khiet/unavailability"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency 'activerecord', '~> 5.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'railties', "~> 5.0"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "byebug", "~> 10.0"
|
30
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
31
|
+
|
32
|
+
spec.description = <<-EOM
|
33
|
+
Unavailability simply adds a capability to manage availabilities.
|
34
|
+
EOM
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unavailability
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Khiet Le
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.3'
|
111
|
+
description: " Unavailability simply adds a capability to manage availabilities.\n"
|
112
|
+
email:
|
113
|
+
- khiet.le@ehochef.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- Gemfile.lock
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- lib/generators/unavailable_date_generator.rb
|
130
|
+
- lib/unavailability.rb
|
131
|
+
- lib/unavailability/unavailable_date.rb
|
132
|
+
- lib/unavailability/unavailable_dates/add.rb
|
133
|
+
- lib/unavailability/unavailable_dates/migration.rb
|
134
|
+
- lib/unavailability/unavailable_dates/remove.rb
|
135
|
+
- lib/unavailability/version.rb
|
136
|
+
- unavailability.gemspec
|
137
|
+
homepage: https://github.com/khiet/unavailability
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.7.6
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Manage availabilities of a Model
|
161
|
+
test_files: []
|