microscope 0.1.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +11 -0
- data/README.md +19 -8
- data/gemfiles/Gemfile.activerecord-3.2.x +5 -0
- data/gemfiles/Gemfile.activerecord-4.0 +5 -0
- data/lib/microscope.rb +1 -5
- data/lib/microscope/mixin.rb +27 -16
- data/lib/microscope/version.rb +1 -1
- data/spec/microscope/mixin_spec.rb +60 -0
- metadata +7 -4
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Microscope
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/microscope.png)](https://rubygems.org/gems/microscope)
|
4
|
+
[![Build Status](https://travis-ci.org/mirego/microscope.png?branch=master)](https://travis-ci.org/mirego/microscope)
|
5
|
+
|
6
|
+
Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime fields.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -17,6 +20,7 @@ create_table "events" do |t|
|
|
17
20
|
t.string "name"
|
18
21
|
t.boolean "special"
|
19
22
|
t.datetime "expired_at"
|
23
|
+
t.date "started_on"
|
20
24
|
end
|
21
25
|
|
22
26
|
class Event < ActiveRecord::Base
|
@@ -33,16 +37,22 @@ Time.now
|
|
33
37
|
# => 2013-07-05 15:43:42
|
34
38
|
|
35
39
|
Event.expired_before(2.months.ago)
|
36
|
-
# SELECT * FROM `events` where `events`.`expired_at`
|
40
|
+
# SELECT * FROM `events` where `events`.`expired_at` < '2013-05-05 15:43:42'
|
37
41
|
|
38
42
|
Event.expired_before_now
|
39
|
-
# SELECT * FROM `events` where `events`.`expired_at`
|
43
|
+
# SELECT * FROM `events` where `events`.`expired_at` < '2013-07-05 15:43:42'
|
40
44
|
|
41
45
|
Event.expired_after_or_at(2.months.from_now)
|
42
46
|
# SELECT * FROM `events` where `events`.`expired_at` >= '2013-09-05 15:43:42'
|
43
47
|
|
44
48
|
Event.expired_between(2.months.ago..1.month.from_now)
|
45
49
|
# SELECT * FROM `events` where `events`.`expired_at` BETWEEN '2013-05-05 15:43:42' AND '2013-08-05 15:43:42'
|
50
|
+
|
51
|
+
Event.started_before(2.days.ago)
|
52
|
+
# SELECT * FROM `events` where `events`.`started_on` < '2013-07-03'
|
53
|
+
|
54
|
+
Event.started_between(2.days.ago..3.days.from_now)
|
55
|
+
# SELECT * FROM `events` where `events`.`started_on` BETWEEN '2013-07-03' AND '2013-07-08'
|
46
56
|
```
|
47
57
|
|
48
58
|
### Options
|
@@ -55,14 +65,14 @@ class Event < ActiveRecord::Base
|
|
55
65
|
end
|
56
66
|
|
57
67
|
class User < ActiveRecord::Base
|
58
|
-
acts_as_microscope, except: [:
|
68
|
+
acts_as_microscope, except: [:created_at]
|
59
69
|
end
|
60
70
|
|
61
71
|
Event.created_before(2.months.ago) # works!
|
62
72
|
Event.updated_before(2.months.ago) # NoMethodError
|
63
73
|
|
64
|
-
User.created_before(2.months.ago) #
|
65
|
-
User.
|
74
|
+
User.created_before(2.months.ago) # NoMethodError
|
75
|
+
User.updated_before(2.months.ago) # works!
|
66
76
|
```
|
67
77
|
|
68
78
|
## License
|
@@ -71,5 +81,6 @@ User.activated_before(2.months.ago) # NoMethodError
|
|
71
81
|
|
72
82
|
## About Mirego
|
73
83
|
|
74
|
-
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun.
|
75
|
-
|
84
|
+
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
|
85
|
+
|
86
|
+
We also love [open-source software](http://open.mirego.com/) and we try to extract as much code as possible from our projects to give back to the community.
|
data/lib/microscope.rb
CHANGED
@@ -9,12 +9,8 @@ module Microscope
|
|
9
9
|
def self.inject_into_active_record
|
10
10
|
@inject_into_active_record ||= Proc.new do
|
11
11
|
def self.acts_as_microscope(options = {})
|
12
|
-
self.microscope_options = options
|
13
|
-
self.send :include, Microscope::Mixin
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.microscope_options=(options)
|
17
12
|
@microscope_options = options
|
13
|
+
include Microscope::Mixin
|
18
14
|
end
|
19
15
|
end
|
20
16
|
end
|
data/lib/microscope/mixin.rb
CHANGED
@@ -16,28 +16,39 @@ module Microscope
|
|
16
16
|
end
|
17
17
|
|
18
18
|
boolean_fields = model_columns.select { |c| c.type == :boolean }.map(&:name)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
scope "not_#{field}", lambda { where(field => false) }
|
23
|
-
end
|
19
|
+
boolean_fields.each do |field|
|
20
|
+
scope field, lambda { where(field => true) }
|
21
|
+
scope "not_#{field}", lambda { where(field => false) }
|
24
22
|
end
|
25
23
|
|
26
24
|
datetime_fields = model_columns.select { |c| c.type == :datetime }.map(&:name)
|
27
|
-
|
28
|
-
|
29
|
-
cropped_field = field.gsub(/_at$/, '')
|
25
|
+
datetime_fields.each do |field|
|
26
|
+
cropped_field = field.gsub(/_at$/, '')
|
30
27
|
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
scope "#{cropped_field}_before", lambda { |time| where("#{field} < ?", time) }
|
29
|
+
scope "#{cropped_field}_before_or_at", lambda { |time| where("#{field} <= ?", time) }
|
30
|
+
scope "#{cropped_field}_before_now", lambda { where("#{field} < ?", Time.now) }
|
34
31
|
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
scope "#{cropped_field}_after", lambda { |time| where("#{field} > ?", time) }
|
33
|
+
scope "#{cropped_field}_after_or_at", lambda { |time| where("#{field} >= ?", time) }
|
34
|
+
scope "#{cropped_field}_after_now", lambda { where("#{field} > ?", Time.now) }
|
38
35
|
|
39
|
-
|
40
|
-
|
36
|
+
scope "#{cropped_field}_between", lambda { |range| where(field => range) }
|
37
|
+
end
|
38
|
+
|
39
|
+
date_fields = model_columns.select { |c| c.type == :date }.map(&:name)
|
40
|
+
date_fields.each do |field|
|
41
|
+
cropped_field = field.gsub(/_on$/, '')
|
42
|
+
|
43
|
+
scope "#{cropped_field}_before", lambda { |time| where("#{field} < ?", time) }
|
44
|
+
scope "#{cropped_field}_before_or_on", lambda { |time| where("#{field} <= ?", time) }
|
45
|
+
scope "#{cropped_field}_before_today", lambda { where("#{field} < ?", Date.today) }
|
46
|
+
|
47
|
+
scope "#{cropped_field}_after", lambda { |time| where("#{field} > ?", time) }
|
48
|
+
scope "#{cropped_field}_after_or_at", lambda { |time| where("#{field} >= ?", time) }
|
49
|
+
scope "#{cropped_field}_after_today", lambda { where("#{field} > ?", Date.today) }
|
50
|
+
|
51
|
+
scope "#{cropped_field}_between", lambda { |range| where(field => range) }
|
41
52
|
end
|
42
53
|
end
|
43
54
|
end
|
data/lib/microscope/version.rb
CHANGED
@@ -143,4 +143,64 @@ describe Microscope::Mixin do
|
|
143
143
|
it { expect(Event.started_between(4.months.ago..2.months.ago).to_a).to eql [@event] }
|
144
144
|
end
|
145
145
|
end
|
146
|
+
|
147
|
+
describe 'Date scopes' do
|
148
|
+
subject { Event }
|
149
|
+
|
150
|
+
before do
|
151
|
+
run_migration do
|
152
|
+
create_table(:events, force: true) do |t|
|
153
|
+
t.date :started_on, default: false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
microscope 'Event'
|
158
|
+
end
|
159
|
+
|
160
|
+
describe 'before scope' do
|
161
|
+
before do
|
162
|
+
@event = Event.create(started_on: 2.months.ago)
|
163
|
+
Event.create(started_on: 1.month.from_now)
|
164
|
+
end
|
165
|
+
|
166
|
+
it { expect(Event.started_before(1.month.ago).to_a).to eql [@event] }
|
167
|
+
end
|
168
|
+
|
169
|
+
describe 'before_today scope' do
|
170
|
+
before do
|
171
|
+
@event = Event.create(started_on: 2.months.ago)
|
172
|
+
Event.create(started_on: 1.month.from_now)
|
173
|
+
end
|
174
|
+
|
175
|
+
it { expect(Event.started_before_today.to_a).to eql [@event] }
|
176
|
+
end
|
177
|
+
|
178
|
+
describe 'after scope' do
|
179
|
+
before do
|
180
|
+
@event = Event.create(started_on: 2.months.from_now)
|
181
|
+
Event.create(started_on: 1.month.ago)
|
182
|
+
end
|
183
|
+
|
184
|
+
it { expect(Event.started_after(1.month.from_now).to_a).to eql [@event] }
|
185
|
+
end
|
186
|
+
|
187
|
+
describe 'after_today scope' do
|
188
|
+
before do
|
189
|
+
@event = Event.create(started_on: 2.months.from_now)
|
190
|
+
Event.create(started_on: 1.month.ago)
|
191
|
+
end
|
192
|
+
|
193
|
+
it { expect(Event.started_after_today.to_a).to eql [@event] }
|
194
|
+
end
|
195
|
+
|
196
|
+
describe 'between scope' do
|
197
|
+
before do
|
198
|
+
Event.create(started_on: 1.month.ago)
|
199
|
+
@event = Event.create(started_on: 3.months.ago)
|
200
|
+
Event.create(started_on: 5.month.ago)
|
201
|
+
end
|
202
|
+
|
203
|
+
it { expect(Event.started_between(4.months.ago..2.months.ago).to_a).to eql [@event] }
|
204
|
+
end
|
205
|
+
end
|
146
206
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: microscope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -119,10 +119,13 @@ extra_rdoc_files: []
|
|
119
119
|
files:
|
120
120
|
- .gitignore
|
121
121
|
- .rspec
|
122
|
+
- .travis.yml
|
122
123
|
- Gemfile
|
123
124
|
- LICENSE.md
|
124
125
|
- README.md
|
125
126
|
- Rakefile
|
127
|
+
- gemfiles/Gemfile.activerecord-3.2.x
|
128
|
+
- gemfiles/Gemfile.activerecord-4.0
|
126
129
|
- lib/microscope.rb
|
127
130
|
- lib/microscope/mixin.rb
|
128
131
|
- lib/microscope/railtie.rb
|
@@ -147,7 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
150
|
version: '0'
|
148
151
|
segments:
|
149
152
|
- 0
|
150
|
-
hash:
|
153
|
+
hash: -4314982214745494353
|
151
154
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
155
|
none: false
|
153
156
|
requirements:
|
@@ -156,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
159
|
version: '0'
|
157
160
|
segments:
|
158
161
|
- 0
|
159
|
-
hash:
|
162
|
+
hash: -4314982214745494353
|
160
163
|
requirements: []
|
161
164
|
rubyforge_project:
|
162
165
|
rubygems_version: 1.8.23
|