wallaby-active_record 0.2.4 → 0.2.5
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 +4 -4
- data/README.md +2 -1
- data/lib/adapters/wallaby/active_record.rb +2 -1
- data/lib/adapters/wallaby/active_record/cancancan_provider.rb +1 -1
- data/lib/adapters/wallaby/active_record/default_provider.rb +1 -1
- data/lib/adapters/wallaby/active_record/model_decorator.rb +28 -26
- data/lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb +3 -2
- data/lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb +6 -4
- data/lib/adapters/wallaby/active_record/model_decorator/fields_builder/polymorphic_builder.rb +3 -2
- data/lib/adapters/wallaby/active_record/model_decorator/fields_builder/sti_builder.rb +17 -13
- data/lib/adapters/wallaby/active_record/model_decorator/title_field_finder.rb +6 -9
- data/lib/adapters/wallaby/active_record/model_finder.rb +16 -29
- data/lib/adapters/wallaby/active_record/model_pagination_provider.rb +5 -13
- data/lib/adapters/wallaby/active_record/model_service_provider.rb +8 -17
- data/lib/adapters/wallaby/active_record/model_service_provider/permitter.rb +1 -1
- data/lib/adapters/wallaby/active_record/model_service_provider/querier.rb +1 -1
- data/lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb +14 -0
- data/lib/adapters/wallaby/active_record/model_service_provider/querier/wrapper.rb +4 -1
- data/lib/adapters/wallaby/active_record/model_service_provider/validator.rb +6 -4
- data/lib/adapters/wallaby/active_record/pundit_provider.rb +2 -2
- data/lib/wallaby/active_record.rb +0 -1
- data/lib/wallaby/active_record/version.rb +1 -1
- metadata +6 -7
- data/lib/adapters/wallaby/active_record/logger.rb +0 -21
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3326a29a32d97399d5d5e5381ca283b5fefc1601bbe212250c2753deb26d9509
|
|
4
|
+
data.tar.gz: e61f519c75994ffce103baee81a573941869487dec966e6060299ec24fe73e12
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 05003c11197eeab16a351e202b3f7ee97a593abb0b7bf99505d516acb924950522805e6a0198fa274a635d737f1a4a9b69a8591f961304a0e4085706de86800a
|
|
7
|
+
data.tar.gz: 8f34233e937848899b6d16a7143a990cd584ccaf23b9c29700ac1cc8837214f1184b34e0fd0fd1f93eaeef15fcaab0898276d63e5529aac4da2a9aac064a378d
|
data/README.md
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
[](https://codeclimate.com/github/wallaby-rails/wallaby-active_record/test_coverage)
|
|
8
8
|
[](https://inch-ci.org/github/wallaby-rails/wallaby-active_record)
|
|
9
9
|
|
|
10
|
-
Wallaby::ActiveRecord is the ActiveRecord adapter
|
|
10
|
+
Wallaby::ActiveRecord is the ActiveRecord adapter that implements the [Wallaby::Core](https://github.com/wallaby-rails/wallaby-core)
|
|
11
|
+
interfaces to handle ActiveRecord model/instance(s) in all the CRUD/authorization/pagination operations.
|
|
11
12
|
|
|
12
13
|
## Install
|
|
13
14
|
|
|
@@ -2,24 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
module Wallaby
|
|
4
4
|
class ActiveRecord
|
|
5
|
-
# Modal decorator for ActiveRecord
|
|
5
|
+
# Modal decorator for {Wallaby::ActiveRecord}
|
|
6
6
|
class ModelDecorator < ::Wallaby::ModelDecorator
|
|
7
|
-
# Data types to exclude for
|
|
8
|
-
INDEX_EXCLUSIVE_DATA_TYPES =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
# Data types to exclude for {#index_field_names}
|
|
8
|
+
INDEX_EXCLUSIVE_DATA_TYPES = %w(
|
|
9
|
+
binary citext hstore json jsonb tsvector xml
|
|
10
|
+
blob mediumblob longblob text mediumtext longtext
|
|
11
|
+
).freeze
|
|
12
12
|
|
|
13
|
-
#
|
|
13
|
+
# Classes to exclude for {#show_field_names}
|
|
14
14
|
SHOW_EXCLUSIVE_CLASS_NAMES = %w(ActiveStorage::Attachment ActiveStorage::Blob).freeze
|
|
15
15
|
|
|
16
|
-
#
|
|
16
|
+
# Fields to exclude for {#form_field_names}
|
|
17
17
|
FORM_EXCLUSIVE_DATA_TYPES = %w(created_at updated_at).freeze
|
|
18
18
|
|
|
19
|
-
#
|
|
19
|
+
# Original metadata information of the primative and association fields
|
|
20
|
+
# pulling out from the ActiveRecord model.
|
|
20
21
|
#
|
|
21
|
-
# It needs to be frozen so that we can keep the metadata
|
|
22
|
-
# @example sample fields:
|
|
22
|
+
# It needs to be frozen so that we can keep the metadata intact.
|
|
23
|
+
# @example sample fields metadata:
|
|
23
24
|
# model_decorator.fields
|
|
24
25
|
# # =>
|
|
25
26
|
# {
|
|
@@ -27,28 +28,29 @@ module Wallaby
|
|
|
27
28
|
# id: { name: 'id', type: 'integer', label: 'Id' },
|
|
28
29
|
# # association field
|
|
29
30
|
# category: {
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
31
|
+
# name: 'category',
|
|
32
|
+
# type: 'belongs_to',
|
|
33
|
+
# label: 'Category',
|
|
34
|
+
# is_association: true,
|
|
35
|
+
# is_through: false,
|
|
36
|
+
# has_scope: false,
|
|
37
|
+
# foreign_key: 'category_id',
|
|
38
|
+
# class: Category
|
|
38
39
|
# }
|
|
39
40
|
# }
|
|
40
41
|
# @return [ActiveSupport::HashWithIndifferentAccess] metadata
|
|
41
42
|
def fields
|
|
43
|
+
# NOTE: Need to check the database and table's existence before building up the metadata
|
|
44
|
+
# so that the database creation and migration related task can be executed.
|
|
42
45
|
@fields ||= ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash|
|
|
43
|
-
|
|
44
|
-
# before pulling out the metadata from model.
|
|
45
|
-
# So that the database and migration related task can be executed.
|
|
46
|
-
next unless ::ActiveRecord::Base.connected? && @model_class.table_exists?
|
|
46
|
+
next unless @model_class.table_exists?
|
|
47
47
|
|
|
48
48
|
hash.merge! general_fields
|
|
49
49
|
hash.merge! association_fields
|
|
50
50
|
hash.except!(*foreign_keys_from_associations)
|
|
51
51
|
end.freeze
|
|
52
|
+
rescue ::ActiveRecord::NoDatabaseError
|
|
53
|
+
Hash.new({})
|
|
52
54
|
end
|
|
53
55
|
|
|
54
56
|
# A copy of {#fields} for index page
|
|
@@ -86,7 +88,8 @@ module Wallaby
|
|
|
86
88
|
end.keys
|
|
87
89
|
end
|
|
88
90
|
|
|
89
|
-
# @return [Array<String>] a list of field names for form (new/edit) page
|
|
91
|
+
# @return [Array<String>] a list of field names for form (new/edit) page
|
|
92
|
+
# (note: timestamps fields (e.g. created_at/updated_at) and complex relation fields are excluded).
|
|
90
93
|
def form_field_names
|
|
91
94
|
@form_field_names ||=
|
|
92
95
|
form_fields.reject do |field_name, metadata|
|
|
@@ -109,9 +112,8 @@ module Wallaby
|
|
|
109
112
|
# To guess the title for resource.
|
|
110
113
|
#
|
|
111
114
|
# It will go through the fields and try to find out the one that looks
|
|
112
|
-
# like a name or text
|
|
115
|
+
# like a name or text representing this resource. Otherwise, it will fall
|
|
113
116
|
# back to primary key.
|
|
114
|
-
#
|
|
115
117
|
# @param resource [Object]
|
|
116
118
|
# @return [String] the title of given resource
|
|
117
119
|
def guess_title(resource)
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module Wallaby
|
|
4
4
|
class ActiveRecord
|
|
5
5
|
class ModelDecorator
|
|
6
|
-
# To
|
|
6
|
+
# To build the metadata for fields
|
|
7
7
|
class FieldsBuilder
|
|
8
8
|
# @param model_class [Class]
|
|
9
9
|
def initialize(model_class)
|
|
@@ -22,7 +22,8 @@ module Wallaby
|
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
# @return [Hash<String, Hash>] a hash for
|
|
25
|
+
# @return [Hash<String, Hash>] a hash for association fields
|
|
26
|
+
# (e.g. belongs_to / has_one / has_many / has_and_belongs_to_many)
|
|
26
27
|
def association_fields
|
|
27
28
|
@model_class.reflections.each_with_object({}) do |(name, ref), fields|
|
|
28
29
|
metadata = {
|
data/lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb
CHANGED
|
@@ -4,7 +4,7 @@ module Wallaby
|
|
|
4
4
|
class ActiveRecord
|
|
5
5
|
class ModelDecorator
|
|
6
6
|
class FieldsBuilder
|
|
7
|
-
# To build the metadata for associations
|
|
7
|
+
# To build the metadata for associations fields
|
|
8
8
|
class AssociationBuilder
|
|
9
9
|
# Update the metadata
|
|
10
10
|
# @param metadata [Hash]
|
|
@@ -18,7 +18,7 @@ module Wallaby
|
|
|
18
18
|
metadata[:foreign_key] = foreign_key_for(reflection, type)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
protected
|
|
22
22
|
|
|
23
23
|
# @param reflection [ActiveRecord::Reflection]
|
|
24
24
|
# @param type [Symbol]
|
|
@@ -35,13 +35,15 @@ module Wallaby
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# @param reflection [ActiveRecord::Reflection]
|
|
38
|
-
# @return [
|
|
38
|
+
# @return [true] if it's a through relation
|
|
39
|
+
# @return [false] otherwise
|
|
39
40
|
def through?(reflection)
|
|
40
41
|
reflection.is_a? ::ActiveRecord::Reflection::ThroughReflection
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
# @param reflection [ActiveRecord::Reflection]
|
|
44
|
-
# @return [
|
|
45
|
+
# @return [true] if it has scope
|
|
46
|
+
# @return [false] otherwise
|
|
45
47
|
def scope?(reflection)
|
|
46
48
|
reflection.scope.present?
|
|
47
49
|
end
|
data/lib/adapters/wallaby/active_record/model_decorator/fields_builder/polymorphic_builder.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Wallaby
|
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
protected
|
|
23
23
|
|
|
24
24
|
# @param reflection [ActiveRecord::Reflection]
|
|
25
25
|
# @return [Array<Class>] a list of classes for this polymorphism
|
|
@@ -38,7 +38,8 @@ module Wallaby
|
|
|
38
38
|
|
|
39
39
|
# @param model_class [Class]
|
|
40
40
|
# @param polymorphic_name [String] polymorphic name
|
|
41
|
-
# @return [
|
|
41
|
+
# @return [true] if polymorphism defined?
|
|
42
|
+
# @return [false] otherwise
|
|
42
43
|
def polymorphic_defined?(model_class, polymorphic_name)
|
|
43
44
|
polymorphic_name_sym = polymorphic_name.try(:to_sym)
|
|
44
45
|
model_class.reflections.any? do |_field_name, reflection|
|
|
@@ -4,44 +4,48 @@ module Wallaby
|
|
|
4
4
|
class ActiveRecord
|
|
5
5
|
class ModelDecorator
|
|
6
6
|
class FieldsBuilder
|
|
7
|
-
#
|
|
7
|
+
# This class updates the field metadata's value of **type** and **sti_class_list**
|
|
8
|
+
# for STI (Single Table Inheritance) model
|
|
8
9
|
class StiBuilder
|
|
9
10
|
# @param model_class [Class]
|
|
10
11
|
def initialize(model_class)
|
|
11
12
|
@model_class = model_class
|
|
12
13
|
end
|
|
13
14
|
|
|
14
|
-
#
|
|
15
|
+
# Update the field metadata's value for **type** and **sti_class_list**
|
|
15
16
|
# @param metadata [Hash]
|
|
16
17
|
# @param column [ActiveRecord::ConnectionAdapters::Column]
|
|
17
18
|
def update(metadata, column)
|
|
18
19
|
return unless @model_class.inheritance_column == column.name
|
|
19
20
|
|
|
20
21
|
metadata[:type] = 'sti'
|
|
21
|
-
metadata[:sti_class_list] = sti_list(
|
|
22
|
+
metadata[:sti_class_list] = sti_list(find_sti_parent_of(@model_class))
|
|
22
23
|
end
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
protected
|
|
25
26
|
|
|
27
|
+
# Return the alphabet-order STI list
|
|
28
|
+
# by traversing the inheritance tree for given model.
|
|
26
29
|
# @param klass [Class]
|
|
27
|
-
# @return [Array<Class>]
|
|
30
|
+
# @return [Array<Class>]
|
|
28
31
|
def sti_list(klass)
|
|
29
|
-
|
|
30
|
-
list.sort_by(&:name)
|
|
32
|
+
(klass.descendants << klass).sort_by(&:name)
|
|
31
33
|
end
|
|
32
34
|
|
|
35
|
+
# Find out which parent is the one that can give us the STI list.
|
|
33
36
|
# @param klass [Class]
|
|
34
|
-
# @return [Class]
|
|
35
|
-
def
|
|
37
|
+
# @return [Class]
|
|
38
|
+
def find_sti_parent_of(klass)
|
|
36
39
|
parent = klass
|
|
37
|
-
parent = parent.superclass until
|
|
40
|
+
parent = parent.superclass until not_sti_parent?(parent.superclass)
|
|
38
41
|
parent
|
|
39
42
|
end
|
|
40
43
|
|
|
41
44
|
# @param klass [Class]
|
|
42
|
-
# @return [
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
# @return [true] if klass is ActiveRecord::Base or abstract
|
|
46
|
+
# @return [false] otherwise
|
|
47
|
+
def not_sti_parent?(klass)
|
|
48
|
+
klass == ::ActiveRecord::Base || klass.try(:abstract_class?)
|
|
45
49
|
end
|
|
46
50
|
end
|
|
47
51
|
end
|
|
@@ -16,15 +16,12 @@ module Wallaby
|
|
|
16
16
|
|
|
17
17
|
# @return [String] field name that can be used as title
|
|
18
18
|
def find
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
target_field \
|
|
26
|
-
|| possible_title_fields.keys.first \
|
|
27
|
-
|| @model_class.primary_key
|
|
19
|
+
FieldUtils.first_field_by(
|
|
20
|
+
{
|
|
21
|
+
name: /title|name|label|string/,
|
|
22
|
+
type: 'string'
|
|
23
|
+
}, @fields
|
|
24
|
+
) || @model_class.primary_key
|
|
28
25
|
end
|
|
29
26
|
end
|
|
30
27
|
end
|
|
@@ -2,43 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
module Wallaby
|
|
4
4
|
class ActiveRecord
|
|
5
|
-
#
|
|
5
|
+
# Finder to return all the appropriate ActiveRecord models.
|
|
6
6
|
class ModelFinder < ::Wallaby::ModelFinder
|
|
7
|
-
#
|
|
7
|
+
# Return a list of ActiveRecord::Base subclasses that aren't one of the following types:
|
|
8
|
+
#
|
|
9
|
+
# 1. abstract class
|
|
10
|
+
# 2. anonymous class
|
|
11
|
+
# 3. the HABTM relation class
|
|
12
|
+
# @return [Array<Class>]
|
|
8
13
|
def all
|
|
9
|
-
|
|
10
|
-
|
|
14
|
+
::ActiveRecord::Base.descendants.reject do |model_class|
|
|
15
|
+
defined?(::ApplicationRecord) && model_class == ::ApplicationRecord ||
|
|
16
|
+
model_class.abstract_class? ||
|
|
17
|
+
anonymous?(model_class) ||
|
|
18
|
+
model_class.name.index('HABTM')
|
|
11
19
|
end.sort_by(&:to_s)
|
|
12
20
|
end
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
# @return [ApplicationRecord, ActiveRecord::Base] base ActiveRecord class
|
|
16
|
-
def self.base
|
|
17
|
-
return ::ApplicationRecord if defined? ::ApplicationRecord
|
|
18
|
-
|
|
19
|
-
::ActiveRecord::Base
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
-
# Is model class abstract?
|
|
25
|
-
# @param model_class [Class]
|
|
26
|
-
# @return [Boolean]
|
|
27
|
-
def abstract?(model_class)
|
|
28
|
-
model_class.abstract_class?
|
|
29
|
-
end
|
|
22
|
+
protected
|
|
30
23
|
|
|
31
24
|
# @see Wallaby::ModuleUtils.anonymous_class?
|
|
32
25
|
def anonymous?(model_class)
|
|
33
|
-
ModuleUtils.anonymous_class?
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
# Check and see if given model class is intermediate class that generated
|
|
37
|
-
# for has and belongs to many assocation
|
|
38
|
-
# @param model_class [Class]
|
|
39
|
-
# @return [Boolean]
|
|
40
|
-
def habtm?(model_class)
|
|
41
|
-
model_class.name.index('HABTM')
|
|
26
|
+
ModuleUtils.anonymous_class?(model_class).tap do |result|
|
|
27
|
+
Logger.warn "Anonymous class is detected for table #{model_class.try :table_name}" if result
|
|
28
|
+
end
|
|
42
29
|
end
|
|
43
30
|
end
|
|
44
31
|
end
|
|
@@ -8,25 +8,17 @@ module Wallaby
|
|
|
8
8
|
# @return [true] if paginatable
|
|
9
9
|
# @return [false] if not paginatable
|
|
10
10
|
def paginatable?
|
|
11
|
-
paginatable
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
@collection.respond_to?(:total_entries) # will_paginate
|
|
15
|
-
Logger.warn "#{@collection.inspect} is not paginatable." unless paginatable
|
|
16
|
-
|
|
17
|
-
paginatable
|
|
11
|
+
(@collection.respond_to?(:unscope) && @collection.respond_to?(:count)).tap do |paginatable|
|
|
12
|
+
Logger.warn "#{@collection} is not paginatable." unless paginatable
|
|
13
|
+
end
|
|
18
14
|
end
|
|
19
15
|
|
|
20
16
|
# @return [Integer] total count for the collection
|
|
21
17
|
def total
|
|
22
|
-
|
|
23
|
-
@collection.try(:total_count) || \
|
|
24
|
-
@collection.try(:total_entries) # will_paginate
|
|
18
|
+
@collection.unscope(:offset, :limit).count
|
|
25
19
|
end
|
|
26
20
|
|
|
27
|
-
# @return [Integer] page size from parameters or
|
|
28
|
-
# {https://rubydoc.info/gems/wallaby-core/Wallaby/Configuration/Pagination#page_size-instance_method page_size}
|
|
29
|
-
# Wallaby configuration
|
|
21
|
+
# @return [Integer] page size from parameters or Wallaby configuration
|
|
30
22
|
def page_size
|
|
31
23
|
(@params[:per] || Wallaby.configuration.pagination.page_size).to_i
|
|
32
24
|
end
|
|
@@ -8,7 +8,6 @@ module Wallaby
|
|
|
8
8
|
# @param action [String, Symbol]
|
|
9
9
|
# @param authorizer
|
|
10
10
|
# @return [ActionController::Parameters] whitelisted parameters
|
|
11
|
-
# @see Wallaby::ModelServiceProvider#permit
|
|
12
11
|
def permit(params, action, authorizer)
|
|
13
12
|
authorized_fields = authorizer.permit_params action, @model_class
|
|
14
13
|
params.require(param_key).permit(authorized_fields || permitted_fields)
|
|
@@ -18,7 +17,6 @@ module Wallaby
|
|
|
18
17
|
# @param params [ActionController::Parameters]
|
|
19
18
|
# @param authorizer [Ability] for now
|
|
20
19
|
# @return [ActiveRecord::Relation] relation
|
|
21
|
-
# @see Wallaby::ModelServiceProvider#collection
|
|
22
20
|
def collection(params, authorizer)
|
|
23
21
|
query = querier.search params
|
|
24
22
|
query = query.order params[:sort] if params[:sort].present?
|
|
@@ -28,19 +26,15 @@ module Wallaby
|
|
|
28
26
|
# @param query [ActiveRecord::Relation]
|
|
29
27
|
# @param params [ActionController::Parameters]
|
|
30
28
|
# @return [ActiveRecord::Relation] paginated query
|
|
31
|
-
# @see Wallaby::ModelServiceProvider#paginate
|
|
32
29
|
def paginate(query, params)
|
|
33
|
-
# NOTE: do not take out the `.to_i` as will_paginate requires an integer `per_page`
|
|
34
30
|
per = (params[:per] || Wallaby.configuration.pagination.page_size).to_i
|
|
35
|
-
|
|
36
|
-
query = query.
|
|
37
|
-
query = query.per_page per if query.respond_to? :per_page # will_paginate
|
|
31
|
+
page = [params[:page].to_i, 1].max # starting from page 1
|
|
32
|
+
query = query.offset((page - 1) * per).limit(per)
|
|
38
33
|
query
|
|
39
34
|
end
|
|
40
35
|
|
|
41
36
|
# @note No mass assignment happens here!
|
|
42
37
|
# @return [Object] new resource object
|
|
43
|
-
# @see Wallaby::ModelServiceProvider#new
|
|
44
38
|
def new(_params, _authorizer)
|
|
45
39
|
@model_class.new
|
|
46
40
|
end
|
|
@@ -50,7 +44,6 @@ module Wallaby
|
|
|
50
44
|
# @param id [Integer, String]
|
|
51
45
|
# @return [Object] persisted resource object
|
|
52
46
|
# @raise [Wallaby::ResourceNotFound] when record is not found
|
|
53
|
-
# @see Wallaby::ModelServiceProvider#find
|
|
54
47
|
def find(id, _params, _authorizer)
|
|
55
48
|
@model_class.find id
|
|
56
49
|
rescue ::ActiveRecord::RecordNotFound
|
|
@@ -61,7 +54,6 @@ module Wallaby
|
|
|
61
54
|
# @param resource [Object]
|
|
62
55
|
# @param params [ActionController::Parameters]
|
|
63
56
|
# @param authorizer [Wallaby::ModelAuthorizer]
|
|
64
|
-
# @see Wallaby::ModelServiceProvider#create
|
|
65
57
|
def create(resource, params, authorizer)
|
|
66
58
|
save __callee__, resource, params, authorizer
|
|
67
59
|
end
|
|
@@ -70,14 +62,12 @@ module Wallaby
|
|
|
70
62
|
# @param resource [Object]
|
|
71
63
|
# @param params [ActionController::Parameters]
|
|
72
64
|
# @param authorizer [Wallaby::ModelAuthorizer]
|
|
73
|
-
# @see Wallaby::ModelServiceProvider#update
|
|
74
65
|
def update(resource, params, authorizer)
|
|
75
66
|
save __callee__, resource, params, authorizer
|
|
76
67
|
end
|
|
77
68
|
|
|
78
69
|
# Remove a record from database
|
|
79
70
|
# @param resource [Object]
|
|
80
|
-
# @see Wallaby::ModelServiceProvider#destroy
|
|
81
71
|
def destroy(resource, _params, _authorizer)
|
|
82
72
|
resource.destroy
|
|
83
73
|
end
|
|
@@ -109,7 +99,8 @@ module Wallaby
|
|
|
109
99
|
|
|
110
100
|
# See if a resource is valid
|
|
111
101
|
# @param resource [Object]
|
|
112
|
-
# @return [
|
|
102
|
+
# @return [true] if valid
|
|
103
|
+
# @return [false] otherwise
|
|
113
104
|
def valid?(resource)
|
|
114
105
|
validator.valid? resource
|
|
115
106
|
end
|
|
@@ -137,22 +128,22 @@ module Wallaby
|
|
|
137
128
|
permitter.simple_field_names << permitter.compound_hashed_fields
|
|
138
129
|
end
|
|
139
130
|
|
|
140
|
-
# @
|
|
131
|
+
# @return [Wallaby::ActiveRecord::ModelServiceProvider::Permitter]
|
|
141
132
|
def permitter
|
|
142
133
|
@permitter ||= Permitter.new @model_decorator
|
|
143
134
|
end
|
|
144
135
|
|
|
145
|
-
# @
|
|
136
|
+
# @return [Wallaby::ActiveRecord::ModelServiceProvider::Querier]
|
|
146
137
|
def querier
|
|
147
138
|
@querier ||= Querier.new @model_decorator
|
|
148
139
|
end
|
|
149
140
|
|
|
150
|
-
# @
|
|
141
|
+
# @return [Wallaby::ActiveRecord::ModelServiceProvider::Normalizer]
|
|
151
142
|
def normalizer
|
|
152
143
|
@normalizer ||= Normalizer.new @model_decorator
|
|
153
144
|
end
|
|
154
145
|
|
|
155
|
-
# @
|
|
146
|
+
# @return [Wallaby::ActiveRecord::ModelServiceProvider::Validator]
|
|
156
147
|
def validator
|
|
157
148
|
@validator ||= Validator.new @model_decorator
|
|
158
149
|
end
|
|
@@ -11,6 +11,14 @@ module Wallaby
|
|
|
11
11
|
PCT = '%' # :nodoc:
|
|
12
12
|
|
|
13
13
|
class << self
|
|
14
|
+
# @example Return the escaped keyword if the first/last char of the keyword is `%`/`_`
|
|
15
|
+
# Wallaby::ActiveRecord::ModelServiceProvider::Querier::Escaper.execute('%something_else%')
|
|
16
|
+
# # => '%something\_else%'
|
|
17
|
+
# @example Return the escaped keyword wrapped with `%` if the first/last char of the keyword is NOT `%`/`_`
|
|
18
|
+
# Wallaby::ActiveRecord::ModelServiceProvider::Querier::Escaper.execute('keyword')
|
|
19
|
+
# # => '%keyword%'
|
|
20
|
+
# @param keyword [String]
|
|
21
|
+
# @return [String] escaped string for LIKE query
|
|
14
22
|
def execute(keyword)
|
|
15
23
|
first = keyword.first
|
|
16
24
|
last = keyword.last
|
|
@@ -23,6 +31,12 @@ module Wallaby
|
|
|
23
31
|
"#{starting}#{escaped}#{ending}"
|
|
24
32
|
end
|
|
25
33
|
|
|
34
|
+
protected
|
|
35
|
+
|
|
36
|
+
# @param first_condition [Boolean] first condition
|
|
37
|
+
# @param first_char [Boolean] first char
|
|
38
|
+
# @param second_condition [nil, String] second condition
|
|
39
|
+
# @param default_sign [String]
|
|
26
40
|
def sign(
|
|
27
41
|
first_condition, first_char, second_condition, default_sign = PCT
|
|
28
42
|
)
|
|
@@ -4,7 +4,9 @@ module Wallaby
|
|
|
4
4
|
class ActiveRecord
|
|
5
5
|
class ModelServiceProvider
|
|
6
6
|
class Querier
|
|
7
|
-
# Wrapper for the
|
|
7
|
+
# Wrapper for the {Wallaby::ActiveRecord::ModelServiceProvider::Querier::Transformer} result.
|
|
8
|
+
# It's only used by {Wallaby::ActiveRecord::ModelServiceProvider::Querier} to tell it apart
|
|
9
|
+
# with non-{Wallaby::ActiveRecord::ModelServiceProvider::Querier::Transformer} result
|
|
8
10
|
class Wrapper
|
|
9
11
|
attr_reader :list
|
|
10
12
|
delegate :push, to: :list
|
|
@@ -12,6 +14,7 @@ module Wallaby
|
|
|
12
14
|
delegate :last, to: :list
|
|
13
15
|
delegate :[], to: :last
|
|
14
16
|
|
|
17
|
+
# @param list [Array]
|
|
15
18
|
def initialize(list = [])
|
|
16
19
|
@list = list
|
|
17
20
|
end
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module Wallaby
|
|
4
4
|
class ActiveRecord
|
|
5
5
|
class ModelServiceProvider
|
|
6
|
-
#
|
|
6
|
+
# Validate values for record create / update
|
|
7
7
|
class Validator
|
|
8
8
|
# @param model_decorator [Wallaby::ModelDecorator]
|
|
9
9
|
def initialize(model_decorator)
|
|
@@ -11,7 +11,8 @@ module Wallaby
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
# @param resource [Object] resource object
|
|
14
|
-
# @return [
|
|
14
|
+
# @return [true] if the resource object is valid
|
|
15
|
+
# @return [false] otherwise
|
|
15
16
|
def valid?(resource)
|
|
16
17
|
resource.attributes.each do |field_name, values|
|
|
17
18
|
metadata = @model_decorator.fields[field_name]
|
|
@@ -22,10 +23,11 @@ module Wallaby
|
|
|
22
23
|
resource.errors.blank?
|
|
23
24
|
end
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
protected
|
|
26
27
|
|
|
27
28
|
# @param values [Array]
|
|
28
|
-
# @return [
|
|
29
|
+
# @return [true] if the values are valid range values
|
|
30
|
+
# @return [false] otherwise
|
|
29
31
|
def valid_range_type?(values, metadata)
|
|
30
32
|
!metadata \
|
|
31
33
|
|| !%w(daterange tsrange tstzrange).include?(metadata[:type]) \
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Wallaby
|
|
4
4
|
class ActiveRecord
|
|
5
|
-
# Pundit provider for ActiveRecord
|
|
5
|
+
# Pundit provider for {Wallaby::ActiveRecord}
|
|
6
6
|
class PunditProvider < PunditAuthorizationProvider
|
|
7
7
|
# Filter a scope
|
|
8
8
|
# @param _action [Symbol, String]
|
|
@@ -11,7 +11,7 @@ module Wallaby
|
|
|
11
11
|
def accessible_for(_action, scope)
|
|
12
12
|
Pundit.policy_scope! user, scope
|
|
13
13
|
rescue Pundit::NotDefinedError
|
|
14
|
-
Logger.warn "Cannot find scope policy for
|
|
14
|
+
Logger.warn "Cannot find scope policy for `#{scope}`."
|
|
15
15
|
scope
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -6,7 +6,6 @@ require 'wallaby/active_record/version'
|
|
|
6
6
|
|
|
7
7
|
# All files required for ActiveRecord ORM
|
|
8
8
|
require 'adapters/wallaby/active_record'
|
|
9
|
-
require 'adapters/wallaby/active_record/logger'
|
|
10
9
|
require 'adapters/wallaby/active_record/model_finder'
|
|
11
10
|
|
|
12
11
|
# ModelPaginationProvider: begin
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wallaby-active_record
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tian Chen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-
|
|
11
|
+
date: 2020-04-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -28,16 +28,16 @@ dependencies:
|
|
|
28
28
|
name: wallaby-core
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - '='
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 0.2.
|
|
33
|
+
version: 0.2.2
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- -
|
|
38
|
+
- - '='
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 0.2.
|
|
40
|
+
version: 0.2.2
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: cancancan
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -106,7 +106,6 @@ files:
|
|
|
106
106
|
- lib/adapters/wallaby/active_record.rb
|
|
107
107
|
- lib/adapters/wallaby/active_record/cancancan_provider.rb
|
|
108
108
|
- lib/adapters/wallaby/active_record/default_provider.rb
|
|
109
|
-
- lib/adapters/wallaby/active_record/logger.rb
|
|
110
109
|
- lib/adapters/wallaby/active_record/model_decorator.rb
|
|
111
110
|
- lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb
|
|
112
111
|
- lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Wallaby
|
|
4
|
-
class ActiveRecord
|
|
5
|
-
# move this to wallaby-core later
|
|
6
|
-
class Logger
|
|
7
|
-
class << self
|
|
8
|
-
%w(debug warn info).each do |method_id|
|
|
9
|
-
define_method method_id do |message, replacements = {}|
|
|
10
|
-
source = caller(replacements.delete(:sourcing).try(:to_i) || 1, 1).first
|
|
11
|
-
Rails.logger.public_send(
|
|
12
|
-
method_id,
|
|
13
|
-
"#{method_id.upcase}: #{format message, replacements}\nfrom #{source}"
|
|
14
|
-
)
|
|
15
|
-
nil
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|