dynamic-records-meritfront 1.1.8 → 1.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +113 -31
- data/lib/dynamic-records-meritfront/version.rb +1 -1
- data/lib/dynamic-records-meritfront.rb +311 -226
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d7119a7be4c9ab826096a83190f68aa1d069e7db36f03ddeade1665788fc449
|
4
|
+
data.tar.gz: 21e633fc3369067d1e6bd9090e83a2f241d6efacd2bb63f4c6d06cba0bf85e5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 330b3763f9c3387313f2142fe039e93fd36cca3a0736563f832b929640f6286fc3f03f3dde6dfa6b9e28ad52b576d14bab2eefc0525bc5fae712673f7069f4b5
|
7
|
+
data.tar.gz: e05da4103fe1fed2942963bf898f4a0994fa75a9fd2ddcf7cf9c4b2a3aa0cf78baed5c42c39fcdba43e2fa949850160a591da2ba092cfc41b62f873ef2f51848
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -11,7 +11,7 @@ I dont tend to get much feedback, so any given would be appreciated.
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
gem '
|
14
|
+
gem 'dynamic-records-meritfront'
|
15
15
|
```
|
16
16
|
|
17
17
|
And then execute:
|
@@ -20,34 +20,18 @@ And then execute:
|
|
20
20
|
|
21
21
|
Or install it yourself as:
|
22
22
|
|
23
|
-
$ gem install
|
23
|
+
$ gem install dynamic-records-meritfront
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
###
|
28
|
-
|
29
|
-
hashed global ids look like this: "gid://meritfront/User/K9YI4K". They also have an optional tag so it can also look like "gid://meritfront/User/K9YI4K@user_image". They are based on global ids.
|
27
|
+
### Apply to your ApplicationRecord class as such
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
See the hashid-rails gem for more (https://github.com/jcypret/hashid-rails). Also note that I aliased .hashid to .hid and .find_by_hashid to .hfind
|
39
|
-
|
40
|
-
#### methods from this gem
|
41
|
-
|
42
|
-
1. hgid(tag: nil) - get the hgid with optional tag. Aliased to ghid
|
43
|
-
2. hgid_as_selector(str, attribute: 'id') - get a css selector for the hgid, good for updating the front-end
|
44
|
-
3. self.locate_hgid(hgid_string, with_associations: nil, returns_nil: false) - locates the database record from a hgid. Here are some examples of usage:
|
45
|
-
- ApplicationRecord.locate_hgid(hgid) - <b>DANGEROUS</b> will return any object referenced by the hgid.
|
46
|
-
- User.locate_hgid(hgid) - locates the User record but only if the hgid references a user class. Fires an error if not.
|
47
|
-
- ApplicationRecord.locate_hgid(hgid, with_associations: [:votes]) - locates the record but only if the record's class has a :votes active record association. So for instance, you can accept only votable objects for upvote functionality. Fires an error if the hgid does not match.
|
48
|
-
- User.locate_hgid(hgid, returns_nil: true) - locates the hgid but only if it is the user class. Returns nil if not.
|
49
|
-
4. get_hgid_tag(hgid) - returns the tag attached to the hgid
|
50
|
-
5. self.blind_hgid(id, tag) - creates
|
29
|
+
```ruby
|
30
|
+
class ApplicationRecord < ActiveRecord::Base
|
31
|
+
self.abstract_class = true
|
32
|
+
include DynamicRecordsMeritfront
|
33
|
+
end
|
34
|
+
```
|
51
35
|
|
52
36
|
### SQL methods
|
53
37
|
|
@@ -92,6 +76,7 @@ obj.has_association?(:votes) #false
|
|
92
76
|
</details>
|
93
77
|
|
94
78
|
#### self.headache_sql(name, sql, opts = { })
|
79
|
+
A better and safer way to write sql.
|
95
80
|
with options:
|
96
81
|
- instantiate_class: returns User, Post, etc objects instead of straight sql output.
|
97
82
|
I prefer doing the alterantive
|
@@ -100,8 +85,8 @@ with options:
|
|
100
85
|
- prepare: sets whether the db will preprocess the strategy for lookup (defaults true) (have not verified the prepared-ness)
|
101
86
|
- name_modifiers: allows one to change the preprocess associated name, useful in cases of dynamic sql.
|
102
87
|
- multi_query: allows more than one query (you can seperate an insert and an update with ';' I dont know how else to say it.)
|
103
|
-
this disables other options (except name_modifiers). Not sure how it effects prepared statements.
|
104
|
-
- async:
|
88
|
+
this disables other options including arguments (except name_modifiers). Not sure how it effects prepared statements.
|
89
|
+
- async: Gets passed to ActiveRecord::Base.connection.exec_query as a parameter. See that methods documentation for more. I was looking through the source code, and I think it only effects how it logs to the logfile?
|
105
90
|
- other options: considered sql arguments
|
106
91
|
|
107
92
|
<details>
|
@@ -118,7 +103,7 @@ Delete Friend Requests between two users after they have become friends.
|
|
118
103
|
</details>
|
119
104
|
|
120
105
|
<details>
|
121
|
-
<summary>
|
106
|
+
<summary>dynamic sql example usage</summary>
|
122
107
|
Get all users who have made a friend request to a particular user with an optional limit.
|
123
108
|
This is an example of why this method is good for dynamic prepared statements.
|
124
109
|
|
@@ -138,13 +123,110 @@ This is an example of why this method is good for dynamic prepared statements.
|
|
138
123
|
])
|
139
124
|
```
|
140
125
|
</details>
|
126
|
+
<details>
|
127
|
+
<summary>example usage with selecting records that match list of ids</summary>
|
128
|
+
Get users who match a list of ids. Uses a postgresql Array, see the potential issues section
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
id_list = [1,2,3]
|
132
|
+
return User.headache_sql('get_usrs', %Q{
|
133
|
+
SELECT * FROM users WHERE id = ANY (:id_list)
|
134
|
+
}, id_list: id_list)
|
135
|
+
```
|
136
|
+
</details>
|
137
|
+
|
138
|
+
<details>
|
139
|
+
<summary>example usage a custom upsert</summary>
|
140
|
+
Do an upsert
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
rows = uzrs.map{|u| [
|
144
|
+
u.id, #user_id
|
145
|
+
self.id, #conversation_id
|
146
|
+
from, #invited_by
|
147
|
+
t, #created_at
|
148
|
+
t, #updated_at
|
149
|
+
]}
|
150
|
+
ApplicationRecord.headache_sql("upsert_conversation_invites_2", %Q{
|
151
|
+
INSERT INTO conversation_participants (user_id, conversation_id, invited_by, created_at, updated_at)
|
152
|
+
VALUES :rows
|
153
|
+
ON CONFLICT (conversation_id,user_id)
|
154
|
+
DO UPDATE SET updated_at = :time
|
155
|
+
}, rows: rows, time: t)
|
156
|
+
```
|
157
|
+
This will output sql similar to below. Note this can be done for multiple conversation_participants. Also note that it only sent one time variable as an argument as headache_sql detected that we were sending duplicate information.
|
158
|
+
```sql
|
159
|
+
INSERT INTO conversation_participants (user_id, conversation_id, invited_by, created_at, updated_at)
|
160
|
+
VALUES ($1,$2,$3,$4,$4)
|
161
|
+
ON CONFLICT (conversation_id,user_id)
|
162
|
+
DO UPDATE SET updated_at = $4
|
163
|
+
-- [["rows_1", 15], ["rows_2", 67], ["rows_3", 6], ["rows_4", "2022-10-13 20:49:27.441372"]]
|
164
|
+
```
|
165
|
+
</details>
|
166
|
+
|
167
|
+
|
141
168
|
|
142
169
|
#### self.headache_preload(records, associations)
|
143
|
-
Preloads from a list of records, and not from a ActiveRecord_Relation. This will be useful when using the above headache_sql method.
|
170
|
+
Preloads from a list of records, and not from a ActiveRecord_Relation. This will be useful when using the above headache_sql method (as it returns a list of records, and not a record relation).
|
171
|
+
|
172
|
+
<details>
|
173
|
+
<summary>example usage</summary>
|
174
|
+
Preload :votes on some comments. :votes is an active record has_many relation.
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
comments = Comment.headache_sql('get_comments', %Q{
|
178
|
+
SELECT * FROM comments LIMIT 4
|
179
|
+
})
|
180
|
+
ApplicationRecord.headache_preload(comments, [:votes])
|
181
|
+
puts comments[0].votes #this line should be preloaded and hence not call the database
|
182
|
+
```
|
183
|
+
</details>
|
184
|
+
|
185
|
+
### Hashed Global IDS
|
186
|
+
|
187
|
+
hashed global ids look like this: "gid://meritfront/User/K9YI4K". They also have an optional tag so it can also look like "gid://meritfront/User/K9YI4K@user_image". They are based on global ids.
|
188
|
+
|
189
|
+
I have been using hgids (Hashed Global IDs) for a while now and they have some unique benefits in front-end back-end communication. This is as they:
|
190
|
+
1. hash the id which is good practice
|
191
|
+
2. provide a way to have tags, this is good when updating different UI elements dynamically from the backend. For instance updating the @user_image without affecting the @user_name
|
192
|
+
3. Carry the class with them, this can allow for more abstract and efficient code, and prevents id collisions between diffrent classes.
|
193
|
+
|
194
|
+
#### methods from the hashid-rails gem
|
195
|
+
|
196
|
+
See the hashid-rails gem for more (https://github.com/jcypret/hashid-rails). Also note that I aliased .hashid to .hid and .find_by_hashid to .hfind
|
197
|
+
|
198
|
+
#### methods from this gem
|
199
|
+
|
200
|
+
1. hgid(tag: nil) - get the hgid with optional tag. Aliased to ghid
|
201
|
+
2. hgid_as_selector(str, attribute: 'id') - get a css selector for the hgid, good for updating the front-end (especially over cable-ready and morphdom operations)
|
202
|
+
3. self.locate_hgid(hgid_string, with_associations: nil, returns_nil: false) - locates the database record from a hgid. Here are some examples of usage:
|
203
|
+
- ApplicationRecord.locate_hgid(hgid) - <b>DANGEROUS</b> will return any object referenced by the hgid.
|
204
|
+
- User.locate_hgid(hgid) - locates the User record but only if the hgid references a user class. Fires an error if not.
|
205
|
+
- ApplicationRecord.locate_hgid(hgid, with_associations: [:votes]) - locates the record but only if the record's class has a :votes active record association. So for instance, you can accept only votable objects for upvote functionality. Fires an error if the hgid does not match.
|
206
|
+
- User.locate_hgid(hgid, returns_nil: true) - locates the hgid but only if it is the user class. Returns nil if not.
|
207
|
+
4. get_hgid_tag(hgid) - returns the tag attached to the hgid
|
208
|
+
5. self.blind_hgid(id, tag) - creates a hgid without bringing the object down from the database. Useful with hashid-rails encode_id and decode_id methods
|
209
|
+
|
210
|
+
## Potential Issues
|
211
|
+
|
212
|
+
This gem was made with a postgresql database. Although most of the headache_sql code <i>should</i> be usable between databases, there is no abstracted ActiveRecord array type, and no similar classes to ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array for non-postgresql databases (at least, none I could find). I am not 100% sure that it will be an issue, but it might be.
|
213
|
+
|
214
|
+
Let me know if this actually becomes an issue for someone and I will throw in a workaround.
|
144
215
|
|
216
|
+
## Next Up
|
217
|
+
- I have the beginnings of something called swiss_instaload in mind, which will load multiple tables at the same time. For instance instead of Doing a ```usrs = User.all``` combined with a ```usrs.preload(:votes)```, which takes two sql requests, it could be done in one. Its kind of a crazy and dubious idea (efficiency wise), but I have a working prototype. It works by casting everything to json before returning it from the database. There might be a better way to do that long term though.
|
218
|
+
- will be changing names from headache_* which is a bit negative to swiss_* as in swiss_army_knife which is known for its wide versitility. headache names will become aliases.
|
219
|
+
|
220
|
+
## Changelog
|
221
|
+
|
222
|
+
1.1.10
|
223
|
+
- Added functionality in headache_sql where for sql arguments that are equal, we only use one sql argument instead of repeating arguments
|
224
|
+
- Added functionality in headache_sql for 'multi row expressions' which are inputtable as an Array of Arrays. See the upsert example in the headache_sql documentation above for more.
|
225
|
+
- Added a warning in the README for non-postgresql databases. Contact me if you hit issues and we can work it out.
|
226
|
+
|
145
227
|
## Contributing
|
146
228
|
|
147
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
229
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LukeClancy/dynamic-records-meritfront. 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/LukeClancy/dynamic-records-meritfront/blob/master/CODE_OF_CONDUCT.md).
|
148
230
|
|
149
231
|
## License
|
150
232
|
|
@@ -152,4 +234,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
152
234
|
|
153
235
|
## Code of Conduct
|
154
236
|
|
155
|
-
Everyone interacting in the ActiveRecordMeritfront project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
237
|
+
Everyone interacting in the ActiveRecordMeritfront project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/LukeClancy/dynamic-records-meritfront/blob/master/CODE_OF_CONDUCT.md).
|
@@ -2,7 +2,7 @@ require "dynamic-records-meritfront/version"
|
|
2
2
|
require 'hashid/rails'
|
3
3
|
|
4
4
|
module DynamicRecordsMeritfront
|
5
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
# the two aliases so I dont go insane
|
8
8
|
module Hashid::Rails
|
@@ -12,199 +12,273 @@ module DynamicRecordsMeritfront
|
|
12
12
|
alias hfind find_by_hashid
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
included do
|
16
|
+
# include hash id gem
|
17
|
+
include Hashid::Rails
|
18
|
+
#should work, probably able to override by redefining in ApplicationRecord class.
|
19
|
+
#Note we defined here as it breaks early on as Rails.application returns nil
|
20
|
+
PROJECT_NAME = Rails.application.class.to_s.split("::").first.to_s.downcase
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
|
32
|
-
# class UserImageRelation < ApplicationRecord
|
33
|
-
# belongs_to :imageable, polymorphic: true
|
34
|
-
# belongs_to :image
|
35
|
-
# end
|
36
|
-
# else
|
37
|
-
# class UserImageRelation; end
|
38
|
-
# end
|
39
|
-
# example usage two: only load relation if it exists in the database
|
40
|
-
# class UserImageRelation < ApplicationRecord
|
41
|
-
# if ApplicationRecord.has_run_migration?('UserImageRelationsTwo')
|
42
|
-
# belongs_to :imageable, polymorphic: true
|
43
|
-
# end
|
44
|
-
# end
|
45
|
-
#
|
46
|
-
#current version of migrations
|
47
|
-
cv = ActiveRecord::Base.connection.migration_context.current_version
|
48
|
-
|
49
|
-
#find the migration object for the name
|
50
|
-
migration = ActiveRecord::Base.connection.migration_context.migrations.filter!{|a|
|
51
|
-
a.name == nm
|
52
|
-
}.first
|
53
|
-
|
54
|
-
#if the migration object is nil, it has not yet been created
|
55
|
-
if migration.nil?
|
56
|
-
Rails.logger.info "No migration found for #{nm}. The migration has not yet been created, or is foreign to this database."
|
57
|
-
return false
|
58
|
-
end
|
59
|
-
|
60
|
-
#get the version number for the migration name
|
61
|
-
needed_version = migration.version
|
23
|
+
class MultiRowExpression
|
24
|
+
#this class is meant to be used in congunction with headache_sql method
|
25
|
+
#Could be used like so in headache_sql:
|
26
|
+
|
27
|
+
#ApplicationRecord.headache_sql( "teeeest", %Q{
|
28
|
+
# INSERT INTO tests(id, username, is_awesome)
|
29
|
+
# VALUES :rows
|
30
|
+
# ON CONFLICT SET is_awesome = true
|
31
|
+
#}, rows: [[1, luke, true], [2, josh, false]])
|
62
32
|
|
63
|
-
|
64
|
-
migration_ran = (cv >= needed_version)
|
33
|
+
#which would output this sql
|
65
34
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
Rails.logger.info "#{nm} migration has not run yet. This may lead to limited functionality"
|
70
|
-
end
|
35
|
+
# INSERT INTO tests(id, username, is_awesome)
|
36
|
+
# VALUES ($0,$1,$2),($3,$4,$5)
|
37
|
+
# ON CONFLICT SET is_awesome = true
|
71
38
|
|
72
|
-
|
39
|
+
attr_accessor :val
|
40
|
+
def initialize(val)
|
41
|
+
#assuming we are putting in an array of arrays.
|
42
|
+
self.val = val
|
73
43
|
end
|
74
|
-
|
75
|
-
|
76
|
-
#
|
77
|
-
|
44
|
+
def for_query(x = 0, unique_value_hash:)
|
45
|
+
#accepts x = current number of variables previously processed
|
46
|
+
#returns ["sql string with $# location information", variables themselves in order, new x]
|
47
|
+
db_val = val.map{|attribute_array| "(#{
|
48
|
+
attribute_index = 0
|
49
|
+
attribute_array.map{|attribute|
|
50
|
+
prexist_num = unique_value_hash[attribute]
|
51
|
+
if prexist_num
|
52
|
+
attribute_array[attribute_index] = nil
|
53
|
+
ret = "$#{prexist_num}"
|
54
|
+
else
|
55
|
+
unique_value_hash[attribute] = x
|
56
|
+
ret = "$#{x}"
|
57
|
+
x += 1
|
58
|
+
end
|
59
|
+
attribute_index += 1
|
60
|
+
next ret
|
61
|
+
}.join(",")
|
62
|
+
})"}.join(",")
|
63
|
+
return db_val, val.flatten.select{|a| not a.nil?}, x
|
78
64
|
end
|
65
|
+
end
|
79
66
|
|
80
|
-
|
81
|
-
|
82
|
-
|
67
|
+
module ClassMethods
|
68
|
+
def has_run_migration?(nm)
|
69
|
+
#put in a string name of the class and it will say if it has allready run the migration.
|
70
|
+
#good during enum migrations as the code to migrate wont run if enumerate is there
|
71
|
+
#as it is not yet enumerated (causing an error when it loads the class that will have the
|
72
|
+
#enumeration in it). This can lead it to being impossible to commit clean code.
|
73
|
+
#
|
74
|
+
# example usage one: only create the record class if it currently exists in the database
|
75
|
+
# if ApplicationRecord.has_run_migration?('UserImageRelationsTwo')
|
76
|
+
# class UserImageRelation < ApplicationRecord
|
77
|
+
# belongs_to :imageable, polymorphic: true
|
78
|
+
# belongs_to :image
|
79
|
+
# end
|
80
|
+
# else
|
81
|
+
# class UserImageRelation; end
|
82
|
+
# end
|
83
|
+
# example usage two: only load relation if it exists in the database
|
84
|
+
# class UserImageRelation < ApplicationRecord
|
85
|
+
# if ApplicationRecord.has_run_migration?('UserImageRelationsTwo')
|
86
|
+
# belongs_to :imageable, polymorphic: true
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
#current version of migrations
|
91
|
+
cv = ActiveRecord::Base.connection.migration_context.current_version
|
92
|
+
|
93
|
+
#find the migration object for the name
|
94
|
+
migration = ActiveRecord::Base.connection.migration_context.migrations.filter!{|a|
|
95
|
+
a.name == nm
|
96
|
+
}.first
|
97
|
+
|
98
|
+
#if the migration object is nil, it has not yet been created
|
99
|
+
if migration.nil?
|
100
|
+
Rails.logger.info "No migration found for #{nm}. The migration has not yet been created, or is foreign to this database."
|
101
|
+
return false
|
102
|
+
end
|
103
|
+
|
104
|
+
#get the version number for the migration name
|
105
|
+
needed_version = migration.version
|
106
|
+
|
107
|
+
#if current version is above or equal, the migration has allready been run
|
108
|
+
migration_ran = (cv >= needed_version)
|
109
|
+
|
110
|
+
if migration_ran
|
111
|
+
Rails.logger.info "#{nm} migration was run on #{needed_version}. If old and all instances are migrated, consider removing code check."
|
112
|
+
else
|
113
|
+
Rails.logger.info "#{nm} migration has not run yet. This may lead to limited functionality"
|
114
|
+
end
|
115
|
+
|
116
|
+
return migration_ran
|
117
|
+
end
|
118
|
+
|
119
|
+
def list_associations
|
120
|
+
#lists associations (see has_association? below)
|
121
|
+
reflect_on_all_associations.map(&:name)
|
122
|
+
end
|
83
123
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
args = args.flatten.map { |a| a.to_sym }
|
88
|
-
associations = list_associations
|
89
|
-
(args.length == (associations & args).length)
|
90
|
-
end
|
91
|
-
|
92
|
-
def blind_hgid(id, tag: nil)
|
93
|
-
# this method is to get an hgid for a class without actually calling it down from the database.
|
94
|
-
# For example Notification.blind_hgid 1 will give gid://PROJECT_NAME/Notification/69DAB69 etc.
|
95
|
-
unless id.class == String
|
96
|
-
id = self.encode_id id
|
97
|
-
end
|
98
|
-
gid = "gid://#{PROJECT_NAME}/#{self.to_s}/#{id}"
|
99
|
-
if !tag
|
100
|
-
gid
|
101
|
-
else
|
102
|
-
"#{gid}@#{tag}"
|
103
|
-
end
|
104
|
-
end
|
124
|
+
def has_association?(*args)
|
125
|
+
#checks whether current class has needed association (for example, checks it has comments)
|
126
|
+
#associations can be seen in has_many belongs_to and other similar methods
|
105
127
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
#get the class
|
128
|
-
begin
|
129
|
-
cls = splitz[-2].constantize
|
130
|
-
rescue NameError, NoMethodError
|
131
|
-
if returns_nil
|
132
|
-
nil
|
133
|
-
else
|
134
|
-
raise StandardError.new 'Unusual or unavailable string or hgid'
|
135
|
-
end
|
136
|
-
end
|
137
|
-
#get the hash
|
138
|
-
hash = splitz[-1]
|
139
|
-
# if self == ApplicationRecord (for instance), then check that cls is a subclass
|
140
|
-
# if self is not ApplicationRecord, then check cls == this objects class
|
141
|
-
# if with_associations defined, make sure that the class has the associations given (see has_association above)
|
142
|
-
if ((self.abstract_class? and cls < self) or ( (not self.abstract_class?) and cls == self )) and
|
143
|
-
( with_associations == nil or cls.has_association?(with_associations) )
|
144
|
-
#if all is as expected, return the object with its id.
|
145
|
-
if block_given?
|
146
|
-
yield(hash)
|
147
|
-
else
|
148
|
-
cls.hfind(hash)
|
149
|
-
end
|
150
|
-
elsif returns_nil
|
151
|
-
#allows us to handle issues with input
|
152
|
-
nil
|
153
|
-
else
|
154
|
-
#stops execution as default
|
155
|
-
raise StandardError.new 'Not the expected class, or a subclass of ApplicationRecord if called on that.'
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
def get_hgid_tag(hgid_string)
|
160
|
-
if hgid_string.include?('@')
|
161
|
-
return hgid_string.split('@')[-1]
|
162
|
-
else
|
163
|
-
return nil
|
164
|
-
end
|
165
|
-
end
|
128
|
+
#flattens so you can pass self.has_association?(:comments, :baseable_comments) aswell as
|
129
|
+
# self.has_association?([:comments, :baseable_comments]) without issue
|
130
|
+
#
|
131
|
+
args = args.flatten.map { |a| a.to_sym }
|
132
|
+
associations = list_associations
|
133
|
+
(args.length == (associations & args).length)
|
134
|
+
end
|
135
|
+
|
136
|
+
def blind_hgid(id, tag: nil, encode: true)
|
137
|
+
# this method is to get an hgid for a class without actually calling it down from the database.
|
138
|
+
# For example Notification.blind_hgid 1 will give gid://PROJECT_NAME/Notification/69DAB69 etc.
|
139
|
+
if id.class == Integer and encode
|
140
|
+
id = self.encode_id id
|
141
|
+
end
|
142
|
+
gid = "gid://#{PROJECT_NAME}/#{self.to_s}/#{id}"
|
143
|
+
if !tag
|
144
|
+
gid
|
145
|
+
else
|
146
|
+
"#{gid}@#{tag}"
|
147
|
+
end
|
148
|
+
end
|
166
149
|
|
167
|
-
|
168
|
-
|
169
|
-
|
150
|
+
def string_as_selector(str, attribute: 'id')
|
151
|
+
#this is needed to allow us to quey various strange characters in the id etc. (see hgids)
|
152
|
+
#also useful for querying various attributes
|
153
|
+
return "*[#{attribute}=\"#{str}\"]"
|
154
|
+
end
|
155
|
+
|
156
|
+
def locate_hgid(hgid_string, with_associations: nil, returns_nil: false)
|
157
|
+
if hgid_string == nil or hgid_string.class != String
|
158
|
+
if returns_nil
|
159
|
+
return nil
|
160
|
+
else
|
161
|
+
raise StandardError.new("non-string class passed to ApplicationRecord#locate_hgid as the hgid_string variable")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
if hgid_string.include?('@')
|
165
|
+
hgid_string = hgid_string.split('@')
|
166
|
+
hgid_string.pop
|
167
|
+
hgid_string = hgid_string.join('@') # incase the model was a tag that was tagged. (few months later: Wtf? Guess ill keep it)
|
168
|
+
end
|
169
|
+
#split the thing
|
170
|
+
splitz = hgid_string.split('/')
|
171
|
+
#get the class
|
172
|
+
begin
|
173
|
+
cls = splitz[-2].constantize
|
174
|
+
rescue NameError, NoMethodError
|
175
|
+
if returns_nil
|
176
|
+
nil
|
177
|
+
else
|
178
|
+
raise StandardError.new 'Unusual or unavailable string or hgid'
|
179
|
+
end
|
180
|
+
end
|
181
|
+
#get the hash
|
182
|
+
hash = splitz[-1]
|
183
|
+
# if self == ApplicationRecord (for instance), then check that cls is a subclass
|
184
|
+
# if self is not ApplicationRecord, then check cls == this objects class
|
185
|
+
# if with_associations defined, make sure that the class has the associations given (see has_association above)
|
186
|
+
if ((self.abstract_class? and cls < self) or ( (not self.abstract_class?) and cls == self )) and
|
187
|
+
( with_associations == nil or cls.has_association?(with_associations) )
|
188
|
+
#if all is as expected, return the object with its id.
|
189
|
+
if block_given?
|
190
|
+
yield(hash)
|
191
|
+
else
|
192
|
+
cls.hfind(hash)
|
193
|
+
end
|
194
|
+
elsif returns_nil
|
195
|
+
#allows us to handle issues with input
|
196
|
+
nil
|
197
|
+
else
|
198
|
+
#stops execution as default
|
199
|
+
raise StandardError.new 'Not the expected class, or a subclass of ApplicationRecord if called on that.'
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def get_hgid_tag(hgid_string)
|
204
|
+
if hgid_string.include?('@')
|
205
|
+
return hgid_string.split('@')[-1]
|
206
|
+
else
|
207
|
+
return nil
|
208
|
+
end
|
209
|
+
end
|
170
210
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
# active_model/type/big_integer
|
175
|
-
# active_model/type/binary
|
176
|
-
# active_model/type/boolean
|
177
|
-
# active_model/type/date
|
178
|
-
# active_model/type/date_time
|
179
|
-
# active_model/type/decimal
|
180
|
-
# active_model/type/float
|
181
|
-
# active_model/type/immutable_string
|
182
|
-
# active_model/type/integer
|
183
|
-
# active_model/type/string
|
184
|
-
# active_model/type/time
|
185
|
-
# active_model
|
211
|
+
#thank god for some stack overflow people are pretty awesome https://stackoverflow.com/questions/64894375/executing-a-raw-sql-query-in-rails-with-an-array-parameter-against-postgresql
|
212
|
+
#BigIntArray = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(ActiveModel::Type::BigInteger.new).freeze
|
213
|
+
#IntegerArray = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(ActiveModel::Type::Integer.new).freeze
|
186
214
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
215
|
+
#https://api.rubyonrails.org/files/activemodel/lib/active_model/type_rb.html
|
216
|
+
# active_model/type/helpers
|
217
|
+
# active_model/type/value
|
218
|
+
# active_model/type/big_integer
|
219
|
+
# active_model/type/binary
|
220
|
+
# active_model/type/boolean
|
221
|
+
# active_model/type/date
|
222
|
+
# active_model/type/date_time
|
223
|
+
# active_model/type/decimal
|
224
|
+
# active_model/type/float
|
225
|
+
# active_model/type/immutable_string
|
226
|
+
# active_model/type/integer
|
227
|
+
# active_model/type/string
|
228
|
+
# active_model/type/time
|
229
|
+
# active_model
|
200
230
|
|
231
|
+
DB_TYPE_MAPS = {
|
232
|
+
String => ActiveModel::Type::String,
|
233
|
+
Symbol => ActiveModel::Type::String,
|
234
|
+
Integer => ActiveModel::Type::BigInteger,
|
235
|
+
BigDecimal => ActiveRecord::Type::Decimal,
|
236
|
+
TrueClass => ActiveModel::Type::Boolean,
|
237
|
+
FalseClass => ActiveModel::Type::Boolean,
|
238
|
+
Date => ActiveModel::Type::Date,
|
239
|
+
DateTime => ActiveModel::Type::DateTime,
|
240
|
+
Time => ActiveModel::Type::Time,
|
241
|
+
Float => ActiveModel::Type::Float,
|
242
|
+
Array => Proc.new{ |first_el_class| ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(DB_TYPE_MAPS[first_el_class].new) }
|
243
|
+
}
|
201
244
|
|
202
|
-
|
203
|
-
|
204
|
-
|
245
|
+
def convert_to_query_attribute(name, v)
|
246
|
+
#yes its dumb I know dont look at me look at rails
|
247
|
+
|
248
|
+
# https://stackoverflow.com/questions/40407700/rails-exec-query-bindings-ignored
|
249
|
+
# binds = [ ActiveRecord::Relation::QueryAttribute.new(
|
250
|
+
# "id", 6, ActiveRecord::Type::Integer.new
|
251
|
+
# )]
|
252
|
+
# ApplicationRecord.connection.exec_query(
|
253
|
+
# 'SELECT * FROM users WHERE id = $1', 'sql', binds
|
254
|
+
# )
|
255
|
+
|
256
|
+
return v if v.kind_of? ActiveRecord::Relation::QueryAttribute #so users can have fine-grained control if they are trying to do something
|
257
|
+
#that we didn't handle properly.
|
258
|
+
|
259
|
+
type = DB_TYPE_MAPS[v.class]
|
260
|
+
if type.nil?
|
261
|
+
raise StandardError.new("#{v}'s class #{v.class} unsupported type right now for ApplicationRecord#headache_sql")
|
262
|
+
elsif type.class == Proc
|
263
|
+
a = v[0]
|
264
|
+
# if a.nil?
|
265
|
+
# a = Integer
|
266
|
+
# elsif a.class == Array
|
267
|
+
a = a.nil? ? Integer : a.class
|
268
|
+
type = type.call(a)
|
269
|
+
else
|
270
|
+
type = type.new
|
271
|
+
end
|
272
|
+
|
273
|
+
ActiveRecord::Relation::QueryAttribute.new( name, v, type )
|
205
274
|
end
|
206
275
|
|
207
|
-
|
276
|
+
#allows us to preload on a list and not a active record relation. So basically from the output of headache_sql
|
277
|
+
def headache_preload(records, associations)
|
278
|
+
ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
|
279
|
+
end
|
280
|
+
|
281
|
+
def headache_sql(name, sql, opts = { }) #see below for opts
|
208
282
|
# - instantiate_class - returns User, Post, etc objects instead of straight sql output.
|
209
283
|
# I prefer doing the alterantive
|
210
284
|
# User.headache_class(...)
|
@@ -212,11 +286,12 @@ module DynamicRecordsMeritfront
|
|
212
286
|
# - prepare sets whether the db will preprocess the strategy for lookup (defaults true) (I dont think turning this off works...)
|
213
287
|
# - name_modifiers allows one to change the preprocess associated name, useful in cases of dynamic sql.
|
214
288
|
# - multi_query allows more than one query (you can seperate an insert and an update with ';' I dont know how else to say it.)
|
215
|
-
# this disables other options (except name_modifiers). Not sure how it effects prepared statements.
|
289
|
+
# this disables other options (except name_modifiers). Not sure how it effects prepared statements. Its a fairly useless
|
290
|
+
# command as you can do multiple queries anyway with 'WITH' statements and also gain the other options.
|
216
291
|
# - async does what it says but I haven't used it yet so. Probabably doesn't work
|
217
292
|
#
|
218
293
|
# Any other option is assumed to be a sql argument (see other examples in code base)
|
219
|
-
|
294
|
+
|
220
295
|
#grab options from the opts hash
|
221
296
|
instantiate_class = opts.delete(:instantiate_class)
|
222
297
|
name_modifiers = opts.delete(:name_modifiers)
|
@@ -225,74 +300,85 @@ module DynamicRecordsMeritfront
|
|
225
300
|
multi_query = opts.delete(:multi_query) == true
|
226
301
|
async = opts.delete(:async) == true
|
227
302
|
params = opts
|
228
|
-
|
303
|
+
|
304
|
+
#unique value hash cuts down on the number of repeated arguments like in an update or insert statement
|
305
|
+
#by checking if there is an equal existing argument and then using that argument number instead.
|
306
|
+
#If this functionality is used at a lower level we should probably remove this.
|
307
|
+
unique_value_hash = {}
|
308
|
+
|
229
309
|
#allows dynamic sql prepared statements.
|
230
310
|
for mod in name_modifiers
|
231
311
|
name << "_#{mod.to_s}" unless mod.nil?
|
232
312
|
end
|
233
|
-
|
313
|
+
|
234
314
|
unless multi_query
|
235
315
|
#https://stackoverflow.com/questions/49947990/can-i-execute-a-raw-sql-query-leverage-prepared-statements-and-not-use-activer/67442353#67442353
|
236
|
-
|
237
316
|
#change the keys to $1, $2 etc. this step is needed for ex. {id: 1, id_user: 2}.
|
238
317
|
#doing the longer ones first prevents id replacing :id_user -> 1_user
|
239
318
|
keys = params.keys.sort{|a,b| b.to_s.length <=> a.to_s.length}
|
240
|
-
|
319
|
+
sql_vals = []
|
241
320
|
x = 1
|
242
321
|
for key in keys
|
243
322
|
#replace the key with $1, $2 etc
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
323
|
+
v = params[key]
|
324
|
+
|
325
|
+
#this is where we guess what it is
|
326
|
+
looks_like_multi_attribute_array = ((v.class == Array) and (not v.first.nil?) and (v.first.class == Array))
|
327
|
+
|
328
|
+
if v.class == MultiRowExpression or looks_like_multi_attribute_array
|
329
|
+
#it looks like or is a multi-row expression (like those in an insert statement)
|
330
|
+
v = MultiRowExpression.new(v) if looks_like_multi_attribute_array
|
331
|
+
#process into usable information
|
332
|
+
sql_for_replace, mat_vars, new_x = v.for_query(x, unique_value_hash: unique_value_hash)
|
333
|
+
#replace the key with the sql
|
334
|
+
if sql.gsub!(":#{key}", sql_for_replace) != nil
|
335
|
+
#if successful set the new x number and append variables to our sql variables
|
336
|
+
x = new_x
|
337
|
+
name_num = 0
|
338
|
+
mat_vars.each{|mat_var|
|
339
|
+
name_num += 1
|
340
|
+
sql_vals << convert_to_query_attribute("#{key}_#{name_num}", mat_var)
|
341
|
+
}
|
342
|
+
end
|
248
343
|
else
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
# binds = [ ActiveRecord::Relation::QueryAttribute.new(
|
253
|
-
# "id", 6, ActiveRecord::Type::Integer.new
|
254
|
-
# )]
|
255
|
-
# ApplicationRecord.connection.exec_query(
|
256
|
-
# 'SELECT * FROM users WHERE id = $1', 'sql', binds
|
257
|
-
# )
|
258
|
-
v = params[key]
|
259
|
-
type = DB_TYPE_MAPS[v.class]
|
260
|
-
if type.nil?
|
261
|
-
raise StandardError.new("#{v}'s class #{v.class} unsupported type right now for ApplicationRecord#headache_sql")
|
262
|
-
elsif type.class == Proc
|
263
|
-
a = v[0]
|
264
|
-
a.nil? ? a = Integer : a = a.class
|
265
|
-
type = type.call(a)
|
344
|
+
prexist_arg_num = unique_value_hash[v]
|
345
|
+
if prexist_arg_num
|
346
|
+
sql.gsub!(":#{key}", "$#{prexist_arg_num}")
|
266
347
|
else
|
267
|
-
|
348
|
+
if sql.gsub!(":#{key}", "$#{x}") == nil
|
349
|
+
#nothing changed, param not used, delete it
|
350
|
+
params.delete key
|
351
|
+
else
|
352
|
+
unique_value_hash[v] = x
|
353
|
+
sql_vals << convert_to_query_attribute(key, v)
|
354
|
+
x += 1
|
355
|
+
end
|
268
356
|
end
|
269
|
-
|
270
|
-
vals << ActiveRecord::Relation::QueryAttribute.new( key, v, type )
|
271
357
|
end
|
272
|
-
x += 1
|
273
358
|
end
|
274
|
-
ret = ActiveRecord::Base.connection.exec_query sql, name,
|
359
|
+
ret = ActiveRecord::Base.connection.exec_query sql, name, sql_vals, prepare: prepare, async: async
|
275
360
|
else
|
276
361
|
ret = ActiveRecord::Base.connection.execute sql, name
|
277
362
|
end
|
278
|
-
|
363
|
+
|
279
364
|
#this returns a PG::Result object, which is pretty basic. To make this into User/Post/etc objects we do
|
280
365
|
#the following
|
281
366
|
if instantiate_class or self != ApplicationRecord
|
282
367
|
instantiate_class = self if not instantiate_class
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
368
|
+
#no I am not actually this cool see https://stackoverflow.com/questions/30826015/convert-pgresult-to-an-active-record-model
|
369
|
+
fields = ret.columns
|
370
|
+
vals = ret.rows
|
371
|
+
ret = vals.map { |v|
|
372
|
+
instantiate_class.instantiate(Hash[fields.zip(v)])
|
373
|
+
}
|
289
374
|
end
|
290
375
|
ret
|
291
376
|
end
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
377
|
+
|
378
|
+
def quick_safe_increment(id, col, val)
|
379
|
+
where(id: id).update_all("#{col} = #{col} + #{val}")
|
380
|
+
end
|
381
|
+
end
|
296
382
|
|
297
383
|
def list_associations
|
298
384
|
#lists associations (see class method above)
|
@@ -321,11 +407,10 @@ module DynamicRecordsMeritfront
|
|
321
407
|
return self.class.string_as_selector(gidstr, attribute: attribute)
|
322
408
|
end
|
323
409
|
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
410
|
+
#just for ease of use
|
411
|
+
def headache_preload(records, associations)
|
412
|
+
self.class.headache_preload(records, associations)
|
413
|
+
end
|
329
414
|
def safe_increment(col, val) #also used in follow, also used in comment#kill
|
330
415
|
self.class.where(id: self.id).update_all("#{col} = #{col} + #{val}")
|
331
416
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic-records-meritfront
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke Clancy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashid-rails
|