microscope 0.0.1 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -41,10 +41,30 @@ Event.expired_before_now
41
41
  Event.expired_after_or_at(2.months.from_now)
42
42
  # SELECT * FROM `events` where `events`.`expired_at` >= '2013-09-05 15:43:42'
43
43
 
44
- Event.expired_between(2.months.go..1.month.from_now)
44
+ Event.expired_between(2.months.ago..1.month.from_now)
45
45
  # SELECT * FROM `events` where `events`.`expired_at` BETWEEN '2013-05-05 15:43:42' AND '2013-08-05 15:43:42'
46
46
  ```
47
47
 
48
+ ### Options
49
+
50
+ You can use a few options when calling `acts_as_microscope`:
51
+
52
+ ```ruby
53
+ class Event < ActiveRecord::Base
54
+ acts_as_microscope, only: [:created_at]
55
+ end
56
+
57
+ class User < ActiveRecord::Base
58
+ acts_as_microscope, except: [:activated_at]
59
+ end
60
+
61
+ Event.created_before(2.months.ago) # works!
62
+ Event.updated_before(2.months.ago) # NoMethodError
63
+
64
+ User.created_before(2.months.ago) # works!
65
+ User.activated_before(2.months.ago) # NoMethodError
66
+ ```
67
+
48
68
  ## License
49
69
 
50
70
  `Microscope` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/microscope/blob/master/LICENSE.md) file.
@@ -8,9 +8,14 @@ require "microscope/mixin"
8
8
  module Microscope
9
9
  def self.inject_into_active_record
10
10
  @inject_into_active_record ||= Proc.new do
11
- def self.acts_as_microscope
11
+ def self.acts_as_microscope(options = {})
12
+ self.microscope_options = options
12
13
  self.send :include, Microscope::Mixin
13
14
  end
15
+
16
+ def self.microscope_options=(options)
17
+ @microscope_options = options
18
+ end
14
19
  end
15
20
  end
16
21
  end
@@ -8,7 +8,12 @@ module Microscope
8
8
 
9
9
  module ClassMethods
10
10
  def define_microscrope_scopes
11
- model_columns = self.columns.dup
11
+ except = @microscope_options[:except] || []
12
+ model_columns = self.columns.dup.reject { |c| except.include?(c.name.to_sym) }
13
+
14
+ if only = @microscope_options[:only]
15
+ model_columns = model_columns.select { |c| only.include?(c.name.to_sym) }
16
+ end
12
17
 
13
18
  boolean_fields = model_columns.select { |c| c.type == :boolean }.map(&:name)
14
19
  class_eval do
@@ -1,3 +1,3 @@
1
1
  module Microscope
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
@@ -1,6 +1,59 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Microscope::Mixin do
4
+ describe :acts_as_microscope do
5
+ subject { User }
6
+
7
+ before do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.boolean :active, default: false
11
+ t.boolean :admin, default: false
12
+ t.boolean :moderator, default: false
13
+ end
14
+ end
15
+ end
16
+
17
+ describe :except do
18
+ before do
19
+ microscope 'User', except: [:admin]
20
+ end
21
+
22
+ it { should respond_to :active }
23
+ it { should respond_to :not_active }
24
+ it { should respond_to :moderator }
25
+ it { should respond_to :not_moderator }
26
+ it { should_not respond_to :admin }
27
+ it { should_not respond_to :not_admin }
28
+ end
29
+
30
+ describe :only do
31
+ before do
32
+ microscope 'User', only: [:admin]
33
+ end
34
+
35
+ it { should_not respond_to :active }
36
+ it { should_not respond_to :not_active }
37
+ it { should_not respond_to :moderator }
38
+ it { should_not respond_to :not_moderator }
39
+ it { should respond_to :admin }
40
+ it { should respond_to :not_admin }
41
+ end
42
+
43
+ describe 'except and only' do
44
+ before do
45
+ microscope 'User', only: [:admin], except: [:active]
46
+ end
47
+
48
+ it { should_not respond_to :active }
49
+ it { should_not respond_to :not_active }
50
+ it { should_not respond_to :moderator }
51
+ it { should_not respond_to :not_moderator }
52
+ it { should respond_to :admin }
53
+ it { should respond_to :not_admin }
54
+ end
55
+ end
56
+
4
57
  describe 'Boolean scopes' do
5
58
  subject { User }
6
59
 
@@ -1,8 +1,8 @@
1
1
  module ModelMacros
2
2
  # Create a new microscope model
3
- def microscope(klass_name, &block)
3
+ def microscope(klass_name, options = {}, &block)
4
4
  spawn_model klass_name, ActiveRecord::Base do
5
- acts_as_microscope
5
+ acts_as_microscope options
6
6
  instance_exec(&block) if block
7
7
  end
8
8
  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.0.1
4
+ version: '0.1'
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-07-05 00:00:00.000000000 Z
13
+ date: 2013-07-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -147,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  segments:
149
149
  - 0
150
- hash: 533199632649490219
150
+ hash: -1603212847534960096
151
151
  required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  none: false
153
153
  requirements:
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  version: '0'
157
157
  segments:
158
158
  - 0
159
- hash: 533199632649490219
159
+ hash: -1603212847534960096
160
160
  requirements: []
161
161
  rubyforge_project:
162
162
  rubygems_version: 1.8.23