clerk-rails 0.1.7 → 0.1.8

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: 8fc72472099f48dfd7b073a34b63f3e2586c005c5d33153dd2c387fe45316902
4
- data.tar.gz: fe08b5909129f27e517c523f571d83507bf573064a528b6c77da4227e0c58001
3
+ metadata.gz: 5ebd7434ec6b1e5d225ac7de149693f061e1204ca473ad4e17a5bfea5868a489
4
+ data.tar.gz: 5c75c18dad6e96ae75ef990eea3a1f37673153d91bc7c47b6885cc6d2a0ef885
5
5
  SHA512:
6
- metadata.gz: 7753b548f6b4ebad847b51756430511750e94b2dcc58fd38e94192377be8c21efa5051a2032febda1efa002ce63f5647d6e80d2990c0baf6a151390dbea725ca
7
- data.tar.gz: 408dd88b31dd599b73530782874192a1cceb5adbea9521afc179989542575430e62b1fb700ed4b7f94ae0baa2048a4b56b8c0ae1014ea9f4f77d770f16a4b960
6
+ metadata.gz: 9b10930865c7a58a193845a2e5108915801142fb0172ea877e575481f9d44cfa8fd440bc30f7d15b8209166d06f1db6286f0721818a3f5ef39fb13b2904d1840
7
+ data.tar.gz: ec883cfa162d37fbf9aace0db5a43d011dd21ddc2dfd739a23b4e829054e1cb5bf54aeda39e865ba37ba84cc285c01635557a729a7282eb1cb507155f37438a7
@@ -0,0 +1 @@
1
+ ::ActionController::Base.send :include, Clerk::ApplicationHelper
@@ -0,0 +1,55 @@
1
+ [::ActiveRecord::Base].each do |base|
2
+ base.class_eval do
3
+
4
+ def self.clerk(association_name, permissions: [])
5
+ plural_role = association_name.to_s.pluralize.to_sym
6
+ singular_role = association_name.to_s.singularize.to_sym
7
+
8
+ roles_association = :"clerk_roles_#{association_name}"
9
+
10
+ remote_class = self.name
11
+
12
+ unless self.respond_to? :clerk_roles
13
+ self.class_eval do
14
+ include Clerk::Clerked
15
+ end
16
+ end
17
+
18
+ has_many roles_association,
19
+ ->{ where(scope_class: remote_class, name: singular_role) },
20
+ class_name: "Clerk::Role",
21
+ foreign_key: :scope_id
22
+
23
+ plural_role = association_name.to_s.pluralize.to_sym
24
+ singular_role = association_name.to_s.singularize.to_sym
25
+
26
+ account_accessor = self.name.underscore.pluralize.to_sym
27
+
28
+ # Add clerk permissions to `self` the associated class
29
+ self.clerk_permissions_map[singular_role] = permissions
30
+
31
+ # Add magic methods to Clerk::Acount
32
+ Clerk::Account.class_eval do
33
+ define_method account_accessor do
34
+ self.class::RolesWrapper.new(self, account_accessor)
35
+ end
36
+
37
+ define_method :"is_#{singular_role}?" do |*args|
38
+ has_role?(singular_role, *args)
39
+ end
40
+
41
+ permissions.each do |permission|
42
+ define_method :"can_#{permission}?" do |*args|
43
+ has_permission?(permission, *args)
44
+ end
45
+ end
46
+ end
47
+
48
+ [
49
+ association_name.to_sym,
50
+ through: roles_association,
51
+ source: :account
52
+ ]
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,41 @@
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
+ attribute_names = attributes_for_create(attribute_names)
6
+
7
+ values = attributes_with_values(attribute_names)
8
+
9
+ response = self.class.clerk_persistence_api.post(self.class.clerk_persistence_path, values)
10
+
11
+ if response.status == 200
12
+ new_id = JSON.parse(response.body)["id"]
13
+ elsif response.status == 422
14
+ response.data.each do |k, vs|
15
+ vs.each do |v|
16
+ self.errors.add(k, v)
17
+ end
18
+ end
19
+ raise ActiveRecord::RecordInvalid.new(self)
20
+ else
21
+ Rails.logger.fatal "Server Failed: #{response.data[:message]}"
22
+ raise ActiveRecord::RecordNotSaved.new("Failed to save the server record", self)
23
+ end
24
+
25
+ self.id ||= new_id if self.class.primary_key
26
+
27
+ @new_record = false
28
+
29
+ yield(self) if block_given?
30
+
31
+ id
32
+ end
33
+
34
+ def _create_record(*args, &block)
35
+ if defined?(self.class.clerk_persistence_path)
36
+ _clerk_create_record(*args, &block)
37
+ else
38
+ _original_create_record(*args, &block)
39
+ end
40
+ end
41
+ end
data/lib/clerk-rails.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "clerk/version"
2
+ require "clerk/api"
2
3
  require "clerk/engine"
