birthday 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -9
- data/lib/railslove/acts/birthday/birthday.rb +2 -0
- data/lib/railslove/acts/birthday/version.rb +1 -1
- data/spec/birthday_spec.rb +17 -3
- metadata +5 -5
data/README.md
CHANGED
@@ -10,16 +10,16 @@ To install this gem, fire this command from your terminal:
|
|
10
10
|
|
11
11
|
or add this line to your Gemfile:
|
12
12
|
|
13
|
-
gem 'birthday', '~> 0.
|
13
|
+
gem 'birthday', '~> 0.2.0'
|
14
14
|
|
15
15
|
## Synopsis
|
16
16
|
|
17
17
|
After installing this gem, you are able to work with anniversaries, such as birthdays in such a manner:
|
18
18
|
|
19
19
|
class User < ActiveRecord::Base
|
20
|
-
|
20
|
+
|
21
21
|
acts_as_birthday :birthday
|
22
|
-
|
22
|
+
|
23
23
|
end
|
24
24
|
|
25
25
|
Add `acts_as_birthday :field_name` to your ActiveRecord model (or symbols for multiple fields, if you need to), and right away you can access methods:
|
@@ -37,10 +37,16 @@ it will automatically create methods: `birthday_age`, `birthday_today?`, `annive
|
|
37
37
|
|
38
38
|
### Created scopes
|
39
39
|
|
40
|
-
On top of that you get useful scopes: `find_birthdays_for`, `find_anniversaries_for` and `find_something_elses_for`.
|
40
|
+
On top of that you get useful scopes: `birthday_today`, `find_birthdays_for`, `anniversary_today`, `find_anniversaries_for`, `something_else_today`, and `find_something_elses_for`.
|
41
41
|
|
42
42
|
These scopes accept maximum of two parameters, which have to respond to method `to_date` (by default objects of class Date, Time and DateTime). Thanks to these now you can search for birthdays...
|
43
43
|
|
44
|
+
* today:
|
45
|
+
|
46
|
+
# Let's say today is April 24th:
|
47
|
+
> User.birthday_today
|
48
|
+
=> [#<User id: 56, birthday: "1976-04-24">]
|
49
|
+
|
44
50
|
* on a specific date:
|
45
51
|
|
46
52
|
> User.find_birthdays_for(Date.parse('04-04-2000'))
|
@@ -60,15 +66,15 @@ These scopes accept maximum of two parameters, which have to respond to method `
|
|
60
66
|
Since all these are essentially scopes, there's nothing stopping you from chaining them with other scopes:
|
61
67
|
|
62
68
|
class User < ActiveRecord::Base
|
63
|
-
|
69
|
+
|
64
70
|
acts_as_birthday :birthday
|
65
|
-
|
71
|
+
|
66
72
|
scope :admins, {:is_admin => true}
|
67
73
|
scope :named_like, lambda { |name| { :conditions => [ "first_name LIKE :q OR email LIKE :q OR last_name LIKE :q", { :q => "%#{name}%" } ] } }
|
68
|
-
|
74
|
+
|
69
75
|
end
|
70
|
-
|
71
|
-
|
76
|
+
|
77
|
+
|
72
78
|
> User.admins.named_like("Mike").find_birthdays_for(Date.parse('12-12-2000'), Date.parse('03-01-2001'))
|
73
79
|
|
74
80
|
### Your own adapters
|
@@ -37,6 +37,8 @@ module Railslove
|
|
37
37
|
::Railslove::Acts::Birthday::Adapter.adapter_for(self.connection).scope_hash(field, date_start, date_end)
|
38
38
|
})
|
39
39
|
|
40
|
+
self.send(scope_method, :"#{field.to_s}_today", lambda{ self.send(:"find_#{field.to_s.pluralize}_for", Date.today) })
|
41
|
+
|
40
42
|
class_eval %{
|
41
43
|
def #{field}_age
|
42
44
|
return nil unless self.#{field}?
|
data/spec/birthday_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe Marriage do
|
|
30
30
|
@m1.anniversary_age.should eq(1)
|
31
31
|
@m2.anniversary_age.should eq(2)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it 'finds people\'s by anniversary date' do
|
35
35
|
time_travel_to Date.parse('2011-08-08')
|
36
36
|
anniversaries_today = Marriage.find_anniversaries_for(Date.today)
|
@@ -38,6 +38,13 @@ describe Marriage do
|
|
38
38
|
anniversaries_today.should include(@m2)
|
39
39
|
end
|
40
40
|
|
41
|
+
it 'finds people\'s by anniversary date - dumb scope' do
|
42
|
+
time_travel_to Date.parse('2011-08-08')
|
43
|
+
anniversaries_today = Marriage.anniversary_today
|
44
|
+
anniversaries_today.should_not include(@m1)
|
45
|
+
anniversaries_today.should include(@m2)
|
46
|
+
end
|
47
|
+
|
41
48
|
it 'finds people\'s by anniversary dates' do
|
42
49
|
anniversaries = Marriage.find_anniversaries_for(Date.parse('2011-08-01'), Date.parse('2011-12-12'))
|
43
50
|
anniversaries.should include(@m1)
|
@@ -73,7 +80,7 @@ describe Person do
|
|
73
80
|
@p1.birthday_age.should eq(4)
|
74
81
|
@p2.birthday_age.should eq(7)
|
75
82
|
end
|
76
|
-
|
83
|
+
|
77
84
|
it 'finds people\'s by birthday date' do
|
78
85
|
time_travel_to Date.parse('2011-08-08')
|
79
86
|
birthdays_today = Person.find_birthdays_for(Date.today)
|
@@ -81,6 +88,13 @@ describe Person do
|
|
81
88
|
birthdays_today.should include(@p2)
|
82
89
|
end
|
83
90
|
|
91
|
+
it 'finds people\'s by birthday date - dumb scope' do
|
92
|
+
time_travel_to Date.parse('2011-08-08')
|
93
|
+
birthdays_today = Person.birthday_today
|
94
|
+
birthdays_today.should_not include(@p1)
|
95
|
+
birthdays_today.should include(@p2)
|
96
|
+
end
|
97
|
+
|
84
98
|
it 'finds people\'s by birthday dates' do
|
85
99
|
birthdays = Person.find_birthdays_for(Date.parse('2011-08-01'), Date.parse('2011-12-12'))
|
86
100
|
birthdays.should include(@p1)
|
@@ -92,4 +106,4 @@ describe Person do
|
|
92
106
|
birthdays.should include(@p1)
|
93
107
|
birthdays.should include(@p2)
|
94
108
|
end
|
95
|
-
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: birthday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Mike Po\xC5\x82tyn"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-12-09 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|