expose_query 0.0.3 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/.travis.yml +1 -0
- data/Gemfile +2 -1
- data/README.md +17 -1
- data/expose_query.gemspec +1 -0
- data/lib/expose_query/active_record_query_strategy.rb +1 -1
- data/lib/expose_query/controller_dsl.rb +35 -5
- data/lib/expose_query/version.rb +1 -1
- data/spec/expose_query/controller_dsl_spec.rb +43 -23
- data/spec/fixtures/bird.rb +3 -0
- data/spec/fixtures/birds.yml +6 -0
- data/spec/fixtures/controllers/with_except_parameter_controller.rb +19 -0
- data/spec/fixtures/controllers/with_only_parameter_controller.rb +19 -0
- data/spec/fixtures/dummy_query.rb +8 -0
- data/spec/fixtures/new_dummy_query.rb +8 -0
- data/spec/fixtures/schema.rb +10 -0
- data/spec/spec_helper.rb +21 -2
- metadata +31 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2990d8edf4bcc0b2bb48032c1abfd2a0db7dbb82
|
4
|
+
data.tar.gz: a744527beb953afb61684edb4a8e4e714a24556a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91aaeff699c20c1c02e4445287d8f43b1eb215a55b7ad39a8e365e8fe78c0d9f736d99574a07df9f264b2c86961317f85911b2d60312a8b383f24b1332c229b4
|
7
|
+
data.tar.gz: ae5ef76b24b63ee1440dd47f9977cdcdc6d9c808fb17a5e831cef0a65d37d0ef4a881afaff690e296eb94e4e82e11f9b64eb432071841c77a7d94ebae3a223af
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ And then execute:
|
|
17
17
|
|
18
18
|
## Usage
|
19
19
|
|
20
|
-
|
20
|
+
Include module to controller:
|
21
21
|
|
22
22
|
```
|
23
23
|
class PhotosController < ApplicationController
|
@@ -37,6 +37,22 @@ And define query class:
|
|
37
37
|
end
|
38
38
|
```
|
39
39
|
|
40
|
+
## Advanced parameters
|
41
|
+
|
42
|
+
Allow define query objects for specific expose:
|
43
|
+
|
44
|
+
```
|
45
|
+
class PhotosController < ApplicationController
|
46
|
+
include ExposeQuery::ControllerDsl
|
47
|
+
expose(:photos)
|
48
|
+
expose(:recent_photos, model: :photo)
|
49
|
+
|
50
|
+
expose_query PhotoQuery, PaginationQuery, only: :photos
|
51
|
+
expose_query RecentPhotosQuery, only: :recent_photos
|
52
|
+
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
40
56
|
|
41
57
|
## Contributing
|
42
58
|
|
data/expose_query.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_runtime_dependency 'decent_exposure', '~> 2.0'
|
21
21
|
|
22
22
|
spec.add_development_dependency 'rspec'
|
23
|
+
spec.add_development_dependency 'pry'
|
23
24
|
spec.add_development_dependency 'rspec-rails'
|
24
25
|
spec.add_development_dependency 'actionpack', '>= 3.1.0'
|
25
26
|
spec.add_development_dependency 'activesupport', '>= 3.1.0'
|
@@ -5,17 +5,47 @@ module ExposeQuery
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
|
8
|
-
class_attribute :
|
8
|
+
class_attribute :query_objects
|
9
9
|
|
10
|
-
def self.expose_query(*
|
11
|
-
self.
|
10
|
+
def self.expose_query(*attrs)
|
11
|
+
self.query_objects ||= []
|
12
|
+
options = attrs.extract_options!
|
13
|
+
filter_classes = attrs.map do |class_filter|
|
14
|
+
OpenStruct.new(query_class: class_filter,
|
15
|
+
only: Array.wrap(options[:only]).compact,
|
16
|
+
except: Array.wrap(options[:except]).compact)
|
17
|
+
end
|
18
|
+
self.query_objects += Array.wrap(filter_classes)
|
12
19
|
end
|
13
20
|
|
21
|
+
def self.query_classes
|
22
|
+
self.query_objects.map(&:query_class)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def apply_filters(source_scope, expose_name = nil)
|
28
|
+
query_objects ? apply_objects_for(source_scope, expose_name) : source_scope
|
14
29
|
end
|
15
30
|
|
16
|
-
|
17
|
-
|
31
|
+
private
|
32
|
+
|
33
|
+
def apply_objects_for(source_scope, expose_name)
|
34
|
+
query_objects.reduce(source_scope) do |scope, filter_object|
|
35
|
+
if filter_object_allowed_for?(filter_object, expose_name.to_sym)
|
36
|
+
filter_object.query_class.new(self).apply(scope)
|
37
|
+
else
|
38
|
+
scope
|
39
|
+
end
|
40
|
+
end
|
18
41
|
end
|
19
42
|
|
43
|
+
def filter_object_allowed_for?(object, expose_name)
|
44
|
+
if object.only.blank?
|
45
|
+
!object.except.include?(expose_name)
|
46
|
+
else
|
47
|
+
object.only.include?(expose_name)
|
48
|
+
end
|
49
|
+
end
|
20
50
|
end
|
21
51
|
end
|
data/lib/expose_query/version.rb
CHANGED
@@ -1,25 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require '
|
3
|
-
require 'action_controller'
|
2
|
+
require 'expose_query'
|
4
3
|
|
5
|
-
class DummyQuery < ExposeQuery::BaseQuery
|
6
|
-
|
7
|
-
def apply(source_scope)
|
8
|
-
source_scope
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
class DummyController < ActionController::Base
|
14
|
-
expose(:bird) #{ 'Bird' }
|
15
|
-
expose_query DummyQuery
|
16
|
-
|
17
|
-
end
|
18
4
|
|
19
5
|
describe ExposeQuery::ControllerDsl do
|
20
6
|
|
21
7
|
describe '.expose_query' do
|
22
|
-
subject {
|
8
|
+
subject { Controllers::WithOnlyParameterController }
|
23
9
|
|
24
10
|
it { should respond_to(:expose_query) }
|
25
11
|
|
@@ -28,21 +14,55 @@ describe ExposeQuery::ControllerDsl do
|
|
28
14
|
end
|
29
15
|
|
30
16
|
describe '.query_classes' do
|
31
|
-
subject {
|
17
|
+
subject { Controllers::WithOnlyParameterController.query_classes }
|
32
18
|
|
33
19
|
it { is_expected.to include(DummyQuery) }
|
34
20
|
|
21
|
+
it { is_expected.to include(NewDummyQuery) }
|
22
|
+
|
35
23
|
end
|
36
24
|
|
37
|
-
describe '' do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
25
|
+
describe 'applying queries' do
|
26
|
+
before do
|
27
|
+
allow(controller).to receive(:request) do
|
28
|
+
double('request', post?: false, put?: false, patch?: false)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when expose_query pass only parameter' do
|
34
|
+
let(:controller) { Controllers::WithOnlyParameterController.new }
|
35
|
+
|
36
|
+
it 'apply filters only one expose' do
|
37
|
+
expect_any_instance_of(DummyQuery).to receive(:apply)
|
38
|
+
expect_any_instance_of(NewDummyQuery).not_to receive(:apply)
|
39
|
+
controller.birds
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'apply filter for only expose' do
|
43
|
+
expect_any_instance_of(DummyQuery).to receive(:apply)
|
44
|
+
expect_any_instance_of(NewDummyQuery).to receive(:apply)
|
45
|
+
controller.another_birds
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when expose_query pass except parameter' do
|
51
|
+
let(:controller) { Controllers::WithExceptParameterController.new}
|
42
52
|
|
43
|
-
|
53
|
+
it 'apply query for birds' do
|
54
|
+
expect_any_instance_of(DummyQuery).to receive(:apply)
|
55
|
+
expect_any_instance_of(NewDummyQuery).to receive(:apply)
|
56
|
+
controller.birds
|
57
|
+
end
|
44
58
|
|
59
|
+
it 'apply with except for another_birds' do
|
60
|
+
expect_any_instance_of(DummyQuery).to receive(:apply)
|
61
|
+
expect_any_instance_of(NewDummyQuery).not_to receive(:apply)
|
62
|
+
controller.another_birds
|
63
|
+
end
|
45
64
|
|
65
|
+
end
|
46
66
|
|
47
67
|
end
|
48
68
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'decent_exposure/expose'
|
3
|
+
|
4
|
+
class Controllers::WithExceptParameterController < ActionController::Base
|
5
|
+
expose(:birds)
|
6
|
+
expose(:another_birds, model: :bird)
|
7
|
+
|
8
|
+
expose_query DummyQuery
|
9
|
+
expose_query NewDummyQuery, except: :another_birds
|
10
|
+
|
11
|
+
decent_configuration do
|
12
|
+
strategy DecentExposure::ActiveRecordStrategy
|
13
|
+
end
|
14
|
+
|
15
|
+
def params
|
16
|
+
{}
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'decent_exposure/expose'
|
3
|
+
|
4
|
+
class Controllers::WithOnlyParameterController < ActionController::Base
|
5
|
+
expose(:birds)
|
6
|
+
expose(:another_birds, model: :bird)
|
7
|
+
|
8
|
+
expose_query DummyQuery
|
9
|
+
expose_query NewDummyQuery, only: :another_birds
|
10
|
+
|
11
|
+
decent_configuration do
|
12
|
+
strategy DecentExposure::ActiveRecordStrategy
|
13
|
+
end
|
14
|
+
|
15
|
+
def params
|
16
|
+
{}
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,29 @@
|
|
1
1
|
require 'bundler/setup'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_record/fixtures'
|
4
|
+
require 'pry'
|
2
5
|
Bundler.setup
|
3
|
-
|
4
|
-
|
6
|
+
|
7
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
8
|
+
require 'codeclimate-test-reporter'
|
9
|
+
CodeClimate::TestReporter.start
|
10
|
+
end
|
5
11
|
|
6
12
|
require 'expose_query' # and any other gems you need
|
7
13
|
|
14
|
+
FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures')
|
15
|
+
|
16
|
+
ActiveRecord::Base.establish_connection(
|
17
|
+
:adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
|
18
|
+
:database => ':memory:'
|
19
|
+
)
|
20
|
+
|
21
|
+
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
22
|
+
dep.autoload_paths.unshift FIXTURES_PATH
|
23
|
+
|
24
|
+
ActiveRecord::Migration.verbose = false
|
25
|
+
load File.join(FIXTURES_PATH, 'schema.rb')
|
26
|
+
|
8
27
|
RSpec.configure do |config|
|
9
28
|
# some (optional) config here
|
10
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expose_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Chubarov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: decent_exposure
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec-rails
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -102,6 +116,13 @@ files:
|
|
102
116
|
- lib/expose_query/version.rb
|
103
117
|
- spec/expose_query/active_record_query_strategy_spec.rb
|
104
118
|
- spec/expose_query/controller_dsl_spec.rb
|
119
|
+
- spec/fixtures/bird.rb
|
120
|
+
- spec/fixtures/birds.yml
|
121
|
+
- spec/fixtures/controllers/with_except_parameter_controller.rb
|
122
|
+
- spec/fixtures/controllers/with_only_parameter_controller.rb
|
123
|
+
- spec/fixtures/dummy_query.rb
|
124
|
+
- spec/fixtures/new_dummy_query.rb
|
125
|
+
- spec/fixtures/schema.rb
|
105
126
|
- spec/spec_helper.rb
|
106
127
|
homepage: ''
|
107
128
|
licenses:
|
@@ -123,12 +144,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
144
|
version: '0'
|
124
145
|
requirements: []
|
125
146
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.4.6
|
127
148
|
signing_key:
|
128
149
|
specification_version: 4
|
129
150
|
summary: A helper for creating declaretive interface for decent_exposure queries
|
130
151
|
test_files:
|
131
152
|
- spec/expose_query/active_record_query_strategy_spec.rb
|
132
153
|
- spec/expose_query/controller_dsl_spec.rb
|
154
|
+
- spec/fixtures/bird.rb
|
155
|
+
- spec/fixtures/birds.yml
|
156
|
+
- spec/fixtures/controllers/with_except_parameter_controller.rb
|
157
|
+
- spec/fixtures/controllers/with_only_parameter_controller.rb
|
158
|
+
- spec/fixtures/dummy_query.rb
|
159
|
+
- spec/fixtures/new_dummy_query.rb
|
160
|
+
- spec/fixtures/schema.rb
|
133
161
|
- spec/spec_helper.rb
|
134
|
-
has_rdoc:
|