periscope-mongoid 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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.
@@ -0,0 +1,142 @@
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
+ ## Installation
6
+
7
+ Periscope sits on top of your favorite ORM. Currently, the following ORMs are supported through individual gems extending Periscope:
8
+
9
+ * ActiveRecord ([periscope-activerecord](https://rubygems.org/gems/periscope-activerecord))
10
+ * MongoMapper ([periscope-mongo_mapper](https://rubygems.org/gems/periscope-mongo_mapper))
11
+ * Mongoid ([periscope-mongoid](https://rubygems.org/gems/periscope-mongoid))
12
+
13
+ Simply add the gem to your bundle and you're off!
14
+
15
+ ## The Problem
16
+
17
+ 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_.
18
+
19
+ 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:
20
+
21
+ ```ruby
22
+ def index
23
+ @articles = Article.scoped
24
+ @articles = @articles.published_after(params[:published_after]) if params.key?(:published_after)
25
+ @articles = @articles.published_before(params[:published_before]) if params.key?(:published_before)
26
+ end
27
+ ```
28
+
29
+ You can imagine how bad this would get if more than two scopes were involved.
30
+
31
+ ## The Solution
32
+
33
+ With Periscope, you can have this instead:
34
+
35
+ ```ruby
36
+ def index
37
+ @articles = Article.periscope(request.query_parameters)
38
+ end
39
+ ```
40
+
41
+ The `periscope` method will find keys in your params matching your scope names and chain your scopes for you.
42
+
43
+ **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.
44
+
45
+ ## But Wait!
46
+
47
+ "What if I don't want to make all my scopes publicly accessible?"
48
+
49
+ Within your model you can use the `scope_accessible` method to specify which scopes you want Periscope to honor.
50
+
51
+ ```ruby
52
+ class User < ActiveRecord::Base
53
+ scope :gender, lambda{|g| where(gender: g) }
54
+ scope :makes, lambda{|s| where('salary >= ?', s) }
55
+
56
+ scope_accessible :gender
57
+ end
58
+ ```
59
+
60
+ And in your controller:
61
+
62
+ ```ruby
63
+ class UsersController < ApplicationController
64
+ def index
65
+ @users = User.periscope(request.query_parameters)
66
+ end
67
+ end
68
+ ```
69
+
70
+ 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.
71
+
72
+ By default, all scopes are protected.
73
+
74
+ ## There's More!
75
+
76
+ ### Custom Parameter Parsing
77
+
78
+ 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.
79
+
80
+ 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.
81
+
82
+ ```ruby
83
+ class User < ActiveRecord::Base
84
+ scope :gender, lambda{|g| where(gender: g) }
85
+
86
+ scope_accessible :gender, parser: lambda{|g| [g.downcase] }
87
+ end
88
+ ```
89
+
90
+ ### On/Off Scopes
91
+
92
+ 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.
93
+
94
+ ```ruby
95
+ class User < ActiveRecord::Base
96
+ scope :male, where(gender: 'male')
97
+ scope :female, where(gender: 'female')
98
+
99
+ scope_accessible :male, :female, boolean: true
100
+ end
101
+ ```
102
+
103
+ ### Custom Method Names
104
+
105
+ 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.
106
+
107
+ ```ruby
108
+ class Project < ActiveRecord::Base
109
+ scope_accessible :begin, method: :begins_after
110
+ scope_accessible :end, method: :ends_before
111
+
112
+ def self.begins_after(date)
113
+ where('begins_at >= ?', date)
114
+ end
115
+
116
+ def self.ends_before(date)
117
+ where('ends_at <= ?', date)
118
+ end
119
+ end
120
+ ```
121
+
122
+ Alternatively, you can set `:prefix` and/or `:suffix` options, which will be applied to the query parameter name to determine the corresponding method name.
123
+
124
+ ```ruby
125
+ class Project < ActiveRecord::Base
126
+ scope_accessible :begin, :end, suffix: '_date'
127
+
128
+ def self.begin_date(date)
129
+ where('begins_at >= ?', date)
130
+ end
131
+
132
+ def self.end_date(date)
133
+ where('ends_at <= ?', date)
134
+ end
135
+ end
136
+ ```
137
+
138
+ ## This sucks. How can I make it better?
139
+
140
+ 1. Fork it.
141
+ 2. Make it better.
142
+ 3. Send me a pull request.
@@ -0,0 +1 @@
1
+ require 'periscope/adapters/mongoid'
@@ -0,0 +1,18 @@
1
+ require 'periscope'
2
+ require 'mongoid'
3
+
4
+ module Periscope
5
+ module Adapters
6
+ module Mongoid
7
+ include Periscope
8
+
9
+ private
10
+
11
+ def periscope_default_scope
12
+ scoped
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Mongoid::Document::ClassMethods.send(:include, Periscope::Adapters::Mongoid)
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'periscope-mongoid'
5
+ gem.version = '1.0.0'
6
+
7
+ gem.authors = ['Steve Richert']
8
+ gem.email = ['steve.richert@gmail.com']
9
+ gem.description = %(Push your Mongoid models' scopes up to the surface)
10
+ gem.summary = gem.description
11
+ gem.homepage = 'https://github.com/laserlemon/periscope'
12
+
13
+ gem.add_dependency 'mongoid', '~> 2.0'
14
+ gem.add_dependency 'periscope', '~> 1.0'
15
+
16
+ gem.add_development_dependency 'database_cleaner', '~> 0.8'
17
+ gem.add_development_dependency 'factory_girl', '>= 2', '< 4'
18
+ gem.add_development_dependency 'rake', '~> 0.9'
19
+ gem.add_development_dependency 'rspec', '~> 2.0'
20
+
21
+ gem.files = %w(
22
+ LICENSE
23
+ lib/periscope-mongoid.rb
24
+ lib/periscope/adapters/mongoid.rb
25
+ periscope-mongoid.gemspec
26
+ README.md
27
+ )
28
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: periscope-mongoid
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-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
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: '2.0'
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: database_cleaner
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.8'
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: '0.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: factory_girl
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '2'
70
+ - - <
71
+ - !ruby/object:Gem::Version
72
+ version: '4'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '2'
81
+ - - <
82
+ - !ruby/object:Gem::Version
83
+ version: '4'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '0.9'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: '0.9'
100
+ - !ruby/object:Gem::Dependency
101
+ name: rspec
102
+ requirement: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ~>
106
+ - !ruby/object:Gem::Version
107
+ version: '2.0'
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: '2.0'
116
+ description: Push your Mongoid models' scopes up to the surface
117
+ email:
118
+ - steve.richert@gmail.com
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - LICENSE
124
+ - lib/periscope-mongoid.rb
125
+ - lib/periscope/adapters/mongoid.rb
126
+ - periscope-mongoid.gemspec
127
+ - README.md
128
+ homepage: https://github.com/laserlemon/periscope
129
+ licenses: []
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ segments:
141
+ - 0
142
+ hash: 1760540227657446513
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ segments:
150
+ - 0
151
+ hash: 1760540227657446513
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.24
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Push your Mongoid models' scopes up to the surface
158
+ test_files: []