ncore 2.3.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,6 @@ module NCore
9
9
  class AccessDenied < Error ; end
10
10
  class AccountInactive < Error ; end
11
11
  class AuthenticationFailed < Error ; end
12
- class CertificateError < Error ; end
13
12
  class ConnectionError < Error ; end
14
13
  class RateLimited < Error ; end
15
14
  class RecordNotFound < Error ; end
@@ -24,9 +23,13 @@ module NCore
24
23
  cl_name ||= object.class.class_name if object.class.respond_to?(:class_name)
25
24
  cl_name ||= object.name if object.respond_to?(:name)
26
25
  cl_name ||= object.class.name
27
- msg = "\#{cl_name} Invalid: \#{@object.errors.to_a.join(' ')}"
26
+ msg = "\#{cl_name} Invalid: \#{object.errors.to_a.join(' ')}"
28
27
  super msg
29
28
  end
29
+
30
+ def errors
31
+ object&.errors
32
+ end
30
33
  end
31
34
 
32
35
  class QueryError < Error
@@ -40,6 +43,8 @@ module NCore
40
43
  end
41
44
 
42
45
  class BulkActionError < QueryError ; end
46
+
47
+ # DEPRECATED; to be removed in v3.1
43
48
  class ValidationError < QueryError ; end
44
49
  INCL
45
50
  end
@@ -15,9 +15,36 @@ module NCore
15
15
  class_name.underscore
16
16
  end
17
17
  alias :json_root :attrib_name
18
+
19
+ def i18n_scope
20
+ module_parent::Api.i18n_scope
21
+ end
22
+
23
+ # make NCore::SomeResource.model_name do the right thing
24
+ def model_name
25
+ ::ActiveModel::Name.new(self, nil, ::ActiveSupport::Inflector.demodulize(self))
26
+ end
27
+
18
28
  end
19
29
 
20
30
 
31
+ # from ActiveRecord::Core
32
+ def ==(comparison_object)
33
+ super ||
34
+ comparison_object.instance_of?(self.class) &&
35
+ !id.nil? &&
36
+ comparison_object.id == id
37
+ end
38
+ alias :eql? :==
39
+
40
+ def hash
41
+ if id
42
+ self.class.hash ^ id.hash
43
+ else
44
+ super
45
+ end
46
+ end
47
+
21
48
  def json_root
22
49
  self.class.json_root
23
50
  end
@@ -15,20 +15,20 @@ module NCore
15
15
  if respond_to? :update, true
16
16
  update(update_params)
17
17
  else
18
- raise self.class.parent::Error, "Updating #{self.class.name} objects is not supported."
18
+ raise self.class.module_parent::Error, "Updating #{self.class.name} objects is not supported."
19
19
  end
20
20
  else
21
21
  if respond_to? :create, true
22
22
  create(update_params)
23
23
  else
24
- raise self.class.parent::Error, "Creating #{self.class.name} objects is not supported."
24
+ raise self.class.module_parent::Error, "Creating #{self.class.name} objects is not supported."
25
25
  end
26
26
  end
27
27
  end
28
28
  alias :update_attributes :save
29
29
 
30
30
  def save!(update_params={})
31
- save(update_params) || raise(self.class.parent::RecordInvalid, self)
31
+ save(update_params) || raise(self.class.module_parent::RecordInvalid, self)
32
32
  end
33
33
  alias :update_attributes! :save!
34
34
 
@@ -5,18 +5,14 @@ module NCore
5
5
  module ClassMethods
6
6
  def all(params={})
7
7
  params = parse_request_params(params)
8
- parsed, creds = request(:get, url, params)
8
+ parsed, creds = request(:get, resource_path, params)
9
9
  if parsed[:errors].any?
10
- raise parent::QueryError, parsed[:errors]
10
+ raise module_parent::QueryError, parsed[:errors]
11
11
  end
12
12
  Collection.new.tap do |coll|
13
13
  coll.metadata = parsed[:metadata]
14
14
  parsed[:data].each do |hash|
15
- if key = hash[:object]
16
- coll << discover_class(key, self).new(hash.merge(metadata: parsed[:metadata]), creds)
17
- else
18
- coll << new(hash.merge(metadata: parsed[:metadata]), creds)
19
- end
15
+ coll << factory(hash.merge(metadata: parsed[:metadata]), creds)
20
16
  end
