querii 1.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 446635ffc0f477018660c9586b56705a49e3b899
4
+ data.tar.gz: 5fb91f4beedc05e8d1e31f357548ce22a6f73a62
5
+ SHA512:
6
+ metadata.gz: 6c0b430fb59333ddff6abc66cb01fc48be087b66db5f97151851bab6750053ba60d3d3a58113e110ba23fe7171685b502ff2768f79abc4512519aecc5f9d92a8
7
+ data.tar.gz: 49a835f5f301a63c8db269e5f8f80bccad9b783c14b10be88f89b0662333fa8d11b680c6c2a37d16e292137613ccec268f668541d13bfd5aea6e9de7d418154e
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Querii Object Gem
2
+
3
+ ![Searching records](http://i.imgur.com/pa0oxih.jpg)
4
+
5
+ Querii makes it easy to create query objects that return active record relations
6
+
7
+ ## Why?
8
+
9
+ Querii allows you to extract complicated query collection logic out of a ActiveRecord model to prevent littering the base model and encourage breaking up your query logic into multiple methods, constants, etc, while still returning a standard activerecord collection (now extended by your additional querii methods) that can be further acted upon.
10
+
11
+ ## Basic Usage
12
+
13
+ ```ruby
14
+ module Accounts
15
+ module FancySubset
16
+ include Querii::QueryObject
17
+
18
+ module Scopes
19
+ def applied(*args, **key_args)
20
+ where(cool: 'things', many: key_args.fetch(:hats, 2))
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # basic call
27
+ Accounts::FancySubset.call # or .all
28
+
29
+ # or more advanced chain
30
+ Accounts::FancySubset.call(hats: 4).where(typical: 'filtered')
31
+ # ...Account::ActiveRecord_Relation < ActiveRecord::Relation
32
+ ```
33
+
34
+ Querii infers the base collection from the top level query object namespace of `Accounts` -> `Account.all`, you can override this by setting `self.default_relation = 'ClassName'` within your module. Querii defines a default `Scopes` module that will just return all but to do anything useful by default you will need to define your own `Scopes` module. During a query this module is extended onto the base `ActiveRecord::Relation` and the `applied` method is executed returning an active record collection that can further be queried by both any additional methods defined within your Querii object `Scopes` and scopes or conditions on the standard AR class.
35
+
36
+ ## More
37
+
38
+ ### Default scope
39
+
40
+ The `applied` method is entirely optional and designed for query objects that only contain a single base scope always intended to be called so you can simply do `MyQueryObj.call` or equivalently `MyQueryObj.all`. Additionally the applied method can accept and use any number of args or keyword args.
41
+
42
+ ### Passing an already filtered relation
43
+
44
+ `Account::FancySubset.call(relation: Account.joins(:whatever).such_scope)` will work out just fine.
@@ -0,0 +1,40 @@
1
+ module Querii
2
+ module QueryObject
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+
6
+ base.class_eval do
7
+ # dynamically defines a base Scopes module
8
+ self.const_set(
9
+ :Scopes,
10
+ Module.new do
11
+ def applied(*args, **key_args)
12
+ all
13
+ end
14
+ end
15
+ )
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def call(*args, relation: default_relation_all, **key_args)
21
+ relation.extending(self::Scopes).applied(args, key_args)
22
+ end
23
+
24
+ def default_relation_all
25
+ # .all returns current scope || all
26
+ relation_base.all
27
+ end
28
+
29
+ # queries are namespaced by plural model name
30
+ # from which we can infer query base model
31
+ def relation_base
32
+ default_relation.to_s.constantize || self.to_s.split('::').first.singularize.constantize
33
+ end
34
+
35
+ alias_method :all, :call
36
+
37
+ attr_accessor :default_relation
38
+ end
39
+ end
40
+ end
data/lib/querii.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'querii/query_object'
2
+
3
+ module Querii
4
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: querii
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin
8
+ - Doody
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.14'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.14'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.5'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.5'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: binding_of_caller
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Easily extract complicated query objects into simple query object services.
71
+ email:
72
+ - justin@20dots.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - README.md
78
+ - lib/querii.rb
79
+ - lib/querii/query_object.rb
80
+ homepage: https://github.com/justindoody/querii
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.7
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: AR Query objects patterns
104
+ test_files: []