arqo 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +62 -2
- data/lib/arqo/query.rb +34 -6
- data/lib/arqo/railtie.rb +12 -0
- data/lib/arqo/version.rb +1 -1
- data/lib/arqo.rb +1 -0
- data/lib/generators/arqo/named_base.rb +21 -0
- data/lib/generators/model_generator.rb +13 -0
- data/lib/generators/rails/USAGE +38 -0
- data/lib/generators/rails/query_generator.rb +22 -0
- data/lib/generators/rails/templates/query.rb.erb +15 -0
- data/lib/generators/rspec/query_generator.rb +16 -0
- data/lib/generators/rspec/templates/query_spec.rb.erb +9 -0
- metadata +36 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fe852ef6a59c623ab71a8ca9ecd5320d3252dece6df5a7421df998f1f096e87c
|
4
|
+
data.tar.gz: 9652f930a2ef38f274c590727cce67284478ddc3db0849b0892c4602bc11d8ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 321f73a94a640bdf532818263ad32530e69b79d33f86a941b1e1bb7ed0df06c0e5dd14dd25d6ec1477f8f70121773fecb1e3eb8814e9ad322040007b63bdeb0b
|
7
|
+
data.tar.gz: 32ca904418850549f24bf975e0fe8ac59d122ffebc7e3fca4b420cede4d3dd0e31f13602742e3509798d1d5049d90c66ca892a66d72d248813f79c73d6d499de
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -15,6 +15,7 @@ ARQO (Active Record Query Objects) is a minimal gem that let you use Query Objec
|
|
15
15
|
- [Setting up a query object](#setting-up-a-query-object)
|
16
16
|
- [Deriving the model](#deriving-the-model)
|
17
17
|
- [Chaining scopes](#chaining-scopes)
|
18
|
+
- [Generators](#generators)
|
18
19
|
- [Development](#development)
|
19
20
|
- [Contributing](#contributing)
|
20
21
|
- [License](#license)
|
@@ -96,6 +97,22 @@ you can use it like this:
|
|
96
97
|
CustomNamedQuery.new(User.all).active_last_week
|
97
98
|
```
|
98
99
|
|
100
|
+
you can also set the model class or relation to query from by overriding a simple method, like:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class CustomNamedQuery < ARQO::Query
|
104
|
+
module Scope
|
105
|
+
# ...
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def associated_relation
|
111
|
+
User # you can also do something like User.some_scope
|
112
|
+
end
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
99
116
|
## Chaining scopes
|
100
117
|
Of course you can chain everything together, methods defined in the query object and scopes defined in the model or by Rails.
|
101
118
|
```ruby
|
@@ -119,6 +136,49 @@ And then you chain everything together and it will just work :)
|
|
119
136
|
UserQuery.new.where.not(name: nil).active_last_week.not_deleted.order(:id)
|
120
137
|
```
|
121
138
|
|
139
|
+
## Generators
|
140
|
+
|
141
|
+
To create the query object we can use the rails generator tool. For that, we just run:
|
142
|
+
|
143
|
+
$ rails generate query User
|
144
|
+
|
145
|
+
And it will create your UserQuery object at `app/queries` folder. If you have set Rspec as your test framework, the corresponding spec file will be also created at `spec/queries`.
|
146
|
+
|
147
|
+
:warning: Rspec is the only test framework supported for now.
|
148
|
+
|
149
|
+
If your query object is based on another class, you can set the `associated_relation` attribute to automatically override the `associated_relation` method.
|
150
|
+
|
151
|
+
$ rails generate query CustomUser --associated_relation=User
|
152
|
+
|
153
|
+
### Model Generator
|
154
|
+
|
155
|
+
To generate the query object when you create your rails models, enable the query generators at your application config file adding the following line:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
# config/application.rb
|
159
|
+
|
160
|
+
module App
|
161
|
+
class Application < Rails::Application
|
162
|
+
...
|
163
|
+
|
164
|
+
config.generators do |g|
|
165
|
+
...
|
166
|
+
g.query true # Added line
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
Now, if you run the model generator:
|
173
|
+
|
174
|
+
$ rails generate model User
|
175
|
+
|
176
|
+
The query object and spec will be created as well as the model, migrations, test files, etc.
|
177
|
+
|
178
|
+
Another alternative, it is to add the `--query` option at the end of the command:
|
179
|
+
|
180
|
+
$ rails generate model User --query
|
181
|
+
|
122
182
|
## Development
|
123
183
|
|
124
184
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -127,7 +187,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
127
187
|
|
128
188
|
## Contributing
|
129
189
|
|
130
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
190
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rootstrap/arqo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rootstrap/arqo/blob/master/CODE_OF_CONDUCT.md).
|
131
191
|
|
132
192
|
|
133
193
|
## License
|
@@ -136,7 +196,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
136
196
|
|
137
197
|
## Code of Conduct
|
138
198
|
|
139
|
-
Everyone interacting in the ARQO project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
199
|
+
Everyone interacting in the ARQO project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rootstrap/arqo/blob/master/CODE_OF_CONDUCT.md).
|
140
200
|
|
141
201
|
## Logo attribution
|
142
202
|
Logo made by [iconixar](https://www.flaticon.com/free-icon/archery_3080892) from [www.flaticon.com](https://www.flaticon.com/)
|
data/lib/arqo/query.rb
CHANGED
@@ -4,20 +4,48 @@ module ARQO
|
|
4
4
|
# Parent class for query objects
|
5
5
|
class Query
|
6
6
|
attr_reader :relation
|
7
|
+
|
7
8
|
delegate_missing_to :relation
|
8
9
|
|
9
|
-
def initialize(relation =
|
10
|
+
def initialize(relation = associated_relation)
|
10
11
|
@relation = relation.extending(scope_module)
|
11
12
|
end
|
12
13
|
|
13
14
|
private
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
# Returns the model to which the query object is associated.
|
17
|
+
# By default it will infer the name from the query object's class
|
18
|
+
# assuming the name is the name of the model with "Query" as the suffix.
|
19
|
+
#
|
20
|
+
# As an example, UserQuery would be associated with the User model and
|
21
|
+
# Blog::PostQuery would be associated with the Blog::Post namespaced model.
|
22
|
+
#
|
23
|
+
# This method can be overridden to bypass this convention and associate
|
24
|
+
# the query object to another relation by returning it.
|
25
|
+
#
|
26
|
+
# ==== Example
|
27
|
+
# class PostQuery < ARQO::Query
|
28
|
+
# # ...
|
29
|
+
#
|
30
|
+
# private
|
31
|
+
#
|
32
|
+
# def associated_relation
|
33
|
+
# Blog::Post
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [Class, ActiveRecord::Relation]
|
38
|
+
def associated_relation
|
39
|
+
class_name = self.class.name
|
40
|
+
derived_relation_name = class_name.sub(/Query$/, '')
|
41
|
+
|
42
|
+
unless Object.const_defined?(derived_relation_name)
|
43
|
+
raise NameError, "Could not find model #{derived_relation_name} associated " \
|
44
|
+
"to query #{class_name}.\n Make sure the name is correct or override " \
|
45
|
+
'#associated_relation to provide a custom model'
|
46
|
+
end
|
18
47
|
|
19
|
-
|
20
|
-
derived_relation_name.constantize.all if Object.const_defined?(derived_relation_name)
|
48
|
+
derived_relation_name.constantize.all
|
21
49
|
end
|
22
50
|
|
23
51
|
def scope_module
|
data/lib/arqo/railtie.rb
ADDED
data/lib/arqo/version.rb
CHANGED
data/lib/arqo.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/named_base'
|
4
|
+
|
5
|
+
module ARQO
|
6
|
+
module Generators
|
7
|
+
class NamedBase < Rails::Generators::NamedBase
|
8
|
+
class_option :associated_relation, type: :string, default: nil
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def associated_relation
|
13
|
+
@associated_relation ||= options['associated_relation']
|
14
|
+
end
|
15
|
+
|
16
|
+
def associated_relation_class
|
17
|
+
associated_relation&.to_s&.classify
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'rails/generators/rails/model/model_generator'
|
5
|
+
require_relative 'rails/query_generator'
|
6
|
+
|
7
|
+
module Rails
|
8
|
+
module Generators
|
9
|
+
class ModelGenerator
|
10
|
+
hook_for :query, type: :boolean, default: false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
Usage:
|
2
|
+
rails generate query NAME [--associated_relation=model]
|
3
|
+
|
4
|
+
Description:
|
5
|
+
Generates a new ARQO query object at app/queries. Pass the generator name
|
6
|
+
as an argument, either CamelCased or snake_cased, and an optional
|
7
|
+
attribute 'associated_relation' as an argument.
|
8
|
+
|
9
|
+
The 'associated_relation' attribute expects an ActiveRecord class as value. This
|
10
|
+
is useful for cases where you want to override the associated relation in
|
11
|
+
the query object.
|
12
|
+
|
13
|
+
This generator invokes your configured ORM and test framework, which Active
|
14
|
+
Record and Rspec are the only ones supported at the moment.
|
15
|
+
|
16
|
+
Example:
|
17
|
+
`rails generate query Account`
|
18
|
+
|
19
|
+
Creates a standard account query object and the spec file.
|
20
|
+
|
21
|
+
Query Object: app/queries/account_query.rb
|
22
|
+
Rspec: spec/queries/account_query.rb
|
23
|
+
|
24
|
+
`rails generate query Admin::Account`
|
25
|
+
|
26
|
+
Creates an account query object and the spec file following the namespace
|
27
|
+
location.
|
28
|
+
|
29
|
+
Query Object: app/queries/admin/account_query.rb
|
30
|
+
Rspec: spec/queries/admin/account_query.rb
|
31
|
+
|
32
|
+
`rails generate query Account associated_relation:User`
|
33
|
+
|
34
|
+
Creates an account query object and the spec file including the
|
35
|
+
specification of the `associated_relation` method.
|
36
|
+
|
37
|
+
app/queries/account_query.rb
|
38
|
+
spec/queries/account_query.rb
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../arqo/named_base'
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
module Generators
|
7
|
+
class QueryGenerator < ARQO::Generators::NamedBase
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
check_class_collision suffix: 'Query'
|
11
|
+
|
12
|
+
hook_for :test_framework
|
13
|
+
|
14
|
+
def create_model_query
|
15
|
+
return if class_name.blank?
|
16
|
+
|
17
|
+
template_file = File.join('app/queries', class_path, "#{file_name}_query.rb")
|
18
|
+
template 'query.rb.erb', template_file
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<% module_namespacing do -%>
|
2
|
+
class <%= class_name %>Query < ARQO::Query
|
3
|
+
module Scope
|
4
|
+
# Add <%= class_name %> scopes here
|
5
|
+
end
|
6
|
+
<% if associated_relation -%>
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def associated_relation
|
11
|
+
<%= associated_relation_class %> # you can also do something like <%= associated_relation_class %>.some_scope
|
12
|
+
end
|
13
|
+
<% end -%>
|
14
|
+
end
|
15
|
+
<% end -%>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../arqo/named_base'
|
4
|
+
|
5
|
+
module Rspec
|
6
|
+
module Generators
|
7
|
+
class QueryGenerator < ARQO::Generators::NamedBase
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
def create_model_query_spec
|
11
|
+
template_file = File.join('spec/queries', class_path, "#{file_name}_query_spec.rb")
|
12
|
+
template 'query_spec.rb.erb', template_file
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arqo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santiago Bartesaghi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,20 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '7'
|
19
|
+
version: '5.2'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
30
|
-
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ammeter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
31
39
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
40
|
+
version: '1.1'
|
33
41
|
- !ruby/object:Gem::Dependency
|
34
42
|
name: database_cleaner-active_record
|
35
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,14 +72,14 @@ dependencies:
|
|
64
72
|
requirements:
|
65
73
|
- - "~>"
|
66
74
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
75
|
+
version: 6.0.6
|
68
76
|
type: :development
|
69
77
|
prerelease: false
|
70
78
|
version_requirements: !ruby/object:Gem::Requirement
|
71
79
|
requirements:
|
72
80
|
- - "~>"
|
73
81
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
82
|
+
version: 6.0.6
|
75
83
|
- !ruby/object:Gem::Dependency
|
76
84
|
name: rspec
|
77
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,14 +100,14 @@ dependencies:
|
|
92
100
|
requirements:
|
93
101
|
- - "~>"
|
94
102
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
103
|
+
version: 1.24.1
|
96
104
|
type: :development
|
97
105
|
prerelease: false
|
98
106
|
version_requirements: !ruby/object:Gem::Requirement
|
99
107
|
requirements:
|
100
108
|
- - "~>"
|
101
109
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
110
|
+
version: 1.24.1
|
103
111
|
- !ruby/object:Gem::Dependency
|
104
112
|
name: simplecov
|
105
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,7 +138,7 @@ dependencies:
|
|
130
138
|
version: 1.4.2
|
131
139
|
description:
|
132
140
|
email:
|
133
|
-
-
|
141
|
+
- santib@hey.com
|
134
142
|
executables: []
|
135
143
|
extensions: []
|
136
144
|
extra_rdoc_files: []
|
@@ -139,13 +147,22 @@ files:
|
|
139
147
|
- README.md
|
140
148
|
- lib/arqo.rb
|
141
149
|
- lib/arqo/query.rb
|
150
|
+
- lib/arqo/railtie.rb
|
142
151
|
- lib/arqo/version.rb
|
143
|
-
|
152
|
+
- lib/generators/arqo/named_base.rb
|
153
|
+
- lib/generators/model_generator.rb
|
154
|
+
- lib/generators/rails/USAGE
|
155
|
+
- lib/generators/rails/query_generator.rb
|
156
|
+
- lib/generators/rails/templates/query.rb.erb
|
157
|
+
- lib/generators/rspec/query_generator.rb
|
158
|
+
- lib/generators/rspec/templates/query_spec.rb.erb
|
159
|
+
homepage: https://github.com/rootstrap/arqo
|
144
160
|
licenses:
|
145
161
|
- MIT
|
146
162
|
metadata:
|
147
|
-
homepage_uri: https://github.com/
|
148
|
-
source_code_uri: https://github.com/
|
163
|
+
homepage_uri: https://github.com/rootstrap/arqo
|
164
|
+
source_code_uri: https://github.com/rootstrap/arqo
|
165
|
+
rubygems_mfa_required: 'true'
|
149
166
|
post_install_message:
|
150
167
|
rdoc_options: []
|
151
168
|
require_paths:
|
@@ -154,15 +171,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
154
171
|
requirements:
|
155
172
|
- - ">="
|
156
173
|
- !ruby/object:Gem::Version
|
157
|
-
version: 2.
|
174
|
+
version: 2.5.0
|
158
175
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
176
|
requirements:
|
160
177
|
- - ">="
|
161
178
|
- !ruby/object:Gem::Version
|
162
179
|
version: '0'
|
163
180
|
requirements: []
|
164
|
-
|
165
|
-
rubygems_version: 2.5.2.3
|
181
|
+
rubygems_version: 3.0.3.1
|
166
182
|
signing_key:
|
167
183
|
specification_version: 4
|
168
184
|
summary: Easing the query object pattern in Rails applications.
|