activeentity 0.0.1.beta15 → 0.0.1.beta16
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fc81c04f6aa3b9b2095061533a4e631de8fd2ef7fff0d913514dba7e78058c3
|
4
|
+
data.tar.gz: 6b186abc2ea7c57e96ba9faa4ff77846d649ec84a3ee4174980414e7a306d6de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0261b0ccacb0996ed08028be638b82606a4e64684cba16dec3d25d1b899533145c5b25e31ddd0a5a4d496d97cc22eba42b4b2cc898c60a266584d69b070446f7
|
7
|
+
data.tar.gz: 0a4648417584703bbf90ae819369b730c2fcb0e5ce3c083d72603bff648b838885b3ededea13f9c3dc5592b31e183eb03e83d22774d80449381edf6d66d089cf
|
data/README.md
CHANGED
@@ -65,7 +65,7 @@ Supported Active Record validations:
|
|
65
65
|
|
66
66
|
#### `subset` validation
|
67
67
|
|
68
|
-
Because Active Entity supports array attribute, for some reason,
|
68
|
+
Because Active Entity supports array attribute, for some reason, you may want to test values of an array attribute are all included in a given set.
|
69
69
|
|
70
70
|
Active Entity provides `subset` validation to achieve that, it usage similar to `inclusion` or `exclusion`
|
71
71
|
|
@@ -76,9 +76,9 @@ class Steak < ActiveEntity::Base
|
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
|
-
#### `
|
79
|
+
#### `uniqueness_in_embeds` validation
|
80
80
|
|
81
|
-
Active Entity provides `
|
81
|
+
Active Entity provides `uniqueness_in_embeds` validation to test duplicate nesting virtual record.
|
82
82
|
|
83
83
|
Argument `key` is attribute name of nested model, it also supports multiple attributes by given an array.
|
84
84
|
|
@@ -101,6 +101,23 @@ class Book < ActiveEntity::Base
|
|
101
101
|
end
|
102
102
|
```
|
103
103
|
|
104
|
+
#### `uniqueness_in_active_record` validation
|
105
|
+
|
106
|
+
Active Entity provides `uniqueness_in_active_record` validation to test given `scope` doesn't present in ActiveRecord model.
|
107
|
+
|
108
|
+
The usage same as [uniqueness](https://guides.rubyonrails.org/active_record_validations.html#uniqueness) in addition you must give a AR model `class_name`
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class Candidate < ActiveEntity::Base
|
112
|
+
attribute :name, :string
|
113
|
+
|
114
|
+
validates :name,
|
115
|
+
uniqueness_on_active_record: {
|
116
|
+
class_name: "Staff"
|
117
|
+
}
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
104
121
|
### Others
|
105
122
|
|
106
123
|
These Active Record feature also available in Active Entity
|
@@ -6,29 +6,25 @@ module ActiveEntity
|
|
6
6
|
module AttributeAssignment
|
7
7
|
include ActiveModel::AttributeAssignment
|
8
8
|
|
9
|
-
def assign_attributes(attributes)
|
10
|
-
super(attributes.dup)
|
11
|
-
end
|
12
|
-
|
13
9
|
private
|
14
10
|
|
15
11
|
def _assign_attributes(attributes)
|
16
|
-
multi_parameter_attributes
|
17
|
-
nested_parameter_attributes = {}
|
12
|
+
multi_parameter_attributes = nested_parameter_attributes = nil
|
18
13
|
|
19
14
|
attributes.each do |k, v|
|
20
15
|
key = k.to_s
|
21
16
|
|
22
17
|
if key.include?("(")
|
23
|
-
multi_parameter_attributes[key] =
|
18
|
+
(multi_parameter_attributes ||= {})[key] = v
|
24
19
|
elsif v.is_a?(Hash)
|
25
|
-
nested_parameter_attributes[key] =
|
20
|
+
(nested_parameter_attributes ||= {})[key] = v
|
21
|
+
else
|
22
|
+
_assign_attribute(key, v)
|
26
23
|
end
|
27
24
|
end
|
28
|
-
super(attributes)
|
29
25
|
|
30
|
-
assign_nested_parameter_attributes(nested_parameter_attributes)
|
31
|
-
assign_multiparameter_attributes(multi_parameter_attributes)
|
26
|
+
assign_nested_parameter_attributes(nested_parameter_attributes) if nested_parameter_attributes
|
27
|
+
assign_multiparameter_attributes(multi_parameter_attributes) if multi_parameter_attributes
|
32
28
|
end
|
33
29
|
|
34
30
|
# Assign any deferred nested attributes after the base attributes have been set.
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "mutex_m"
|
4
|
+
require "active_support/core_ext/enumerable"
|
4
5
|
|
5
6
|
module ActiveEntity
|
6
7
|
# = Active Entity Attribute Methods
|
@@ -268,39 +269,6 @@ module ActiveEntity
|
|
268
269
|
write_attribute(attr_name, value)
|
269
270
|
end
|
270
271
|
|
271
|
-
# Returns the name of all database fields which have been read from this
|
272
|
-
# model. This can be useful in development mode to determine which fields
|
273
|
-
# need to be selected. For performance critical pages, selecting only the
|
274
|
-
# required fields can be an easy performance win (assuming you aren't using
|
275
|
-
# all of the fields on the model).
|
276
|
-
#
|
277
|
-
# For example:
|
278
|
-
#
|
279
|
-
# class PostsController < ActionController::Base
|
280
|
-
# after_action :print_accessed_fields, only: :index
|
281
|
-
#
|
282
|
-
# def index
|
283
|
-
# @posts = Post.all
|
284
|
-
# end
|
285
|
-
#
|
286
|
-
# private
|
287
|
-
#
|
288
|
-
# def print_accessed_fields
|
289
|
-
# p @posts.first.accessed_fields
|
290
|
-
# end
|
291
|
-
# end
|
292
|
-
#
|
293
|
-
# Which allows you to quickly change your code to:
|
294
|
-
#
|
295
|
-
# class PostsController < ActionController::Base
|
296
|
-
# def index
|
297
|
-
# @posts = Post.select(:id, :title, :author_id, :updated_at)
|
298
|
-
# end
|
299
|
-
# end
|
300
|
-
def accessed_fields
|
301
|
-
@attributes.accessed
|
302
|
-
end
|
303
|
-
|
304
272
|
private
|
305
273
|
|
306
274
|
def attribute_method?(attr_name)
|
@@ -308,12 +276,6 @@ module ActiveEntity
|
|
308
276
|
defined?(@attributes) && @attributes.key?(attr_name)
|
309
277
|
end
|
310
278
|
|
311
|
-
def attributes_with_values(attribute_names)
|
312
|
-
attribute_names.each_with_object({}) do |name, attrs|
|
313
|
-
attrs[name] = _read_attribute(name)
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
279
|
def format_for_inspect(value)
|
318
280
|
if value.is_a?(String) && value.length > 50
|
319
281
|
"#{value[0, 50]}...".inspect
|
@@ -27,7 +27,7 @@ module ActiveEntity
|
|
27
27
|
raise ArgumentError, "Must provide one of option :class_name or :class."
|
28
28
|
end
|
29
29
|
unless @klass < ActiveRecord::Base
|
30
|
-
raise ArgumentError, "Class must be an Active Entity model, but got #{@
|
30
|
+
raise ArgumentError, "Class must be an Active Entity model, but got #{@klass}."
|
31
31
|
end
|
32
32
|
if @klass.abstract_class?
|
33
33
|
raise ArgumentError, "Class can't be an abstract class."
|
@@ -39,13 +39,6 @@ module ActiveEntity
|
|
39
39
|
value = map_enum_attribute(finder_class, attribute, value)
|
40
40
|
|
41
41
|
relation = build_relation(finder_class, attribute, value)
|
42
|
-
if record.persisted?
|
43
|
-
if finder_class.primary_key
|
44
|
-
relation = relation.where.not(finder_class.primary_key => record.id_in_database)
|
45
|
-
else
|
46
|
-
raise UnknownPrimaryKey.new(finder_class, "Cannot validate uniqueness for persisted record without primary key.")
|
47
|
-
end
|
48
|
-
end
|
49
42
|
relation = scope_relation(record, relation)
|
50
43
|
relation = relation.merge(options[:conditions]) if options[:conditions]
|
51
44
|
|
@@ -94,11 +87,7 @@ module ActiveEntity
|
|
94
87
|
|
95
88
|
def scope_relation(record, relation)
|
96
89
|
Array(options[:scope]).each do |scope_item|
|
97
|
-
scope_value =
|
98
|
-
record.association(scope_item).reader
|
99
|
-
else
|
100
|
-
record._read_attribute(scope_item)
|
101
|
-
end
|
90
|
+
scope_value = record._read_attribute(scope_item)
|
102
91
|
relation = relation.where(scope_item => scope_value)
|
103
92
|
end
|
104
93
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeentity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.beta16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jasl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|