wallaby-core 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/app/controllers/wallaby/resources_controller.rb +10 -1
  4. data/config/locales/wallaby_class.en.yml +2 -2
  5. data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
  6. data/lib/adaptors/wallaby/custom/model_decorator.rb +8 -7
  7. data/lib/adaptors/wallaby/custom/model_finder.rb +3 -2
  8. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
  9. data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -40
  10. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +29 -24
  11. data/lib/authorizers/wallaby/default_authorization_provider.rb +6 -13
  12. data/lib/authorizers/wallaby/model_authorizer.rb +43 -67
  13. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +21 -30
  14. data/lib/concerns/wallaby/application_concern.rb +1 -2
  15. data/lib/concerns/wallaby/authentication_concern.rb +74 -5
  16. data/lib/concerns/wallaby/authorizable.rb +8 -8
  17. data/lib/concerns/wallaby/baseable.rb +91 -10
  18. data/lib/concerns/wallaby/decoratable.rb +3 -3
  19. data/lib/concerns/wallaby/engineable.rb +1 -1
  20. data/lib/concerns/wallaby/fieldable.rb +4 -4
  21. data/lib/concerns/wallaby/paginatable.rb +3 -3
  22. data/lib/concerns/wallaby/resourcable.rb +0 -35
  23. data/lib/concerns/wallaby/resources_concern.rb +3 -2
  24. data/lib/concerns/wallaby/servicable.rb +4 -4
  25. data/lib/decorators/wallaby/resource_decorator.rb +53 -80
  26. data/lib/errors/wallaby/class_not_found.rb +6 -0
  27. data/lib/errors/wallaby/model_not_found.rb +2 -0
  28. data/lib/helpers/wallaby/resources_helper.rb +3 -0
  29. data/lib/helpers/wallaby/secure_helper.rb +3 -3
  30. data/lib/interfaces/wallaby/mode.rb +3 -3
  31. data/lib/interfaces/wallaby/model_authorization_provider.rb +15 -13
  32. data/lib/interfaces/wallaby/model_decorator.rb +15 -3
  33. data/lib/paginators/wallaby/model_paginator.rb +14 -45
  34. data/lib/servicers/wallaby/model_servicer.rb +31 -62
  35. data/lib/services/wallaby/map/mode_mapper.rb +14 -14
  36. data/lib/services/wallaby/map/model_class_collector.rb +1 -1
  37. data/lib/services/wallaby/map/model_class_mapper.rb +7 -26
  38. data/lib/services/wallaby/type_renderer.rb +0 -10
  39. data/lib/utils/wallaby/model_utils.rb +4 -3
  40. data/lib/utils/wallaby/utils.rb +9 -8
  41. data/lib/wallaby/class_array.rb +75 -0
  42. data/lib/wallaby/class_hash.rb +94 -0
  43. data/lib/wallaby/classifier.rb +29 -0
  44. data/lib/wallaby/configuration.rb +31 -2
  45. data/lib/wallaby/configuration/mapping.rb +33 -21
  46. data/lib/wallaby/configuration/metadata.rb +1 -1
  47. data/lib/wallaby/configuration/models.rb +5 -9
  48. data/lib/wallaby/configuration/security.rb +6 -3
  49. data/lib/wallaby/configuration/sorting.rb +1 -1
  50. data/lib/wallaby/core.rb +13 -7
  51. data/lib/wallaby/core/version.rb +1 -1
  52. data/lib/wallaby/engine.rb +9 -20
  53. data/lib/wallaby/logger.rb +35 -0
  54. data/lib/wallaby/map.rb +20 -17
  55. data/lib/wallaby/preloader.rb +77 -0
  56. metadata +8 -4
  57. data/lib/utils/wallaby/logger.rb +0 -21
  58. data/lib/utils/wallaby/preload_utils.rb +0 -44
