arqo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4ba2c84fa910379b38e591ab84f766e057c76de6
4
- data.tar.gz: b6e2b10c631038d4c766683b1159aebbe44325f9
2
+ SHA256:
3
+ metadata.gz: fe852ef6a59c623ab71a8ca9ecd5320d3252dece6df5a7421df998f1f096e87c
4
+ data.tar.gz: 9652f930a2ef38f274c590727cce67284478ddc3db0849b0892c4602bc11d8ea
5
5
  SHA512:
6
- metadata.gz: faa30d5cabb73243dc846d2f71eca8d521b0b3073467039df5350a7e4730096904320c935b1a727ac4f04a1f11b024004756b27dda8da993b5971f60fed271dd
7
- data.tar.gz: d7199ec586500009c448606a63454cc29e9c68e121dfd19874b54a5fc7918ec5af737c1b521f76f03d3173c5a09768ab74a09854ad401d513c1f803877836f78
6
+ metadata.gz: 321f73a94a640bdf532818263ad32530e69b79d33f86a941b1e1bb7ed0df06c0e5dd14dd25d6ec1477f8f70121773fecb1e3eb8814e9ad322040007b63bdeb0b
7
+ data.tar.gz: 32ca904418850549f24bf975e0fe8ac59d122ffebc7e3fca4b420cede4d3dd0e31f13602742e3509798d1d5049d90c66ca892a66d72d248813f79c73d6d499de
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)
@@ -135,6 +136,49 @@ And then you chain everything together and it will just work :)
135
136
  UserQuery.new.where.not(name: nil).active_last_week.not_deleted.order(:id)
136
137
  ```
137
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
+
138
182
  ## Development
139
183
 
140
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.
data/lib/arqo/query.rb CHANGED
@@ -4,6 +4,7 @@ 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
10
  def initialize(relation = associated_relation)
@@ -40,8 +41,8 @@ module ARQO
40
41
 
41
42
  unless Object.const_defined?(derived_relation_name)
42
43
  raise NameError, "Could not find model #{derived_relation_name} associated " \
43
- "to query #{class_name}.\n Make sure the name is correct or override " \
44
- '#associated_relation to provide a custom model'
44
+ "to query #{class_name}.\n Make sure the name is correct or override " \
45
+ '#associated_relation to provide a custom model'
45
46
  end
46
47
 
47
48
  derived_relation_name.constantize.all
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module ARQO
6
+ class Railtie < Rails::Railtie
7
+ generators do |app|
8
+ Rails::Generators.configure! app.config.generators
9
+ require_relative '../generators/model_generator'
10
+ end
11
+ end
12
+ end
data/lib/arqo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ARQO
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/arqo.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'arqo/query'
4
4
  require 'arqo/version'
5
+ require 'arqo/railtie'
5
6
 
6
7
  module ARQO
7
8
  class Error < StandardError; end
@@ -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
@@ -0,0 +1,9 @@
1
+ require 'rails_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ RSpec.describe <%= class_name %>Query do
5
+ subject(:query) { described_class.new }
6
+
7
+ pending 'add some query object tests here'
8
+ end
9
+ <% 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.2.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: 2020-07-19 00:00:00.000000000 Z
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: '4'
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: '4'
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: '7'
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: 5.6.0
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: 5.6.0
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: 0.80.0
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: 0.80.0
110
+ version: 1.24.1
103
111
  - !ruby/object:Gem::Dependency
104
112
  name: simplecov
105
113
  requirement: !ruby/object:Gem::Requirement
@@ -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
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
143
159
  homepage: https://github.com/rootstrap/arqo
144
160
  licenses:
145
161
  - MIT
146
162
  metadata:
147
163
  homepage_uri: https://github.com/rootstrap/arqo
148
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.3.0
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
- rubyforge_project:
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.