autoscope 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +12 -0
  5. data/Guardfile +30 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +29 -0
  8. data/Rakefile +27 -0
  9. data/autoscope.gemspec +27 -0
  10. data/lib/autoscope.rb +10 -0
  11. data/lib/autoscope/active_record_methods.rb +236 -0
  12. data/lib/autoscope/version.rb +3 -0
  13. data/spec/dummy/README.rdoc +261 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  18. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  19. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  20. data/spec/dummy/app/mailers/.gitkeep +0 -0
  21. data/spec/dummy/app/models/.gitkeep +0 -0
  22. data/spec/dummy/app/models/post.rb +2 -0
  23. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/config.ru +4 -0
  25. data/spec/dummy/config/application.rb +63 -0
  26. data/spec/dummy/config/boot.rb +10 -0
  27. data/spec/dummy/config/database.yml +22 -0
  28. data/spec/dummy/config/database.yml.default +111 -0
  29. data/spec/dummy/config/environment.rb +5 -0
  30. data/spec/dummy/config/environments/development.rb +35 -0
  31. data/spec/dummy/config/environments/production.rb +67 -0
  32. data/spec/dummy/config/environments/test.rb +33 -0
  33. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/spec/dummy/config/initializers/inflections.rb +15 -0
  35. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  36. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  37. data/spec/dummy/config/initializers/session_store.rb +8 -0
  38. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  39. data/spec/dummy/config/konfig/application.rb +16 -0
  40. data/spec/dummy/config/konfig/development.rb +10 -0
  41. data/spec/dummy/config/konfig/production.rb +3 -0
  42. data/spec/dummy/config/konfig/test.rb +3 -0
  43. data/spec/dummy/config/konfig/test_prod.rb +3 -0
  44. data/spec/dummy/config/locales/en.yml +5 -0
  45. data/spec/dummy/config/routes.rb +3 -0
  46. data/spec/dummy/data/log/dummy/development.log +113 -0
  47. data/spec/dummy/data/log/dummy/test.log +927 -0
  48. data/spec/dummy/db/.gitkeep +0 -0
  49. data/spec/dummy/db/test.sqlite3 +0 -0
  50. data/spec/dummy/lib/assets/.gitkeep +0 -0
  51. data/spec/dummy/log/.gitkeep +0 -0
  52. data/spec/dummy/log/development.log +34 -0
  53. data/spec/dummy/log/test.log +2964 -0
  54. data/spec/dummy/public/404.html +26 -0
  55. data/spec/dummy/public/422.html +26 -0
  56. data/spec/dummy/public/500.html +25 -0
  57. data/spec/dummy/public/favicon.ico +0 -0
  58. data/spec/dummy/script/rails +6 -0
  59. data/spec/lib/autoscope/activerecord_spec.rb +151 -0
  60. data/spec/spec_helper.rb +27 -0
  61. metadata +221 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ module Autoscope
