periscope-mongo_mapper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +132 -0
- data/lib/periscope/adapters/mongo_mapper.rb +22 -0
- data/lib/periscope-mongo_mapper.rb +1 -0
- data/periscope-mongo_mapper.gemspec +29 -0
- metadata +174 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2011 Steve Richert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Periscope [![Build Status](https://secure.travis-ci.org/laserlemon/periscope.png)](http://travis-ci.org/laserlemon/periscope) [![Dependency Status](https://gemnasium.com/laserlemon/periscope.png)](https://gemnasium.com/laserlemon/periscope)
|
2
|
+
|
3
|
+
Periscope provides a simple way to chain scopes on your models and to open those scopes up to your users.
|
4
|
+
|
5
|
+
## The Problem
|
6
|
+
|
7
|
+
More often than not, the index action in a RESTful Rails controller is expected to do a lot more than simply return all the records for a given model. We ask it to do all sorts of stuff like filtering, sorting and paginating results. Of course, this is typically done using _scopes_.
|
8
|
+
|
9
|
+
But it can get ugly building long, complicated chains of scopes in the controller, especially when you try to give your users control over the scoping. Picture this:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
def index
|
13
|
+
@articles = Article.scoped
|
14
|
+
@articles = @articles.published_after(params[:published_after]) if params.key?(:published_after)
|
15
|
+
@articles = @articles.published_before(params[:published_before]) if params.key?(:published_before)
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
You can imagine how bad this would get if more than two scopes were involved.
|
20
|
+
|
21
|
+
## The Solution
|
22
|
+
|
23
|
+
With Periscope, you can have this instead:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
def index
|
27
|
+
@articles = Article.periscope(request.query_parameters)
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
The `periscope` method will find keys in your params matching your scope names and chain your scopes for you.
|
32
|
+
|
33
|
+
**Note:** We're using `request.query_parameters` so that we can exclude our controller and action params. `request.query_parameters` will just return the params that appear in the query string.
|
34
|
+
|
35
|
+
## But Wait!
|
36
|
+
|
37
|
+
"What if I don't want to make all my scopes publicly accessible?"
|
38
|
+
|
39
|
+
Within your model you can use the `scope_accessible` method to specify which scopes you want Periscope to honor.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
scope :gender, lambda{|g| where(gender: g) }
|
44
|
+
scope :makes, lambda{|s| where('salary >= ?', s) }
|
45
|
+
|
46
|
+
scope_accessible :gender
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
And in your controller:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class UsersController < ApplicationController
|
54
|
+
def index
|
55
|
+
@users = User.periscope(request.query_parameters)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Requests to `/users?gender=male` will filter results to only male users. But a request to `/users?makes=1000000` will return all users, silently ignoring the protected scope.
|
61
|
+
|
62
|
+
By default, all scopes are protected.
|
63
|
+
|
64
|
+
## There's More!
|
65
|
+
|
66
|
+
### Custom Parameter Parsing
|
67
|
+
|
68
|
+
Sometimes the values you get from the query parameters aren't quite good enough. They may need to be massaged in order to work with your scopes and class methods. In those cases, you can provide a `:parser` option to your `scope_accessible` method.
|
69
|
+
|
70
|
+
Parsers must respond to the `call` method, receiving the raw query parameter and returning an array of arguments to pass to the scope or class method.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class User < ActiveRecord::Base
|
74
|
+
scope :gender, lambda{|g| where(gender: g) }
|
75
|
+
|
76
|
+
scope_accessible :gender, parser: lambda{|g| [g.downcase] }
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
### On/Off Scopes
|
81
|
+
|
82
|
+
But not all scopes accept arguments. For scopes that you want to toggle on or off, you can set a `:boolean => true` option. Whenever the received parameter is truthy, the scope will be applied. Otherwise, it will be skipped.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class User < ActiveRecord::Base
|
86
|
+
scope :male, where(gender: 'male')
|
87
|
+
scope :female, where(gender: 'female')
|
88
|
+
|
89
|
+
scope_accessible :male, :female, boolean: true
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
### Custom Method Names
|
94
|
+
|
95
|
+
Sometimes the query parameters you want to open up to your users may collide with existing method names or reserved Ruby words. In order to avoid collision, you can set a `:method` option to specify what method to use for a query parameter.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class Project < ActiveRecord::Base
|
99
|
+
scope_accessible :begin, method: :begins_after
|
100
|
+
scope_accessible :end, method: :ends_before
|
101
|
+
|
102
|
+
def self.begins_after(date)
|
103
|
+
where('begins_at >= ?', date)
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.ends_before(date)
|
107
|
+
where('ends_at <= ?', date)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
Alternatively, you can set `:prefix` and/or `:suffix` options, which will be applied to the query parameter name to determine the corresponding method name.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class Project < ActiveRecord::Base
|
116
|
+
scope_accessible :begin, :end, suffix: '_date'
|
117
|
+
|
118
|
+
def self.begin_date(date)
|
119
|
+
where('begins_at >= ?', date)
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.end_date(date)
|
123
|
+
where('ends_at <= ?', date)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
## This sucks. How can I make it better?
|
129
|
+
|
130
|
+
1. Fork it.
|
131
|
+
2. Make it better.
|
132
|
+
3. Send me a pull request.
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'periscope'
|
2
|
+
require 'mongo_mapper'
|
3
|
+
|
4
|
+
module Periscope
|
5
|
+
module Adapters
|
6
|
+
module MongoMapper
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
include Periscope
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def periscope_default_scope
|
15
|
+
where
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
MongoMapper::Document.plugin(Periscope::Adapters::MongoMapper)
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'periscope/adapters/mongo_mapper'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'periscope-mongo_mapper'
|
5
|
+
gem.version = '1.0.0'
|
6
|
+
|
7
|
+
gem.authors = ['Steve Richert']
|
8
|
+
gem.email = ['steve.richert@gmail.com']
|
9
|
+
gem.description = %(Push your MongoMapper models' scopes up to the surface)
|
10
|
+
gem.summary = gem.description
|
11
|
+
gem.homepage = 'https://github.com/laserlemon/periscope'
|
12
|
+
|
13
|
+
gem.add_dependency 'mongo_mapper', '~> 0.11'
|
14
|
+
gem.add_dependency 'periscope', '~> 1.0'
|
15
|
+
|
16
|
+
gem.add_development_dependency 'bson_ext', '~> 1.4'
|
17
|
+
gem.add_development_dependency 'database_cleaner', '~> 0.8'
|
18
|
+
gem.add_development_dependency 'factory_girl', '>= 2', '< 4'
|
19
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
20
|
+
gem.add_development_dependency 'rspec', '~> 2.0'
|
21
|
+
|
22
|
+
gem.files = %w(
|
23
|
+
LICENSE
|
24
|
+
lib/periscope-mongo_mapper.rb
|
25
|
+
lib/periscope/adapters/mongo_mapper.rb
|
26
|
+
periscope-mongo_mapper.gemspec
|
27
|
+
README.md
|
28
|
+
)
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: periscope-mongo_mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steve Richert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongo_mapper
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.11'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.11'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: periscope
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bson_ext
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.4'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.4'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: database_cleaner
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.8'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.8'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: factory_girl
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2'
|
86
|
+
- - <
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '4'
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2'
|
97
|
+
- - <
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '4'
|
100
|
+
- !ruby/object:Gem::Dependency
|
101
|
+
name: rake
|
102
|
+
requirement: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0.9'
|
108
|
+
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ~>
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0.9'
|
116
|
+
- !ruby/object:Gem::Dependency
|
117
|
+
name: rspec
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ~>
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '2.0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ~>
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '2.0'
|
132
|
+
description: Push your MongoMapper models' scopes up to the surface
|
133
|
+
email:
|
134
|
+
- steve.richert@gmail.com
|
135
|
+
executables: []
|
136
|
+
extensions: []
|
137
|
+
extra_rdoc_files: []
|
138
|
+
files:
|
139
|
+
- LICENSE
|
140
|
+
- lib/periscope-mongo_mapper.rb
|
141
|
+
- lib/periscope/adapters/mongo_mapper.rb
|
142
|
+
- periscope-mongo_mapper.gemspec
|
143
|
+
- README.md
|
144
|
+
homepage: https://github.com/laserlemon/periscope
|
145
|
+
licenses: []
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
152
|
+
requirements:
|
153
|
+
- - ! '>='
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
hash: -4186296594754612487
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
none: false
|
161
|
+
requirements:
|
162
|
+
- - ! '>='
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
segments:
|
166
|
+
- 0
|
167
|
+
hash: -4186296594754612487
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 1.8.24
|
171
|
+
signing_key:
|
172
|
+
specification_version: 3
|
173
|
+
summary: Push your MongoMapper models' scopes up to the surface
|
174
|
+
test_files: []
|