clerk_active_record 0.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/lib/clerk.rb +9 -0
- data/lib/clerk/account.rb +147 -0
- data/lib/clerk/application_record.rb +31 -0
- data/lib/clerk/email.rb +8 -0
- data/lib/clerk/errors.rb +18 -0
- data/lib/clerk/role.rb +88 -0
- data/lib/clerk/session_token.rb +52 -0
- data/lib/clerk_active_record.rb +47 -0
- data/lib/clerk_active_record/patch_persistence.rb +74 -0
- data/lib/clerk_active_record/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 00bb8417dbef561567b058c77406d1a57b16ee2a393834a581ea9c0b5d183cd4
|
4
|
+
data.tar.gz: e2adcb9efe8dd99245a682fd9d553fb654f6e7af53344d25091a2af59ffae65f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b24e85f835dd1d395621a697bfd485267fba0dfdd977a47e894ec6d8fbfb77bf96006ffd128fcad131aeceaac50aeb31b99e2c69d967076e516869d8bfaac938
|
7
|
+
data.tar.gz: b1afc6fdbd7e6b123081d21e7390e80ebc046edfbc575b12e3897d5a070952b65d30a9dcf9eed7c0255aeb37cd4c55be48bf49677bd7ef6f4567741e87ad6d1d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Clerk.dev
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# ClerkActiveRecord
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clerk_active_record`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'clerk_active_record'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install clerk_active_record
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clerk_active_record. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the ClerkActiveRecord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/clerk_active_record/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/clerk.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
module Clerk
|
3
|
+
class Account < Clerk::ApplicationRecord
|
4
|
+
self.table_name = self.clerk_table_name("accounts")
|
5
|
+
self.primary_key = 'id'
|
6
|
+
|
7
|
+
has_many :roles, class_name: "Clerk::Role"
|
8
|
+
has_many :emails, class_name: "Clerk::Email"
|
9
|
+
|
10
|
+
def email(
|
11
|
+
from_email_name:,
|
12
|
+
email_template_token: nil,
|
13
|
+
replacements: nil,
|
14
|
+
subject: nil,
|
15
|
+
body: nil
|
16
|
+
)
|
17
|
+
Clerk::Email.create(
|
18
|
+
account_id: self.id,
|
19
|
+
from_email_name: from_email_name,
|
20
|
+
email_template_token: email_template_token,
|
21
|
+
replacements: replacements,
|
22
|
+
subject: subject,
|
23
|
+
body: body
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.from_session_token(session_token)
|
28
|
+
rsa_public = OpenSSL::PKey::RSA.new ENV["CLERK_SESSION_KEY"]
|
29
|
+
begin
|
30
|
+
decoded_token = ::JWT.decode (session_token), rsa_public, true, { algorithm: 'RS256' }
|
31
|
+
session_id = decoded_token[0]["id"]
|
32
|
+
session = clerk_persistence_api.get("/v1/sessions/#{session_id}").data
|
33
|
+
account = Clerk::Account.new(
|
34
|
+
id: session[:account][:id],
|
35
|
+
email_address: session[:account][:email_address],
|
36
|
+
verified_email_address: session[:account][:verified_email_address]
|
37
|
+
)
|
38
|
+
account.instance_variable_set(:@new_record, false)
|
39
|
+
account.clear_changes_information
|
40
|
+
account
|
41
|
+
rescue => e
|
42
|
+
puts "Failed to find account from session token"
|
43
|
+
puts e
|
44
|
+
return nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_clerk_role(role_type_symbol, instance = nil)
|
49
|
+
json = {}
|
50
|
+
json[:name] = role_type_symbol.to_s
|
51
|
+
json[:account_id] = self.id
|
52
|
+
|
53
|
+
if not instance.nil?
|
54
|
+
json[:scope_class] = instance.class.name
|
55
|
+
json[:scope_id] = instance.id
|
56
|
+
end
|
57
|
+
|
58
|
+
server_url = "#{Clerk.accounts_url}/roles"
|
59
|
+
HTTP.auth("Bearer #{Clerk.key}").post(server_url, :json => json)
|
60
|
+
end
|
61
|
+
|
62
|
+
def has_role?(role, scope)
|
63
|
+
roles.where(name: role, scope_class: scope.class.name, scope_id: scope.id).exists?
|
64
|
+
end
|
65
|
+
|
66
|
+
def roles_for(scope)
|
67
|
+
roles.where(scope_class: scope.class.name, scope_id: scope.id).pluck(:name).map(&:to_sym)
|
68
|
+
end
|
69
|
+
|
70
|
+
def has_permission?(permission, scope)
|
71
|
+
has_role?(scope.class.roles_with_permission(permission), scope)
|
72
|
+
end
|
73
|
+
|
74
|
+
def permissions_for(scope)
|
75
|
+
permissions = Set.new
|
76
|
+
|
77
|
+
roles = roles_for(scope)
|
78
|
+
roles.each do |role|
|
79
|
+
role_permissions = scope.class.clerk_permissions_map[role]
|
80
|
+
|
81
|
+
unless role_permissions.nil?
|
82
|
+
permissions.merge(role_permissions)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
return permissions.to_a
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def method_missing(method_name, *args, &block)
|
92
|
+
# Rails lazy loads modules. If an object hasn't been loaded yet, any inverse association
|
93
|
+
# will not be here yet. Just in case, load the constant here and re-call the method to
|
94
|
+
# before raising an error
|
95
|
+
@miss_test ||= {}
|
96
|
+
if @miss_test.has_key? method_name.to_sym
|
97
|
+
super
|
98
|
+
else
|
99
|
+
@miss_test[method_name.to_sym] = true
|
100
|
+
scope_class = method_name.to_s.classify.constantize
|
101
|
+
send(method_name, *args, &block)
|
102
|
+
end
|
103
|
+
rescue => e
|
104
|
+
super
|
105
|
+
end
|
106
|
+
|
107
|
+
class RolesWrapper
|
108
|
+
def initialize(instance, target)
|
109
|
+
@instance = instance
|
110
|
+
@target_class = target.to_s.classify.constantize
|
111
|
+
end
|
112
|
+
|
113
|
+
def with(role: nil, permission: nil)
|
114
|
+
if (role.nil? and permission.nil?) or (not role.nil? and not permission.nil?)
|
115
|
+
raise ArgumentError.new("Invalid argument, must supply either a role or permission")
|
116
|
+
end
|
117
|
+
|
118
|
+
if not role.nil?
|
119
|
+
return @target_class.where(
|
120
|
+
id: @instance.roles.where(scope_class: @target_class.name, name: role).pluck(:scope_id)
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
if not permission.nil?
|
125
|
+
roles = @target_class.roles_with_permission(permission)
|
126
|
+
return @target_class.where(
|
127
|
+
id: @instance.roles.where(scope_class: @target_class.name, name: roles).pluck(:scope_id)
|
128
|
+
)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def no_with
|
133
|
+
@target_class.where(
|
134
|
+
id: @instance.roles.where(scope_class: @target_class.name).pluck(:scope_id)
|
135
|
+
)
|
136
|
+
end
|
137
|
+
|
138
|
+
def method_missing(m, *args, &block)
|
139
|
+
no_with.send(m, *args, &block)
|
140
|
+
end
|
141
|
+
|
142
|
+
def inspect
|
143
|
+
no_with.inspect
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Clerk
|
2
|
+
class ApplicationRecord < ActiveRecord::Base
|
3
|
+
self.abstract_class = true
|
4
|
+
puts "ugh"
|
5
|
+
puts ENV["CLERK_DATABASE_URL"]
|
6
|
+
puts "go"
|
7
|
+
establish_connection ENV["CLERK_DATABASE_URL"]
|
8
|
+
def self.clerk_table_name(table_name)
|
9
|
+
"#{table_name}_01"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.clerk_persistence_path
|
13
|
+
"/v1/#{table_name[0...-3]}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.clerk_persistence_api
|
17
|
+
@@clerk_persistence_api ||= ClerkActiveRecord::Api::Connection.new(
|
18
|
+
(ENV["CLERK_API_PATH"] || "https://api.clerk.dev"),
|
19
|
+
ENV["CLERK_KEY"]
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.transaction(options = {}, &block)
|
24
|
+
# were overriding all the methods that need transactions
|
25
|
+
# these interfere with pgbouncer. just yield the block
|
26
|
+
yield
|
27
|
+
rescue ActiveRecord::Rollback
|
28
|
+
# rollbacks are silently swallowed
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/clerk/email.rb
ADDED
data/lib/clerk/errors.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Clerk
|
2
|
+
module Errors
|
3
|
+
class ClerkError < StandardError; end
|
4
|
+
|
5
|
+
class ClerkServerError < ClerkError
|
6
|
+
def initialize(message = nil, model = nil)
|
7
|
+
@model = model
|
8
|
+
super(message)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class MethodDisabled < ClerkError
|
13
|
+
def initialize(message = nil)
|
14
|
+
super(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/clerk/role.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module Clerk
|
2
|
+
class Role < Clerk::ApplicationRecord
|
3
|
+
self.table_name = self.clerk_table_name("roles")
|
4
|
+
self.primary_key = 'id'
|
5
|
+
|
6
|
+
belongs_to :account, class_name: "Clerk::Account"
|
7
|
+
|
8
|
+
# # We need to migrate roles over to the new persistence strategy
|
9
|
+
# def self.clerk_persistence_path
|
10
|
+
# nil
|
11
|
+
# end
|
12
|
+
|
13
|
+
# def self.clerk_persistence_api
|
14
|
+
# nil
|
15
|
+
# end
|
16
|
+
|
17
|
+
# def self._insert_record(values)
|
18
|
+
# begin
|
19
|
+
# response = Clerk::ApplicationRecord.clerk_persistence_api.post('/roles', values)
|
20
|
+
# id = JSON.parse(response.body)["id"]
|
21
|
+
# rescue
|
22
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to save the role", self)
|
23
|
+
# end
|
24
|
+
|
25
|
+
# if response.status != 200 or id.nil?
|
26
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to save the role", self)
|
27
|
+
# end
|
28
|
+
|
29
|
+
# return id
|
30
|
+
# end
|
31
|
+
|
32
|
+
# def self._update_record(values, constraints)
|
33
|
+
# update_id = constraints["id"]
|
34
|
+
# if update_id.nil?
|
35
|
+
# raise ActiveRecord::RecordNotFound.new("Must pass an id to update a role", self)
|
36
|
+
# end
|
37
|
+
|
38
|
+
# begin
|
39
|
+
# response = Clerk.api.patch("/roles/#{update_id}", values.merge!(constraints))
|
40
|
+
# rescue
|
41
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to update the role", self)
|
42
|
+
# end
|
43
|
+
|
44
|
+
# if response.status != 200
|
45
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to update the role", self)
|
46
|
+
# end
|
47
|
+
|
48
|
+
# # overriden function returns the number of rows affected
|
49
|
+
# return 1
|
50
|
+
# end
|
51
|
+
|
52
|
+
# def self._delete_record(constraints)
|
53
|
+
# delete_id = constraints["id"]
|
54
|
+
# if delete_id.nil?
|
55
|
+
# raise ActiveRecord::RecordNotFound.new("Must pass an id to delete a role", self)
|
56
|
+
# end
|
57
|
+
|
58
|
+
# begin
|
59
|
+
# response = Clerk.api.delete("/roles/#{delete_id}")
|
60
|
+
# rescue
|
61
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to delete the role", self)
|
62
|
+
# end
|
63
|
+
|
64
|
+
|
65
|
+
# if response.status != 200
|
66
|
+
# raise Clerk::Errors::ClerkServerError.new("Failed to delete the role", self)
|
67
|
+
# end
|
68
|
+
|
69
|
+
# # overriden function returns the number of rows affected
|
70
|
+
# return 1
|
71
|
+
# end
|
72
|
+
|
73
|
+
|
74
|
+
# # disable bulk calls
|
75
|
+
# def self.update_all(updates)
|
76
|
+
# raise Clerk::Errors::MethodDisabled.new("update_all is disabled for Clerk::Role")
|
77
|
+
# end
|
78
|
+
|
79
|
+
# def self.delete_all
|
80
|
+
# raise Clerk::Errors::MethodDisabled.new("delete_all is disabled for Clerk::Role")
|
81
|
+
# end
|
82
|
+
|
83
|
+
# def self.destroy_all
|
84
|
+
# raise Clerk::Errors::MethodDisabled.new("destroy_all is disabled for Clerk::Role")
|
85
|
+
# end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Clerk
|
2
|
+
class SessionToken < Clerk::ApplicationRecord
|
3
|
+
self.table_name = self.clerk_table_name("session_tokens")
|
4
|
+
self.primary_key = 'id'
|
5
|
+
|
6
|
+
belongs_to :account, class_name: "Clerk::Account"
|
7
|
+
|
8
|
+
def self.find_account(cookie:)
|
9
|
+
require "bcrypt"
|
10
|
+
begin
|
11
|
+
id, token, token_hash = decrypt(cookie).split("--")
|
12
|
+
if BCrypt::Password.new(token_hash) == token
|
13
|
+
Account.joins(:session_token).where(token_hash: token_hash, )
|
14
|
+
SessionToken.eager_load(:account).find_by!(id: id, token_hash: token_hash)&.account
|
15
|
+
else
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
rescue => e
|
19
|
+
puts "Error finding acount #{e}"
|
20
|
+
puts "Cookie #{cookie}"
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.cipher_key
|
28
|
+
@@cipher_key ||= ::Base64.strict_decode64(ENV["CLERK_CIPHER_KEY"])
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.decrypt(encrypted_message)
|
32
|
+
cipher = OpenSSL::Cipher.new("aes-256-gcm")
|
33
|
+
|
34
|
+
encrypted_data, iv, auth_tag = encrypted_message.split("--".freeze).map { |v| ::Base64.strict_decode64(v) }
|
35
|
+
|
36
|
+
# Currently the OpenSSL bindings do not raise an error if auth_tag is
|
37
|
+
# truncated, which would allow an attacker to easily forge it. See
|
38
|
+
# https://github.com/ruby/openssl/issues/63
|
39
|
+
raise InvalidMessage if (auth_tag.nil? || auth_tag.bytes.length != 16)
|
40
|
+
|
41
|
+
cipher.decrypt
|
42
|
+
cipher.key = cipher_key
|
43
|
+
cipher.iv = iv
|
44
|
+
cipher.auth_tag = auth_tag
|
45
|
+
cipher.auth_data = ""
|
46
|
+
|
47
|
+
message = cipher.update(encrypted_data)
|
48
|
+
message << cipher.final
|
49
|
+
message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "clerk_active_record/version"
|
2
|
+
require "clerk_active_record/patch_persistence"
|
3
|
+
require "clerk"
|
4
|
+
|
5
|
+
module ClerkActiveRecord
|
6
|
+
module Api
|
7
|
+
class Connection
|
8
|
+
def initialize(url, auth_token)
|
9
|
+
@c = Faraday.new(:url => url) do |conn|
|
10
|
+
conn.request :url_encoded
|
11
|
+
conn.authorization :Bearer, auth_token
|
12
|
+
conn.adapter Faraday.default_adapter
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(path, fields, &block)
|
17
|
+
ClerkActiveRecord::Api::Response.new(@c.post(path, fields.as_json, &block))
|
18
|
+
end
|
19
|
+
|
20
|
+
def patch(path, fields, &block)
|
21
|
+
ClerkActiveRecord::Api::Response.new(@c.patch(path, fields.as_json, &block))
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete(path, fields, &block)
|
25
|
+
ClerkActiveRecord::Api::Response.new(@c.delete(path, fields.as_json, &block))
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(*args, &block)
|
29
|
+
ClerkActiveRecord::Api::Response.new(@c.get(*args, &block))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Response
|
34
|
+
def initialize(faraday_response)
|
35
|
+
@res = faraday_response
|
36
|
+
end
|
37
|
+
|
38
|
+
def data
|
39
|
+
JSON.parse(@res.body, symbolize_names: true)
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(m, *args, &block)
|
43
|
+
@res.send(m, *args, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
::ActiveRecord::Persistence.module_eval do
|
2
|
+
alias_method :_original_create_record, :_create_record
|
3
|
+
|
4
|
+
def _clerk_create_record(attribute_names = self.attribute_names)
|
5
|
+
# Send attributes on the database plus any custom attributes to the API
|
6
|
+
attribute_names = attributes_for_create(attribute_names)
|
7
|
+
attribute_names += (attributes.keys - self.class.columns_hash.keys)
|
8
|
+
|
9
|
+
values = attributes_with_values(attribute_names)
|
10
|
+
|
11
|
+
response = self.class.clerk_persistence_api.post(self.class.clerk_persistence_path, values)
|
12
|
+
|
13
|
+
if response.status == 200
|
14
|
+
new_id = JSON.parse(response.body)["id"]
|
15
|
+
elsif response.status == 422
|
16
|
+
response.data.each do |k, vs|
|
17
|
+
vs.each do |v|
|
18
|
+
self.errors.add(k, v)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
raise ActiveRecord::RecordInvalid.new(self)
|
22
|
+
else
|
23
|
+
Rails.logger.fatal "Server Failed: #{response.data[:message]}"
|
24
|
+
raise ActiveRecord::RecordNotSaved.new("Failed to save the server record", self)
|
25
|
+
end
|
26
|
+
|
27
|
+
self.id ||= new_id if self.class.primary_key
|
28
|
+
|
29
|
+
@new_record = false
|
30
|
+
|
31
|
+
yield(self) if block_given?
|
32
|
+
|
33
|
+
id
|
34
|
+
end
|
35
|
+
|
36
|
+
def _create_record(*args, &block)
|
37
|
+
if defined?(self.class.clerk_persistence_path) and !self.class.clerk_persistence_path.nil?
|
38
|
+
_clerk_create_record(*args, &block)
|
39
|
+
else
|
40
|
+
_original_create_record(*args, &block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
alias_method :_original_update_row, :_update_row
|
45
|
+
|
46
|
+
def _clerk_update_row(attribute_names, attempted_action = "update")
|
47
|
+
response = self.class.clerk_persistence_api.patch(
|
48
|
+
"#{self.class.clerk_persistence_path}/#{id_in_database}",
|
49
|
+
attributes_with_values(attribute_names).except("updated_at")
|
50
|
+
)
|
51
|
+
|
52
|
+
if response.status == 200
|
53
|
+
1 # Affected rows
|
54
|
+
elsif response.status == 422
|
55
|
+
response.data.each do |k, vs|
|
56
|
+
vs.each do |v|
|
57
|
+
self.errors.add(k, v)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
raise ActiveRecord::RecordInvalid.new(self)
|
61
|
+
else
|
62
|
+
Rails.logger.fatal "Server Failed: #{response.data[:message]}"
|
63
|
+
raise ActiveRecord::RecordNotSaved.new("Failed to save the server record", self)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def _update_row(*args, &block)
|
68
|
+
if defined?(self.class.clerk_persistence_path)
|
69
|
+
_clerk_update_row(*args, &block)
|
70
|
+
else
|
71
|
+
_original_update_row(*args, &block)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clerk_active_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Sidoti
|
8
|
+
- Braden Sidoti
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2019-06-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 5.2.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 5.2.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: pg
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: jwt
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
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: openssl
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description:
|
71
|
+
email:
|
72
|
+
- hello@clerk.dev
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- lib/clerk.rb
|
80
|
+
- lib/clerk/account.rb
|
81
|
+
- lib/clerk/application_record.rb
|
82
|
+
- lib/clerk/email.rb
|
83
|
+
- lib/clerk/errors.rb
|
84
|
+
- lib/clerk/role.rb
|
85
|
+
- lib/clerk/session_token.rb
|
86
|
+
- lib/clerk_active_record.rb
|
87
|
+
- lib/clerk_active_record/patch_persistence.rb
|
88
|
+
- lib/clerk_active_record/version.rb
|
89
|
+
homepage:
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.7.8
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Configures Clerk models for ActiveRecord
|
113
|
+
test_files: []
|