4
+
5
+ describe ActiveRecord do
6
+
7
+ before(:all) do
8
+
9
+ Object.const_set('Post', Class.new(ActiveRecord::Base))
10
+
11
+ Post.connection.create_table(:posts, force: true) do |t|
12
+ t.string :title
13
+ t.text :body
14
+ t.integer :user_id
15
+ t.string :protected_field
16
+ t.string :private_field
17
+ t.timestamps
18
+ end
19
+
20
+ Post.class_eval do
21
+
22
+ scope :user_id_scope, lambda { |user_id|
23
+ where(user_id: user_id)
24
+ }
25
+ scope :vararg_scope, lambda { |*ids|
26
+ where(['id IN (?)', args.flatten])
27
+ }
28
+ scope :two_param_scope, lambda { |id1,id2|
29
+ where(['id IN (?)', [id1, id2]])
30
+ }
31
+ scope :no_param_scope, -> { where('x = 1') }
32
+ scope :empty_lambda_scope, lambda{ where(['date > ?', Date.today])}
33
+ scope :optional_arg_scope, lambda{|a, b = 5|}
34
+
35
+ protected_scope :nonsense, lambda{|id, test| where(user_id: id)}
36
+
37
+ end
38
+
39
+ ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
40
+ t.string :name
41
+ t.date :bday
42
+ t.string :type
43
+ t.timestamps
44
+ end
45
+
46
+ Object.const_set('User', Class.new(ActiveRecord::Base))
47
+
48
+ User.class_eval do
49
+
50
+ scope :by_name, lambda { |name|
51
+ where(name: name)
52
+ }
53
+
54
+ end
55
+
56
+ Object.const_set('Admin', Class.new(User))
57
+
58
+ end
59
+
60
+ context '.add_scopes' do
61
+
62
+ it 'adds all types of scopes to the supplied collection' do
63
+
64
+ scope = Post.all
65
+
66
+ scope.expects(:user_id_scope)
67
+ .with('1')
68
+ .returns(scope)
69
+
70
+ scope.expects(:two_param_scope)
71
+ .with('1', '2')
72
+ .returns(scope)
73
+
74
+ scope.expects(:vararg_scope)
75
+ .with('1', '2', '3')
76
+ .returns(scope)
77
+
78
+ scope.expects(:optional_arg_scope)
79
+ .with('req')
80
+ .returns(scope)
81
+
82
+ Post.add_scopes(
83
+ {
84
+ user_id_scope: { user_id: '1' },
85
+ two_param_scope: {
86
+ id1: '1',
87
+ id2: '2'
88
+ },
89
+ vararg_scope: { ids: %w{1 2 3} },
90
+ optional_arg_scope: { a: 'req' }
91
+ },
92
+ scope
93
+ )
94
+
95
+ end
96
+
97
+ context 'Type filter' do
98
+
99
+ it 'applies a filter on the type when a type key is passed in' do
100
+ scope = User.add_scopes(type: 'Admin')
101
+ expect(scope.where_values_hash[:type]).to eql(['Admin'])
102
+ end
103
+
104
+ it 'handles invalid class names' do
105
+ expect {
106
+ scope = User.add_scopes(type: 'InvalidClass')
107
+ }.not_to raise_error
108
+ end
109
+
110
+ it 'merges with the scope that was passed in' do
111
+ scope = User.add_scopes({ type: 'Admin' }, User.where(name: 'Dan'))
112
+
113
+ where_values = scope.where_values_hash.with_indifferent_access
114
+
115
+ expect(where_values["name"]).to eql('Dan')
116
+ expect(where_values["type"]).to eql(['Admin'])
117
+ end
118
+
119
+ it 'does not apply a scope unless a descendant class is supplied' do
120
+ original_scope = User.where(name: 'Dan')
121
+ scope = User.add_scopes({ type: 'Post' }, original_scope)
122
+
123
+ # shouldn't do anything
124
+ expect(scope).to eql(original_scope)
125
+ end
126
+
127
+ end
128
+
129
+
130
+
131
+ context 'Pagination' do
132
+
133
+ it 'adds pagination' do
134
+
135
+ scope = Post.all
136
+
137
+ scope.expects(:paginate)
138
+ .with(page: 3, per_page: 20)
139
+ .returns(scope)
140
+
141
+ Post.add_scopes({ page: 3, per_page: 20 }, scope)
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
149
+ end
150
+
151
+ end
@@ -0,0 +1,27 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ ENV["RAILS_ENV"] ||= 'test'
5
+ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
6
+
7
+ require 'autoscope'
8
+
9
+ require 'rspec/rails'
10
+ require 'rails/engine'
11
+ require 'mocha/setup'
12
+
13
+ # Requires supporting files with custom matchers and macros, etc,
14
+ # in ./support/ and its subdirectories.
15
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
16
+
17
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
18
+
19
+ RSpec.configure do |config|
20
+ config.mock_with :mocha
21
+
22
+ config.treat_symbols_as_metadata_keys_with_true_values = true
23
+ config.run_all_when_everything_filtered = true
24
+
25
+ config.filter_run focus: true
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoscope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Langevin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: will_paginate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Automatically apply scopes to a model
84
+ email:
85
+ - dan.langevin@lifebooker.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - autoscope.gemspec
98
+ - lib/autoscope.rb
99
+ - lib/autoscope/active_record_methods.rb
100
+ - lib/autoscope/version.rb
101
+ - spec/dummy/README.rdoc
102
+ - spec/dummy/Rakefile
103
+ - spec/dummy/app/assets/javascripts/application.js
104
+ - spec/dummy/app/assets/stylesheets/application.css
105
+ - spec/dummy/app/controllers/application_controller.rb
106
+ - spec/dummy/app/helpers/application_helper.rb
107
+ - spec/dummy/app/helpers/posts_helper.rb
108
+ - spec/dummy/app/mailers/.gitkeep
109
+ - spec/dummy/app/models/.gitkeep
110
+ - spec/dummy/app/models/post.rb
111
+ - spec/dummy/app/views/layouts/application.html.erb
112
+ - spec/dummy/config.ru
113
+ - spec/dummy/config/application.rb
114
+ - spec/dummy/config/boot.rb
115
+ - spec/dummy/config/database.yml
116
+ - spec/dummy/config/database.yml.default
117
+ - spec/dummy/config/environment.rb
118
+ - spec/dummy/config/environments/development.rb
119
+ - spec/dummy/config/environments/production.rb
120
+ - spec/dummy/config/environments/test.rb
121
+ - spec/dummy/config/initializers/backtrace_silencers.rb
122
+ - spec/dummy/config/initializers/inflections.rb
123
+ - spec/dummy/config/initializers/mime_types.rb
124
+ - spec/dummy/config/initializers/secret_token.rb
125
+ - spec/dummy/config/initializers/session_store.rb
126
+ - spec/dummy/config/initializers/wrap_parameters.rb
127
+ - spec/dummy/config/konfig/application.rb
128
+ - spec/dummy/config/konfig/development.rb
129
+ - spec/dummy/config/konfig/production.rb
130
+ - spec/dummy/config/konfig/test.rb
131
+ - spec/dummy/config/konfig/test_prod.rb
132
+ - spec/dummy/config/locales/en.yml
133
+ - spec/dummy/config/routes.rb
134
+ - spec/dummy/data/log/dummy/development.log
135
+ - spec/dummy/data/log/dummy/test.log
136
+ - spec/dummy/db/.gitkeep
137
+ - spec/dummy/db/test.sqlite3
138
+ - spec/dummy/lib/assets/.gitkeep
139
+ - spec/dummy/log/.gitkeep
140
+ - spec/dummy/log/development.log
141
+ - spec/dummy/log/test.log
142
+ - spec/dummy/public/404.html
143
+ - spec/dummy/public/422.html
144
+ - spec/dummy/public/500.html
145
+ - spec/dummy/public/favicon.ico
146
+ - spec/dummy/script/rails
147
+ - spec/lib/autoscope/activerecord_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: ''
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.0.3
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Automatically apply scopes to a model
173
+ test_files:
174
+ - spec/dummy/README.rdoc
175
+ - spec/dummy/Rakefile
176
+ - spec/dummy/app/assets/javascripts/application.js
177
+ - spec/dummy/app/assets/stylesheets/application.css
178
+ - spec/dummy/app/controllers/application_controller.rb
179
+ - spec/dummy/app/helpers/application_helper.rb
180
+ - spec/dummy/app/helpers/posts_helper.rb
181
+ - spec/dummy/app/mailers/.gitkeep
182
+ - spec/dummy/app/models/.gitkeep
183
+ - spec/dummy/app/models/post.rb
184
+ - spec/dummy/app/views/layouts/application.html.erb
185
+ - spec/dummy/config.ru
186
+ - spec/dummy/config/application.rb
187
+ - spec/dummy/config/boot.rb
188
+ - spec/dummy/config/database.yml
189
+ - spec/dummy/config/database.yml.default
190
+ - spec/dummy/config/environment.rb
191
+ - spec/dummy/config/environments/development.rb
192
+ - spec/dummy/config/environments/production.rb
193
+ - spec/dummy/config/environments/test.rb
194
+ - spec/dummy/config/initializers/backtrace_silencers.rb
195
+ - spec/dummy/config/initializers/inflections.rb
196
+ - spec/dummy/config/initializers/mime_types.rb
197
+ - spec/dummy/config/initializers/secret_token.rb
198
+ - spec/dummy/config/initializers/session_store.rb
199
+ - spec/dummy/config/initializers/wrap_parameters.rb
200
+ - spec/dummy/config/konfig/application.rb
201
+ - spec/dummy/config/konfig/development.rb
202
+ - spec/dummy/config/konfig/production.rb
203
+ - spec/dummy/config/konfig/test.rb
204
+ - spec/dummy/config/konfig/test_prod.rb
205
+ - spec/dummy/config/locales/en.yml
206
+ - spec/dummy/config/routes.rb
207
+ - spec/dummy/data/log/dummy/development.log
208
+ - spec/dummy/data/log/dummy/test.log
209
+ - spec/dummy/db/.gitkeep
210
+ - spec/dummy/db/test.sqlite3
211
+ - spec/dummy/lib/assets/.gitkeep
212
+ - spec/dummy/log/.gitkeep
213
+ - spec/dummy/log/development.log
214
+ - spec/dummy/log/test.log
215
+ - spec/dummy/public/404.html
216
+ - spec/dummy/public/422.html
217
+ - spec/dummy/public/500.html
218
+ - spec/dummy/public/favicon.ico
219
+ - spec/dummy/script/rails
220
+ - spec/lib/autoscope/activerecord_spec.rb
221
+ - spec/spec_helper.rb