21
17
  end
22
18
  end
@@ -5,9 +5,9 @@ module NCore
5
5
  module ClassMethods
6
6
  def build(params={})
7
7
  params = parse_request_params(params)
8
- parsed, creds = request(:get, url+'/new', params)
8
+ parsed, creds = request(:get, "#{resource_path}/new", params)
9
9
  if parsed[:errors].any?
10
- raise parent::QueryError, parsed[:errors]
10
+ raise module_parent::QueryError, parsed[:errors]
11
11
  end
12
12
  new(parsed, creds)
13
13
  end
@@ -5,7 +5,7 @@ module NCore
5
5
  module ClassMethods
6
6
  def count(params={})
7
7
  params = parse_request_params(params)
8
- parsed, _ = request(:get, "#{url}/count", params)
8
+ parsed, _ = request(:get, "#{resource_path}/count", params)
9
9
  parsed[:data][:count]
10
10
  end
11
11
  end
@@ -6,16 +6,15 @@ module NCore
6
6
  def create!(attribs={})
7
7
  obj = create(attribs)
8
8
  if obj.errors?
9
- raise parent::RecordInvalid, obj
9
+ raise module_parent::RecordInvalid, obj
10
10
  end
11
11
  obj
12
12
  end
13
13
 
14
14
  # always returns a new object; check .errors? or .valid? to see how it went
15
15
  def create(attribs={})
16
- params = parse_request_params(attribs)
17
- obj = new({}, params[:credentials])
18
- obj.send :create, params
16
+ obj = new
17
+ obj.send :create, attribs
19
18
  obj
20
19
  end
21
20
  end
@@ -24,7 +23,7 @@ module NCore
24
23
 
25
24
  def create(attribs={})
26
25
  params = parse_request_params(attribs, json_root: json_root).reverse_merge credentials: api_creds
27
- parsed, @api_creds = request(:post, self.class.url, params)
26
+ parsed, @api_creds = request(:post, self.class.resource_path, params)
28
27
  load(data: params[json_root]) if parsed[:errors].any?
29
28
  load(parsed)
30
29
  errors.empty? ? self : false
@@ -4,18 +4,17 @@ module NCore
4
4
 
5
5
  module ClassMethods
6
6
  def delete!(id, params={})
7
- obj = delete(id, attribs)
7
+ obj = delete(id, params)
8
8
  if obj.errors?
9
- raise parent::RecordInvalid, obj
9
+ raise module_parent::RecordInvalid, obj
10
10
  end
11
11
  obj
12
12
  end
13
13
 
14
14
  # always returns a new object; check .errors? or .valid? to see how it went
15
15
  def delete(id, params={})
16
- raise(parent::RecordNotFound, "Cannot delete id=nil") if id.blank?
17
- params = parse_request_params(params)
18
- obj = new({id: id}, params[:credentials])
16
+ raise(module_parent::RecordNotFound, "Cannot delete id=nil") if id.blank?
17
+ obj = new(id: id)
19
18
  obj.delete(params)
20
19
  obj
21
20
  end
@@ -23,13 +22,13 @@ module NCore
23
22
 
24
23
  def delete(params={})
25
24
  params = parse_request_params(params).reverse_merge credentials: api_creds
26
- parsed, @api_creds = request(:delete, url, params)
25
+ parsed, @api_creds = request(:delete, resource_path, params)
27
26
  load(parsed)
28
27
  errors.empty? ? self : false
29
28
  end
30
29
 
31
30
  def delete!(params={})
32
- delete(params) || raise(self.class.parent::RecordInvalid, self)
31
+ delete(params) || raise(self.class.module_parent::RecordInvalid, self)
33
32
  end
34
33
 
35
34
  end
@@ -4,12 +4,12 @@ module NCore
4
4
 
5
5
  module ClassMethods
6
6
  def bulk_delete!(ids, params={})
7
- raise(parent::RecordNotFound, "ids must not be empty") if ids.blank?
7
+ raise(module_parent::RecordNotFound, "ids must not be empty") if ids.blank?
8
8
  params[:ids] = ids
9
9
  params = parse_request_params(params)
10
- parsed, creds = request(:delete, url, params)
10
+ parsed, creds = request(:delete, resource_path, params)
11
11
  if parsed[:errors].any?
