sql_attributes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6fda02cea7a7746bb74220df0ba4e08a7d6010af032ac846c7940e5e4350aed7
4
+ data.tar.gz: 283e7ddeb199f24a612a7da5986b432c3f6624150cc111ebbba4a18530f892e3
5
+ SHA512:
6
+ metadata.gz: 271b57dc2390c00e5fa7bd640e6c7ccedd70cb52a597fcaf97f51869d4df5632e88cef41dcc72f7ef0877fc170d8b80fa2b177fc7d01bd8516be168c843ccc96
7
+ data.tar.gz: 02d117721e8110f08e0caa96b1fdcc10443540d46d498805b3c7e191fe8444eeecf84f7032af098f107689189052310be77efda22ff9482fb554399a298881bb
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Stef Schenkelaars
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,116 @@
1
+ # SQL Attributes
2
+ This gem is here to help you with fetching record specific data from the database using subqueries. You could also use this gem as an alternative for counter caches but of course you should think about this carefully since it can be read heavy.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'sql_attributes'
9
+ ```
10
+
11
+ The of course run `bundle install`.
12
+
13
+ ## Usage
14
+ It starts by defining an SQL attribute on a class:
15
+
16
+ ```ruby
17
+ class Author < ApplicationRecord
18
+ sql_attribute :books_count, <<~SQL
19
+ SELECT COUNT(*)
20
+ FROM books
21
+ WHERE books.author_id = books.id
22
+ SQL
23
+
24
+ sql_attribute :total_pages, <<~SQL
25
+ SELECT SUM(books.pages)
26
+ FROM books
27
+ WHERE books.author_id = books.id
28
+ SQL
29
+
30
+ # Note that this aggregation method `GROUP_CONCAT` is different for other databases like Postgres
31
+ sql_attribute :publisher_names, <<~SQL
32
+ SELECT DISTINCT GROUP_CONCAT(publishers.name, ' - ')
33
+ FROM publishers
34
+ INNER JOIN books ON books.publisher_id = publishers.id
35
+ WHERE books.author_id = authors.id
36
+ GROUP BY books.author_id
37
+ SQL
38
+ end
39
+ ```
40
+
41
+ Before you can access the attribute, you have to include it to the SQL query. An error will be raised if you dont:
42
+ ```ruby
43
+ authors = Author.all
44
+ authors.map(&:books_count) # => raises SqlAttributes::NotLoaded
45
+ authors.map(&:total_pages) # => raises SqlAttributes::NotLoaded
46
+ ```
47
+
48
+ You can tell ActiveRecord / Arel to include a specific attribute by using the `with_<NAME>` helpers:
49
+ ```ruby
50
+ authors = Author.with_books_count.all
51
+ authors.map(&:books_count) # => [1, 2]
52
+ authors.map(&:total_pages) # => raises SqlAttributes::NotLoaded
53
+ ```
54
+
55
+ These methods are chainable and can be combined with normal scopes:
56
+ ```ruby
57
+ authors = Author.where(publisher_id: 42).with_books_count.with_total_pages.all
58
+ authors.map(&:books_count) # => [1, 2]
59
+ authors.map(&:total_pages) # => [300, 500]
60
+ ```
61
+
62
+ You can also load the attributes with the `with_sql_attributes` helper:
63
+ ```ruby
64
+ authors = Author.with_sql_attributes(:books_count, :publisher_names)
65
+ authors.map(&:books_count) # => [1, 2]
66
+ authors.map(&:total_pages) # => raises SqlAttributes::NotLoaded
67
+ ```
68
+
69
+ If you don't pass any argument, it will load all SQL attributes:
70
+ ```ruby
71
+ authors = Author.with_sql_attributes
72
+ authors.map(&:books_count) # => [1, 2]
73
+ authors.map(&:total_pages) # => [300, 500]
74
+ ```
75
+
76
+ ## Releasing new version
77
+ Publishing a new version is handled by the publish workflow. This workflow publishes a GitHub release to rubygems and GitHub package registry with the version defined in the release.
78
+
79
+ ## Contributing
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Drieam/sql_attributes.
81
+
82
+ ## License
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
84
+
85
+ ## Credits
86
+ A big inspration is [this blog](https://medium.com/@eric.programmer/the-sql-alternative-to-counter-caches-59e2098b7d7) about `The SQL Alternative To Counter Caches`.
87
+
88
+ The most important example from this blog:
89
+
90
+ ```ruby
91
+ class Author < ApplicationRecord
92
+ scope :with_counts, -> {
93
+ select <<~SQL
94
+ authors.*,
95
+ (
96
+ SELECT COUNT(books.id) FROM books
97
+ WHERE author_id = authors.id
98
+ ) AS books_count
99
+ SQL
100
+ }
101
+ end
102
+ ```
103
+
104
+ With this gem, this can be rewritten to
105
+
106
+ ```ruby
107
+ class Author < ApplicationRecord
108
+ sql_attribute :books_count, <<~SQL
109
+ SELECT COUNT(books.id)
110
+ FROM books
111
+ WHERE author_id = authors.id
112
+ SQL
113
+ end
114
+ ```
115
+
116
+ Also some ideas where 'stolen' from [this blog](https://engineering.culturehq.com/posts/2019-01-18-dynamic-activerecord-columns) about `Dynamic ActiveRecord columns`.
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SqlAttributes
4
+ class NotLoaded < StandardError; end
5
+
6
+ class NotDefined < StandardError; end
7
+
8
+ def sql_attributes
9
+ @sql_attributes ||= HashWithIndifferentAccess.new
10
+ end
11
+
12
+ def with_sql_attributes(*attributes)
13
+ requested_attributes =
14
+ if attributes.length.positive?
15
+ Array.wrap(attributes).flatten.reject(&:nil?)
16
+ else
17
+ sql_attributes.keys
18
+ end
19
+
20
+ requested_attributes.inject(all) do |scope, name|
21
+ unless sql_attributes.key?(name)
22
+ raise NotDefined, "You want to load dynamic SQL attribute `#{name}` but it is not defined."
23
+ end
24
+
25
+ scope.public_send("with_#{name}")
26
+ end
27
+ end
28
+
29
+ def sql_attribute(name, subquery)
30
+ sql_attributes[name] = subquery.squish
31
+
32
+ scope "with_#{name}".to_sym, lambda {
33
+ select(
34
+ arel.projections,
35
+ "(#{subquery.squish}) as #{name}"
36
+ )
37
+ }
38
+
39
+ define_method(name) do
40
+ return read_attribute(name) if has_attribute?(name)
41
+
42
+ raise NotLoaded, <<~MESSAGE
43
+ Dynamic SQL attribute `#{name}` not loaded from the database.
44
+
45
+ Use the `with_sql_attributes` scope to load the attribute.
46
+
47
+ MESSAGE
48
+ end
49
+ end
50
+ end
51
+
52
+ ActiveSupport.on_load(:active_record) do
53
+ extend SqlAttributes
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SqlAttributes
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stef Schenkelaars
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-05 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: 6.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: combustion
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: database_cleaner-active_record
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-github
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: rspec-rails
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
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-performance
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Add virtual attributes to an ActiveRecord model based on an SQL query.
140
+ email:
141
+ - stef.schenkelaars@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - LICENSE
147
+ - README.md
148
+ - lib/sql_attributes.rb
149
+ - lib/sql_attributes/version.rb
150
+ homepage: https://drieam.github.io/sql_attributes
151
+ licenses:
152
+ - MIT
153
+ metadata:
154
+ homepage_uri: https://drieam.github.io/sql_attributes
155
+ source_code_uri: https://github.com/Drieam/sql_attributes
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.5.0
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.1.4
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Add virtual attributes to an ActiveRecord model based on an SQL query.
175
+ test_files: []