3
4
  require "clerk/id"
4
5
  require "faraday"
@@ -6,7 +7,6 @@ require "faraday"
6
7
  module Clerk
7
8
  class << self
8
9
  attr_reader :config
9
- attr_reader :api
10
10
 
11
11
  def configure
12
12
  @config = Configuration.new
@@ -27,7 +27,10 @@ module Clerk
27
27
  end
28
28
 
29
29
  def api
30
- @conn ||= Clerk::ApiConnection.new
30
+ @conn ||= Clerk::Api::Connection.new(
31
+ (ENV["CLERK_API_PATH"] || "https://accounts.clerkdev.com"),
32
+ key_secret
33
+ )
31
34
  end
32
35
 
33
36
  def key
@@ -47,46 +50,6 @@ module Clerk
47
50
  end
48
51
  end
49
52
 
50
- class ApiConnection
51
- def initialize
52
- @c = Faraday.new(:url => (ENV["CLERK_API_PATH"] || "https://clerk-server-prod-test.herokuapp.com")) do |conn|
53
- conn.authorization :Bearer, Clerk.key_secret
54
- conn.headers['Content-Type'] = 'application/json'
55
- conn.adapter Faraday.default_adapter
56
- end
57
- end
58
-
59
- def post(path, fields, &block)
60
- Clerk::ApiResponse.new(@c.post(path, fields.to_json, &block))
61
- end
62
-
63
- def patch(path, fields, &block)
64
- Clerk::ApiResponse.new(@c.patch(path, fields.to_json, &block))
65
- end
66
-
67
- def delete(path, fields, &block)
68
- Clerk::ApiResponse.new(@c.delete(path, fields.to_json, &block))
69
- end
70
-
71
- def get(*args, &block)
72
- Clerk::ApiResponse.new(@c.get(*args, &block))
73
- end
74
- end
75
-
76
- class ApiResponse
77
- def initialize(faraday_response)
78
- @res = faraday_response
79
- end
80
-
81
- def data
82
- JSON.parse(@res.body, symbolize_names: true)
83
- end
84
-
85
- def method_missing(m, *args, &block)
86
- @res.send(m, *args, &block)
87
- end
88
- end
89
-
90
53
  class Configuration
91
54
  attr_accessor :app_host, :accounts_host, :database_url, :tunnel_key, :environment, :session_mode, :last_account, :cookie_host
92
55
  end
data/lib/clerk/api.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Clerk
2
+ module Api
3
+ class Connection
4
+ def initialize(url, auth_token)
5
+ @c = Faraday.new(:url => url) do |conn|
6
+ conn.authorization :Bearer, auth_token
7
+ conn.headers['Content-Type'] = 'application/json'
8
+ conn.adapter Faraday.default_adapter
9
+ end
10
+ end
11
+
12
+ def post(path, fields, &block)
13
+ Clerk::Api::Response.new(@c.post(path, fields.to_json, &block))
14
+ end
15
+
16
+ def patch(path, fields, &block)
17
+ Clerk::Api::Response.new(@c.patch(path, fields.to_json, &block))
18
+ end
19
+
20
+ def delete(path, fields, &block)
21
+ Clerk::Api::Response.new(@c.delete(path, fields.to_json, &block))
22
+ end
23
+
24
+ def get(*args, &block)
25
+ Clerk::Api::Response.new(@c.get(*args, &block))
26
+ end
27
+ end
28
+
29
+ class Response
30
+ def initialize(faraday_response)
31
+ @res = faraday_response
32
+ end
33
+
34
+ def data
35
+ JSON.parse(@res.body, symbolize_names: true)
36
+ end
37
+
38
+ def method_missing(m, *args, &block)
39
+ @res.send(m, *args, &block)
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/clerk/engine.rb CHANGED
@@ -15,8 +15,8 @@ module Clerk
15
15
  Clerk.configure do |config|