12
- raise parent::BulkActionError, parsed[:errors]
12
+ raise module_parent::BulkActionError, parsed[:errors]
13
13
  else
14
14
  parsed[:metadata]
15
15
  end
@@ -17,7 +17,7 @@ module NCore
17
17
 
18
18
  def bulk_delete(ids, params={})
19
19
  bulk_delete!(ids, params)
20
- rescue parent::RecordNotFound, parent::BulkActionError
20
+ rescue module_parent::RecordNotFound, module_parent::BulkActionError
21
21
  false
22
22
  end
23
23
  end
@@ -4,21 +4,20 @@ module NCore
4
4
 
5
5
  module ClassMethods
6
6
  def delete(params={})
7
- params = parse_request_params(params)
8
- obj = new({}, params[:credentials])
9
- obj.delete(params) || raise(parent::RecordInvalid, obj)
7
+ obj = new
8
+ obj.delete!(params)
10
9
  end
11
10
  end
12
11
 
13
12
  def delete(params={})
14
13
  params = parse_request_params(params).reverse_merge credentials: api_creds
15
- parsed, @api_creds = request(:delete, url, params)
14
+ parsed, @api_creds = request(:delete, resource_path, params)
16
15
  load(parsed)
17
16
  errors.empty? ? self : false
18
17
  end
19
18
 
20
19
  def delete!(params={})
21
- delete(params) || raise(self.class.parent::RecordInvalid, self)
20
+ delete(params) || raise(self.class.module_parent::RecordInvalid, self)
22
21
  end
23
22
 
24
23
  end
@@ -4,23 +4,22 @@ module NCore
4
4
 
5
5
  module ClassMethods
6
6
  def find(id, params={})
7
- raise(parent::RecordNotFound, "Cannot find id=nil") if id.blank?
8
- params = parse_request_params(params)
9
- o = new({id: id}, params[:credentials])
7
+ raise(module_parent::RecordNotFound, "Cannot find id=nil") if id.blank?
8
+ o = new(id: id)
10
9
  o.reload(params)
11
10
  end
12
11
 
13
12
  def retrieve(id, params={})
14
13
  find id, params
15
- rescue parent::RecordNotFound
16
- false
14
+ rescue module_parent::RecordNotFound
15
+ nil
17
16
  end
18
17
  end
19
18
 
20
19
  def reload(find_params={})
21
20
  return if id.blank?
22
21
  params = parse_request_params(find_params).reverse_merge credentials: api_creds
23
- parsed, @api_creds = request(:get, url, params)
22
+ parsed, @api_creds = request(:get, resource_path, params)
24
23
  @attribs = {}.with_indifferent_access
25
24
  load(parsed)
26
25
  end
@@ -4,18 +4,14 @@ module NCore
4
4
 
5
5
  module ClassMethods
6
6
  def find(params={})
7
- params = parse_request_params(params)
8
- parsed, creds = request(:get, url, params)
9
- if parsed[:errors].any?
10
- raise parent::QueryError, parsed[:errors]
11
- end
12
- new(parsed, creds)
7
+ obj = new
8
+ obj.reload(params)
13
9
  end
14
10
 
15
11
  def retrieve(params={})
16
12
  find params
17
- rescue parent::RecordNotFound
18
- false
13
+ rescue module_parent::RecordNotFound
14
+ nil
19
15
  end
20
16
  end
21
17
 
@@ -27,7 +23,7 @@ module NCore
27
23
 
28
24
  def reload(find_params={})
29
25
  params = parse_request_params(find_params).reverse_merge credentials: api_creds
30
- parsed, @api_creds = request(:get, url, params)
26
+ parsed, @api_creds = request(:get, resource_path, params)
31
27
  @attribs = {}.with_indifferent_access
32
28
  load(parsed)
33
29
  end
@@ -6,31 +6,30 @@ module NCore
6
6
  def update!(id, attribs)
7
7
  obj = update(id, attribs)
8
8
  if obj.errors?
9
- raise parent::RecordInvalid, obj
9
+ raise module_parent::RecordInvalid, obj
10
10
  end
11
11
  obj
12
12
  end
13
13
 
14
14
  # always returns a new object; check .errors? or .valid? to see how it went
15
15
  def update(id, attribs)