@@ -157,12 +157,24 @@ module Wallaby
157
157
  field_names.unshift(primary_key.to_s).uniq
158
158
  end
159
159
 
160
- # Validate presence of a type for given field name
160
+ # Ensure the type is present for given field name
161
161
  # @param type [String, Symbol, nil]
162
162
  # @return [String, Symbol] type
163
163
  # @raise [ArgumentError] when type is nil
164
- def validate_presence_of(field_name, type)
165
- type || raise(::ArgumentError, Locale.t('errors.invalid.type_required', field_name: field_name))
164
+ def ensure_type_is_present(field_name, type, metadata_prefix = '')
165
+ type || raise(::ArgumentError, <<~INSTRUCTION
166
+ The type for field `#{field_name}` is missing in metadata `#{metadata_prefix}_fields`.
167
+ The possible causes are:
168
+
169
+ 1. Check type's value from metadata `#{metadata_prefix}_fields[:#{field_name}][:type]`.
170
+ If it is missing, specify the type as below:
171
+
172
+ #{metadata_prefix}field_name[:#{field_name}][:type] = 'string'
173
+
174
+ 2. If metadata `#{metadata_prefix}_fields` is blank, maybe table hasn't be created yet
175
+ or there is some error in the decorator class declaration.
176
+ INSTRUCTION
177
+ )
166
178
  end
167
179
  end
168
180
  end
@@ -1,72 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Model paginator to provide support for pagination on index page
4
+ # This is the base paginator class to provider pagination for given collection.
5
+ #
6
+ # For best practice, please create an application paginator class (see example)
7
+ # to better control the functions shared between different model paginators.
8
+ # @example Create an application class for Admin Interface usage
9
+ # class Admin::ApplicationPaginator < Wallaby::ModelPaginator
10
+ # base_class!
11
+ # end
5
12
  class ModelPaginator
6
13
  extend Baseable::ClassMethods