16
16
  config.app_host = config_data[:APP_HOST]
17
17
  config.accounts_host = config_data[:ACCOUNTS_HOST]
18
- config.tunnel_key = config_data[:TUNNEL_KEY]
19
18
  config.database_url = config_data[:DATABASE_URL]
19
+ config.tunnel_key = config_data[:TUNNEL_KEY]
20
20
  end
21
21
  rescue => e
22
22
  raise "Clerk could not be loaded. #{config_data}"
@@ -24,69 +24,8 @@ module Clerk
24
24
 
25
25
  if defined?(Rails::Server) and Rails.env.development?
26
26
  require 'clerk/tunnel'
27
- if !Clerk::Tunnel.ready?
28
- Clerk::Tunnel.setup!
29
- end
30
27
  Clerk::Tunnel.start!
31
28
  end
32
29
  end
33
-
34
- initializer "clerk.load_helpers" do
35
- ::ActionController::Base.send :include, Clerk::ApplicationHelper
36
- ::ActiveRecord::Base.class_eval do
37
-
38
- def self.clerk(association_name, permissions: [])
39
- plural_role = association_name.to_s.pluralize.to_sym
40
- singular_role = association_name.to_s.singularize.to_sym
41
-
42
- roles_association = :"clerk_roles_#{association_name}"
43
-
44
- remote_class = self.name
45
-
46
- unless self.respond_to? :clerk_roles
47
- self.class_eval do
48
- include Clerk::Clerked
49
- end
50
- end
51
-
52
- has_many roles_association,
53
- ->{ where(scope_class: remote_class, name: singular_role) },
54
- class_name: "Clerk::Role",
55
- foreign_key: :scope_id
56
-
57
- plural_role = association_name.to_s.pluralize.to_sym
58
- singular_role = association_name.to_s.singularize.to_sym
59
-
60
- account_accessor = self.name.underscore.pluralize.to_sym
61
-
62
- # Add clerk permissions to `self` the associated class
63
- self.clerk_permissions_map[singular_role] = permissions
64
-
65
- # Add magic methods to Clerk::Acount
66
- Clerk::Account.class_eval do
67
- define_method account_accessor do
68
- self.class::RolesWrapper.new(self, account_accessor)
69
- end
70
-
71
- define_method :"is_#{singular_role}?" do |*args|
72
- has_role?(singular_role, *args)
73
- end
74
-
75
- permissions.each do |permission|
76
- define_method :"can_#{permission}?" do |*args|
77
- has_permission?(permission, *args)
78
- end
79
- end
80
- end
81
-
82
- [
83
- association_name.to_sym,
84
- through: roles_association,
85
- source: :account
86
- ]
87
- end
88
- end
89
- end
90
-
91
30
  end
92
31
  end
data/lib/clerk/tunnel.rb CHANGED
@@ -36,8 +36,8 @@ module Clerk
36
36
  end
37
37
 
38
38
  def self.setup!
39
- setup_crt! unless self.crt_ready?
40
- setup_ngrok! unless self.ngrok_ready?
39
+ setup_crt! unless crt_ready?
40
+ setup_ngrok! unless ngrok_ready?
41
41
  puts "=> [Clerk] Setup done."
42
42
  end
43
43
 
@@ -145,6 +145,8 @@ module Clerk
145
145
  end
146
146
 
147
147
  def self.start!
148
+ setup! unless ready?
149
+
148
150
  self.patch_rack_requests
149
151
 
150
152
  server = ObjectSpace.each_object(Rails::Server).first
data/lib/clerk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clerk
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clerk-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clerk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-30 00:00:00.000000000 Z
11
+ date: 2019-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -150,8 +150,12 @@ files:
150
150
  - app/models/concerns/clerk/clerked.rb
151
151
  - app/models/concerns/clerk/errors.rb
152
152
  - app/views/layouts/clerk/application.html.erb
153
+ - config/initializers/add_application_helpers.rb
154
+ - config/initializers/add_clerk_roles.rb
155
+ - config/initializers/patch_persistence.rb
153
156
  - config/routes.rb
154
157
  - lib/clerk-rails.rb
158
+ - lib/clerk/api.rb
155
159
  - lib/clerk/engine.rb
156
160
  - lib/clerk/id.rb
157
161
  - lib/clerk/tunnel.rb