querylet-rails 1.1.0 → 1.2.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
2
  SHA256:
3
- metadata.gz: 8b538b6b0c79fc05704c812ae0efadc9abba452fbe9a4a528918367149ff7ce0
4
- data.tar.gz: 911f27123fc5ed36dc960c16c50241cc4b66419d5e2f344586cf95bdb70edd43
3
+ metadata.gz: 302e8d10adf702f7225a143ff065b6db8bc67a126371f3f84e3113cf781a356d
4
+ data.tar.gz: 2e9b82dbbf9cc7d016a94392c8e75c5e22060fa6f362b80f1c65cad86f15962f
5
5
  SHA512:
6
- metadata.gz: 491d161d381af74c0ffbfcb9be6409ee83653a5897cba99378902f12a4ebec9a2d8bedfdec7157cb59726de265be07369e05d5f38befd84fece409513c160dbd
7
- data.tar.gz: 8df9b821f1eeecf603c072a6a2104054340617c36a313628b1acdf541f2e9c02f391e70ca1e4653c22d5f4dbee24fcf88e22f7297b6b3b5ebfe0fe4413f8e137
6
+ metadata.gz: f4c915bf73424ae790eddada5e967e2bb02037071ab00b76f40c4e30a444740374d7b3f830ea03994bee5f8a8ec9fdf4b32faa1804a379022f90e106d379b914
7
+ data.tar.gz: 35490fe8cde047492b1ff02d09bb8d15ef29e00575fe03138cf7a63c92b5c185b90d7eb1e90a5ad3743d796139578207440ee04e54ec7be8132fae531740a45c
@@ -4,7 +4,6 @@ module QueryletRails
4
4
  module Model
5
5
  module Queryable
6
6
  extend ActiveSupport::Concern
7
- # override this function
8
7
 
9
8
  def query relative_path, data={}
10
9
  self.class.query relative_path, data
@@ -34,18 +33,74 @@ module QueryletRails
34
33
  self.class.select_paginate query, attrs, count
35
34
  end
36
35
 
37
- module ClassMethods
36
+ # This will fetch the current tenant id
37
+ def current_tenant_id
38
+ self.class.current_tenant_id
39
+ end
40
+
41
+ def set_tenant_id
42
+ self.class.set_tenant_id
43
+ end
44
+
45
+ def reset_tenant_id
46
+ self.class.reset_tenant_id
47
+ end
48
+
49
+ # This will apply the current tenant_id on the creation of a new model
50
+ def initialize_tenant_id
51
+ if new_record?
52
+ if has_attribute?(:tenant_id) && self.tenant_id.nil?
53
+ self.tenant_id = current_tenant_id
54
+ end
55
+ end
56
+ end
57
+
58
+ included do
59
+ after_initialize :initialize_tenant_id
60
+ end
61
+
62
+ class_methods do
63
+ # override this function
38
64
  def query_root
39
65
  Rails.root
40
66
  end
41
67
 
42
- def query relative_path, data={}
68
+ def _query_wrap_object template
69
+ <<-HEREDOC.chomp
70
+ (SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
71
+ #{template.to_s.chomp}
72
+ ) object_row);
73
+ HEREDOC
74
+ end
75
+
76
+ def _query_wrap_array template
77
+ <<-HEREDOC.chomp
78
+ (SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
79
+ #{template.to_s.chomp}
80
+ ) array_row);
81
+ HEREDOC
82
+ end
83
+
84
+ def current_tenant_id
85
+ self.connection.execute('SHOW my.tenant_id').getvalue(0, 0).to_i
86
+ end
87
+
88
+ def set_tenant_id new_tenant_id
89
+ raise "tenant_id not an valid value" unless new_tenant_id.is_a?(Integer)
90
+ self.connection.execute "SET SESSION my.tenant_id = #{new_tenant_id}"
91
+ end
92
+
93
+ def reset_tenant_id
94
+ self.connection.execute "RESET my.tenant_id"
95
+ end
96
+
97
+ def _query relative_path, data={}
43
98
  file_path = self.query_root.join('app','queries',relative_path + '.sql')
44
99
  template = File.read file_path
45
- query_compile_template template, data
100
+ _query_compile_template template, data
46
101
  end
47
102
 
48
- def query_compile_template template, data={}
103
+ def _query_compile_template template, data={}
49
104
  querylet = Querylet::Querylet.new path: self.query_root.join('app','queries').to_s
50
105
  begin
51
106
  querylet.compile(template).call(data)
@@ -61,59 +116,47 @@ module QueryletRails
61
116
  end
62
117
  end
63
118
 
119
+ def query relative_path, data={}
120
+ _query relative_path, data
121
+ end
122
+
64
123
  def select_value relative_path, data={}
65
- sql = self.query relative_path, data
124
+ sql = _query relative_path, data
66
125
  self.connection.select_value sql
67
126
  end
68
127
 
69
128
  def select_values relative_path, data={}
70
- sql = self.query relative_path, data
129
+ sql = _query relative_path, data
71
130
  self.connection.select_values sql
72
131
  end
73
132
 
74
133
  def select_array relative_path, data={}
75
- template = self.query relative_path, data
76
- sql = query_wrap_array template
134
+ sql = _query relative_path, data
135
+ sql = self._query_wrap_array sql
77
136
  self.connection.select_value sql
78
137
  end
79
138
 
80
139
  def select_object relative_path, data={}
81
- template = self.query relative_path, data
82
- sql = query_wrap_object template
140
+ sql = _query relative_path, data
141
+ sql = self._query_wrap_object sql
83
142
  self.connection.select_value sql
84
143
  end
85
144
 
86
145
  def select_all relative_path, data={}
87
- sql = self.query relative_path, data
146
+ sql = _query relative_path, data
88
147
  self.connection.select_all sql
89
148
  end
90
149
 
91
- def query_wrap_object template
92
- <<-HEREDOC.chomp
93
- (SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
94
- #{template.to_s.chomp}
95
- ) object_row)
96
- HEREDOC
97
- end
98
-
99
- def query_wrap_array template
100
- <<-HEREDOC.chomp
101
- (SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
102
- #{template.to_s.chomp}
103
- ) array_row)
104
- HEREDOC
105
- end
106
-
107
150
  def select_paginate relative_path, data, count
108
151
  file_path = self.query_root.join('app','queries',relative_path + '.sql')
109
152
  template = File.read file_path
110
153
  if count
111
- sql = self.query_wrap_object template
112
- sql = self.query_compile_template sql, data
154
+ sql = self._query_wrap_object template
155
+ sql = self._query_compile_template sql, data
113
156
  self.connection.select_value sql
114
157
  else
115
- sql = self.query_wrap_array template
116
- sql = self.query_compile_template sql, data
158
+ sql = self._query_wrap_array template
159
+ sql = self._query_compile_template sql, data
117
160
  self.connection.select_value sql
118
161
  end
119
162
  end
@@ -1,3 +1,3 @@
1
1
  module QueryletRails
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querylet-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TeacherSeat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-06 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: querylet
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.1.2
85
+ rubygems_version: 3.1.4
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Querylet Rails