7
-
8
- class << self
9
- # @!attribute [w] model_class
10
- attr_writer :model_class
11
-
12
- # @!attribute [r] model_class
13
- # Return associated model class, e.g. return **Product** for **ProductPaginator**.
14
- #
15
- # If Wallaby can't recognise the model class for Paginator, it's required to be configured as below example:
16
- # @example To configure model class
17
- # class Admin::ProductPaginator < Admin::ApplicationPaginator
18
- # self.model_class = Product
19
- # end
20
- # @example To configure model class for version below 5.2.0
21
- # class Admin::ProductPaginator < Admin::ApplicationPaginator
22
- # def self.model_class
23
- # Product
24
- # end
25
- # end
26
- # @return [Class] assoicated model class
27
- # @return [nil] if current class is marked as base class
28
- # @return [nil] if current class is the same as the value of {Wallaby::Configuration::Mapping#model_paginator}
29
- # @return [nil] if current class is {Wallaby::ModelPaginator}
30
- # @return [nil] if assoicated model class is not found
31
- def model_class
32
- return unless self < ModelPaginator
33
- return if base_class? || self == Wallaby.configuration.mapping.model_paginator
34
-
35
- @model_class ||= Map.model_class_map(name.gsub(/(^#{namespace}::)|(Paginator$)/, EMPTY_STRING))
36
- end
37
-
38
- # @!attribute provider_class
39
- # @return [Class] pagination provider class
40
- # @since 5.2.0
41
- attr_accessor :provider_class
42
- end
14
+ base_class!
43
15
 
44
16
  # @!attribute [r] model_class
45
17
  # @return [Class]
46
18
  attr_reader :model_class
47
19
 
48
20
  # @!attribute [r] provider
49
- # @return [Wallaby::ModelServiceProvider]
50
- # @since 5.2.0
21
+ # @return [Wallaby::ModelPaginationProvider] the instance that does the job
22
+ # @since wallaby-5.2.0
51
23
  attr_reader :provider
52
24
 
53
- # During initialization, Wallaby will assign a pagination provider for this paginator
54
- # to carry out the actual execution.
55
- #
56
- # Therefore, all its actions can be completely replaced by user's own implemnetation.
57
25
  # @param model_class [Class]
58
26
  # @param collection [#to_a] a collection of the resources
59
27
  # @param params [ActionController::Parameters]
60
28
  def initialize(model_class, collection, params)
61
29
  @model_class = self.class.model_class || model_class
62
- raise ArgumentError, Locale.t('errors.required', subject: 'model_class') unless @model_class
30
+ raise ArgumentError, 'Please provide a `model_class`.' unless @model_class
63
31
 
64
32
  @collection = collection
65
33
  @params = params
66
34
  @provider = Map.pagination_provider_map(@model_class).new(@collection, @params)
67
35
  end
68
36
 
69
- delegate(*ModelPaginationProvider.instance_methods(false), to: :provider)
70
37
  # @!method paginatable?
71
38
  # (see Wallaby::ModelPaginationProvider#paginatable?)
72
39
 
@@ -111,5 +78,7 @@ module Wallaby
111
78
 
112
79
  # @!method number_of_pages
113
80
  # (see Wallaby::ModelPaginationProvider#number_of_pages)
81
+
82
+ delegate(*ModelPaginationProvider.instance_methods(false), to: :provider)
114
83
  end
115
84
  end
@@ -1,45 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Model servicer contains resourceful operations for Rails resourceful actions.
4
+ # This is the base servicer class to provider data source related operations
5
+ # for given/associated model. In general, it works together with {#authorizer}
6
+ # to ensure that all operations are legitmate.
7
+ #
8
+ # For best practice, please create an application servicer class (see example)
9
+ # to better control the functions shared between different model servicers.
10
+ # @example Create an application class for Admin Interface usage
11
+ # class Admin::ApplicationServicer < Wallaby::ModelServicer
12
+ # base_class!
13
+ # end
5
14
  class ModelServicer
6
15
  extend Baseable::ClassMethods
7
-
8
- class << self
9
- # @!attribute [w] model_class
10
- attr_writer :model_class
11
-
12
- # @!attribute [r] model_class
13
- # Return associated model class, e.g. return **Product** for **ProductServicer**.
14
- #
15
- # If Wallaby can't recognise the model class for Servicer, it's required to be configured as below example:
16
- # @example To configure model class
17
- # class Admin::ProductServicer < Admin::ApplicationServicer
18
- # self.model_class = Product
19
- # end
20
- # @example To configure model class for version below 5.2.0
21
- # class Admin::ProductServicer < Admin::ApplicationServicer
22
- # def self.model_class
23
- # Product
24
- # end
25
- # end
26
- # @return [Class] assoicated model class
27
- # @return [nil] if current class is marked as base class
28
- # @return [nil] if current class is the same as the value of {Wallaby::Configuration::Mapping#model_servicer}
29
- # @return [nil] if current class is {Wallaby::ModelServicer}
30
- # @return [nil] if assoicated model class is not found
31
- def model_class
32
- return unless self < ModelServicer
33
- return if base_class? || self == Wallaby.configuration.mapping.model_servicer
34
-
35
- @model_class ||= Map.model_class_map(name.gsub(/(^#{namespace}::)|(Servicer$)/, EMPTY_STRING))
36
- end
37
-
38
- # @!attribute provider_class
39
- # @return [Class] service provider class
40
- # @since 5.2.0
41
- attr_accessor :provider_class
42
- end
16
+ base_class!
43
17
 
44
18
  # @!attribute [r] model_class
45
19
  # @return [Class]
@@ -47,46 +21,41 @@ module Wallaby
47
21
 
48
22
  # @!attribute [r] model_decorator
49
23
  # @return [Wallaby::ModelDecorator]
50
- # @since 5.2.0
24
+ # @since wallaby-5.2.0
51
25
  attr_reader :model_decorator
52
26
 
53
27
  # @!attribute [r] authorizer
54
28
  # @return [Wallaby::ModelAuthorizer]
55
- # @since 5.2.0
29
+ # @since wallaby-5.2.0
56
30
  attr_reader :authorizer
57
31
 
58
32
  # @!attribute [r] provider
59
- # @return [Wallaby::ModelServiceProvider]
60
- # @since 5.2.0
33
+ # @return [Wallaby::ModelServiceProvider] the instance that does the job
34
+ # @since wallaby-5.2.0
61
35
  attr_reader :provider
62
36
 
63
37
  # @!method user
64
38
  # @return [Object]
65
- # @since 5.2.0
39
+ # @since wallaby-5.2.0
66
40
  delegate :user, to: :authorizer
67
41
 
68
- # During initialization, Wallaby will assign a service provider for this servicer
69
- # to carry out the actual execution.
70
- #
71
- # Therefore, all its actions can be completely replaced by user's own implemnetation.
72
42
  # @param model_class [Class]
73
43
  # @param authorizer [Wallaby::ModelAuthorizer]
74
44
  # @param model_decorator [Wallaby::ModelDecorator]
75
- # @raise [ArgumentError] if param model_class is blank
45
+ # @raise [ArgumentError] if model_class is blank
76
46
  def initialize(model_class, authorizer, model_decorator = nil)
77
47
  @model_class = model_class || self.class.model_class
78
- raise ArgumentError, Locale.t('errors.required', subject: 'model_class') unless @model_class
48
+ raise ArgumentError, 'Please provide a `model_class`.' unless @model_class
79
49
 
80
50
  @model_decorator = model_decorator || Map.model_decorator_map(model_class)
81
51
  @authorizer = authorizer
82
- provider_class = self.class.provider_class || Map.service_provider_map(@model_class)
83
- @provider = provider_class.new(@model_class, @model_decorator)
52
+ @provider = Map.service_provider_map(@model_class).new(@model_class, @model_decorator)
84
53
  end
85
54
 
86
55
  # @note This is a template method that can be overridden by subclasses.
87
56
  # Whitelist parameters for mass assignment.
88
57
  # @param params [ActionController::Parameters, Hash]
89
- # @param action [String, Symbol]
58
+ # @param action [String, Symbol, nil]
90
59
  # @return [ActionController::Parameters] permitted params
91
60
  def permit(params, action = nil)
92
61
  provider.permit params, action, authorizer
@@ -95,7 +64,7 @@ module Wallaby
95
64
  # @note This is a template method that can be overridden by subclasses.
96
65
  # Return a collection by querying the datasource (e.g. database, REST API).
97
66
  # @param params [ActionController::Parameters, Hash]
98
- # @return [Enumerable] list of records
67
+ # @return [Enumerable] list of resources
99
68
  def collection(params)
100
69
  provider.collection params, authorizer
101
70
  end
@@ -105,49 +74,49 @@ module Wallaby
105
74
  # Paginate given {#collection}.
106
75
  # @param query [Enumerable]
107
76
  # @param params [ActionController::Parameters]
108
- # @return [Enumerable] list of records
77
+ # @return [Enumerable] list of resources
109
78
  delegate :paginate, to: :provider
110
79
 
111
80
  # @note This is a template method that can be overridden by subclasses.
112
81
  # Initialize an instance of the model class.
113
82
  # @param params [ActionController::Parameters]
114
- # @return [Object] initialized object
83
+ # @return [Object] initialized resource
115
84
  def new(params)
116
85
  provider.new params, authorizer
117
86
  end
118
87
 
119
88
  # @note This is a template method that can be overridden by subclasses.
120
- # To find a record.
89
+ # To find a resource.
121
90
  # @param id [Object]
122
91
  # @param params [ActionController::Parameters]
123
- # @return [Object] resource object
92
+ # @return [Object] found resource
124
93
  def find(id, params)
125
94
  provider.find id, params, authorizer
126
95
  end
127
96
 
128
97
  # @note This is a template method that can be overridden by subclasses.
129
- # To create a record.
98
+ # To create a resource.
130
99
  # @param resource [Object]
131
100
  # @param params [ActionController::Parameters]
132
- # @return [Object] resource object
101
+ # @return [Object] created resource
133
102
  def create(resource, params)
134
103
  provider.create resource, params, authorizer
135
104
  end
136
105
 
137
106
  # @note This is a template method that can be overridden by subclasses.
138
- # To update a record.
107
+ # To update a resource.
139
108
  # @param resource [Object]
140
109
  # @param params [ActionController::Parameters]
141
- # @return [Object] resource object
110
+ # @return [Object] resource
142
111
  def update(resource, params)
143
112
  provider.update resource, params, authorizer
144
113
  end
145
114
 
146
115
  # @note This is a template method that can be overridden by subclasses.
147
- # To delete a record.
116
+ # To delete a resource.
148
117
  # @param resource [Object]
149
118
  # @param params [ActionController::Parameters]
150
- # @return [Object] resource object
119
+ # @return [Object] resource
151
120
  def destroy(resource, params)
152
121
  provider.destroy resource, params, authorizer
153
122
  end
@@ -2,23 +2,23 @@
2
2
 
3
3
  module Wallaby
4
4
  class Map
5
- # To generate a hash map (`model` => `mode`).
6
- # This will be used to tell if a model can be handled by Wallaby
5
+ # Go through each {Wallaby::Mode} (e.g. **ActiveRecord**/**Her**)
6
+ # and find out all the model classes respectively.
7
+ # Then a hash (Model => {Wallaby::Mode}) is constructed
8
+ # to tell {Wallaby} which {Wallaby::Mode} to use for a given model.
7
9
  class ModeMapper
8
- # @param mode_classes [Array<Class>] model classes
9
- def initialize(mode_classes)
10
- @mode_classes = mode_classes
11
- end
10
+ extend Classifier
12
11
 
13
- # This will walk through each mode (e.g. **ActiveRecord**/**Her**) then pull out all the models,
14
- # and then form a hash of (`model` => `mode`).
15
- # @return [Hash] { model_class => mode }
16
- def map
17
- return {} if @mode_classes.blank?
12
+ # @param class_names [Wallaby::ClassArray] mode class names
13
+ # @return [WallabyClassHash]
14
+ def self.execute(class_names)
15
+ ClassHash.new.tap do |hash|
16
+ next if class_names.blank?
18
17
 
19
- @mode_classes.each_with_object({}) do |mode_class, map|
20
- mode_class.model_finder.new.all.each do |model_class|
21
- map[model_class] = mode_class
18
+ class_names.each_with_object(hash) do |mode_name, map|
19
+ mode_name.model_finder.new.all.each do |model_class|
20
+ map[model_class] = mode_name
21
+ end
22
22
  end
23
23
  end
24
24
  end
@@ -8,7 +8,7 @@ module Wallaby
8
8
  # @param models [Array<Class>]
9
9
  def initialize(configuration, models = nil)
10
10
  @configuration = configuration
11
- @models = models || []
11
+ @models = ClassArray.new(models || [])
12
12
  end
13
13
 
14
14
  # @return [Array<Class>] model class
@@ -2,37 +2,18 @@
2
2
 
3
3
  module Wallaby
4
4
  class Map
5
- # Generate a map.
5
+ # Go through the class list and generate a {.map .map} that uses the class's model_class as the key.
6
6
  class ModelClassMapper
7
- # Iterate all classes and generate a hash using their model classes as the key
8
- # @see #map
9
7
  # @param class_array [Array<Class>]
10
- # @return [Hash] model class => descendant class
11
- def self.map(class_array, &block)
12
- new.send :map, class_array, &block
13
- end
14
-
15
- protected
8
+ # @return [Wallaby::ClassHash] model class => descendant class
9
+ def self.map(class_array)
10
+ (class_array || EMPTY_ARRAY).each_with_object(ClassHash.new) do |klass, hash|
11
+ next if ModuleUtils.anonymous_class?(klass)
12
+ next if klass.try(:base_class?) || klass.model_class.blank?
16
13
 
17
- # @return [Hash] model class => descendant class
18
- def map(class_array)
19
- (class_array || EMPTY_ARRAY).each_with_object({}) do |klass, map|
20
- next if anonymous?(klass) || base_class?(klass) || !klass.model_class
21
-
22
- map[klass.model_class] = block_given? ? yield(klass) : klass
14
+ hash[klass.model_class] = block_given? ? yield(klass) : klass
23
15
  end
24
16
  end
25
-
26
- # @see Wallaby::ModuleUtils.anonymous_class?
27
- def anonymous?(klass)
28
- ModuleUtils.anonymous_class? klass
29
- end
30
-
31
- # @param klass [Class]
32
- # @return [Boolean] whether the class is base or not
33
- def base_class?(klass)
34
- ModuleUtils.try_to klass, :base_class?
35
- end
36
17
  end
37
18
  end
38
19
  end
@@ -35,16 +35,6 @@ module Wallaby
35
35
  locals[:metadata] = locals[:object].public_send :"#{action_name}_metadata_of", locals[:field_name]
36
36
  locals[:value] = locals[:object].public_send locals[:field_name]
37
37
  end
38
-
39
- # @param options [String]
40
- # @param view [ActionView]
41
- # @return [String] partial path string
42
- # @return [String] blank string
43
- def find_partial(options, view)
44
- formats = [view.request.format.to_sym]
45
- lookup = view.lookup_context
46
- lookup.find_template options, lookup.prefixes, true, [], formats: formats
47
- end
48
38
  end
49
39
  end
50
40
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Utils for model
4
+ # Model related utils
5
5
  module ModelUtils
6
6
  class << self
7
7
  # Convert model class (e.g. `Namespace::Product`) into resources name (e.g. `namespace::products`)
@@ -32,10 +32,11 @@ module Wallaby
32
32
  return if resources_name.blank?
33
33
 
34
34
  class_name = to_model_name resources_name
35
+ # NOTE: DO NOT try to use const_defined? and const_get EVER.
36
+ # This is Rails, use constantize
35
37
  class_name.constantize
36
38
  rescue NameError
37
- Logger.warn Locale.t('errors.not_found.model', model: class_name), sourcing: 2
38
- nil
39
+ Logger.warn Locale.t('errors.not_found.model', model: class_name), sourcing: 2..10
39
40
  end
40
41
 
41
42
  # Convert resources name (e.g. `namespace::products`) into model name (e.g. `Namespace::Product`)
@@ -3,19 +3,20 @@
3
3
  module Wallaby
4
4
  # Utils
5
5
  module Utils
6
- # Display deprecate message including the line where it's used
7
- # @param key [String]
8
- # @param caller [String] the line where it's called
9
- # @param options [Hash]
10
- def self.deprecate(key, caller:, options: {})
11
- warn Locale.t(key, options.merge(from: caller[0]))
12
- end
13
-
14
6
  # @see http://stackoverflow.com/a/8710663/1326499
15
7
  # @param object [Object]
16
8
  # @return [Object] a clone object
17
9
  def self.clone(object)
18
10
  ::Marshal.load(::Marshal.dump(object))
19
11
  end
12
+
13
+ # @param object [Object, nil]
14
+ # @return [String] inspection string for the given object
15
+ def self.inspect(object)
16
+ return 'nil' unless object
17
+ return object.name if object.is_a? Class
18
+
19
+ "#{object.class}##{object.id}"
20
+ end
20
21
  end
21
22
  end