16
- raise(parent::RecordNotFound, "Cannot update id=nil") if id.blank?
17
- params = parse_request_params(attribs)
18
- obj = new({id: id}, params[:credentials])
19
- obj.update params
16
+ raise(module_parent::RecordNotFound, "Cannot update id=nil") if id.blank?
17
+ obj = new(id: id)
18
+ obj.update attribs
20
19
  obj
21
20
  end
22
21
  end
23
22
 
24
23
  def update(attribs={})
25
24
  params = parse_request_params(attribs, json_root: json_root).reverse_merge credentials: api_creds
26
- parsed, @api_creds = request(:put, url, params)
25
+ parsed, @api_creds = request(:put, resource_path, params)
27
26
  load(data: params[json_root]) if parsed[:errors].any?
28
27
  load(parsed)
29
28
  errors.empty? ? self : false
30
29
  end
31
30
 
32
31
  def update!(params={})
33
- update(params) || raise(self.class.parent::RecordInvalid, self)
32
+ update(params) || raise(self.class.module_parent::RecordInvalid, self)
34
33
  end
35
34
 
36
35
  end
@@ -1,5 +1,3 @@
1
- require 'active_model'
2
-
3
1
  module NCore
4
2
  module ActiveModel
5
3
  extend ActiveSupport::Concern
@@ -8,12 +6,11 @@ module NCore
8
6
  include ::ActiveModel::Conversion
9
7
  extend ::ActiveModel::Naming
10
8
  extend ::ActiveModel::Translation
11
- alias :errors :errors_for_actionpack
12
9
  end
13
10
 
14
- if defined?(Rails)
11
+ if defined?(::Rails)
15
12
  def logger
16
- Rails.logger
13
+ ::Rails.logger
17
14
  end
18
15
  end
19
16
 
@@ -29,17 +26,30 @@ module NCore
29
26
  delete(*args)
30
27
  end
31
28
 
32
- # actionpack 4 requires a more robust Errors object
33
- def errors_for_actionpack
34
- e0 = ::ActiveModel::Errors.new(self)
35
- @errors.each do |e|
36
- e0.add :base, e
29
+
30
+ # compatible with ActiveRecord 5.2+ default settings
31
+ # on <= 5.1, does not include version
32
+ def cache_key(*_)
33
+ if new_record?
34
+ "#{model_name.cache_key}/new"
35
+ else
36
+ "#{model_name.cache_key}/#{id}"
37
37
  end
38
- e0
39
38
  end
40
39
 
41
- end
40
+ def cache_version
41
+ if timestamp = try(:updated_at)
42
+ timestamp.utc.to_s(:usec)
43
+ end
44
+ end
42
45
 
43
- Base.send :include, ActiveModel
44
- SingletonBase.send :include, ActiveModel
46
+ def cache_key_with_version
47
+ if version = cache_version
48
+ "#{cache_key}-#{version}"
49
+ else
50
+ cache_key
51
+ end
52
+ end
53
+
54
+ end
45
55
  end
@@ -1,17 +1,19 @@
1
1
  # ActiveSupport 6.0 changed :parent -> :module_parent
2
- # This eliminates deprecation messages so everything works like v5.2 and prior.
2
+ # This continues to support AS <= 5.2.
3
3
 
4
4
  module NCore
5
5
  module ModuleFix
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- alias :parent :module_parent
9
+ alias :module_parent :parent
10
+ alias :module_parent_name :parent_name
11
+ alias :module_parents :parents
10
12
  end
11
13
 
12
14
  end
13
15
  end
14
16
 
15
- if Module.respond_to?(:module_parent)
17
+ unless Module.respond_to?(:module_parent)
16
18
  Module.include NCore::ModuleFix
17
19
  end
@@ -4,6 +4,7 @@ module NCore
4
4
 
5
5
  included do
6
6
  extend Associations
7
+ include ActiveModel
7
8
  include Attributes
8
9
  include Client
9
10
  include Identity
@@ -20,13 +21,13 @@ module NCore
20
21
  include Update if types.include? :update
21
22
  end
22
23
 
23
- def url
24
+ def resource_path
24
25
  class_name.underscore
25
26
  end
26
27
  end
27
28
 
28
- def url
29
- self.class.url
29
+ def resource_path
30
+ self.class.resource_path
30
31
  end
31
32
 
32
33
  end