wallaby-core 0.2.0 → 0.2.1

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/wallaby/resources_controller.rb +6 -375
  3. data/app/security/ability.rb +1 -1
  4. data/config/locales/wallaby.en.yml +92 -128
  5. data/config/locales/wallaby_class.en.yml +0 -21
  6. data/lib/adaptors/wallaby/custom/model_service_provider.rb +8 -8
  7. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +2 -1
  8. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +2 -2
  9. data/lib/concerns/wallaby/application_concern.rb +111 -0
  10. data/lib/concerns/wallaby/authentication_concern.rb +93 -0
  11. data/lib/concerns/wallaby/authorizable.rb +1 -1
  12. data/lib/concerns/wallaby/decoratable.rb +1 -1
  13. data/lib/concerns/wallaby/paginatable.rb +1 -1
  14. data/lib/concerns/wallaby/resourcable.rb +4 -0
  15. data/lib/concerns/wallaby/resources_concern.rb +433 -0
  16. data/lib/concerns/wallaby/servicable.rb +1 -1
  17. data/lib/errors/wallaby/model_not_found.rb +1 -1
  18. data/lib/errors/wallaby/resource_not_found.rb +1 -1
  19. data/lib/helpers/wallaby/application_helper.rb +6 -0
  20. data/lib/helpers/wallaby/form_helper.rb +2 -3
  21. data/lib/helpers/wallaby/index_helper.rb +2 -2
  22. data/lib/helpers/wallaby/links_helper.rb +5 -5
  23. data/lib/helpers/wallaby/styling_helper.rb +17 -3
  24. data/lib/interfaces/wallaby/mode.rb +2 -2
  25. data/lib/interfaces/wallaby/model_decorator.rb +1 -1
  26. data/lib/paginators/wallaby/model_paginator.rb +1 -1
  27. data/lib/routes/wallaby/resources_router.rb +1 -1
  28. data/lib/servicers/wallaby/model_servicer.rb +2 -1
  29. data/lib/services/wallaby/map/model_class_collector.rb +1 -1
  30. data/lib/services/wallaby/type_renderer.rb +2 -2
  31. data/lib/utils/wallaby/locale.rb +53 -0
  32. data/lib/utils/wallaby/logger.rb +21 -0
  33. data/lib/utils/wallaby/model_utils.rb +1 -1
  34. data/lib/utils/wallaby/module_utils.rb +1 -1
  35. data/lib/utils/wallaby/utils.rb +1 -1
  36. data/lib/wallaby/core.rb +6 -0
  37. data/lib/wallaby/core/version.rb +1 -1
  38. data/lib/wallaby/engine.rb +3 -3
  39. data/lib/wallaby/map.rb +1 -1
  40. metadata +7 -4
  41. data/app/controllers/wallaby/application_controller.rb +0 -84
  42. data/app/controllers/wallaby/secure_controller.rb +0 -81
@@ -54,7 +54,7 @@ module Wallaby
54
54
  @current_servicer ||=
55
55
  (controller_to_get(:model_servicer) \
56
56
  || Map.servicer_map(current_model_class, controller_to_get(:application_servicer))).try do |klass|
57
- Rails.logger.info %( - Current servicer: #{klass})
57
+ Logger.debug %(Current servicer: #{klass})
58
58
  klass.new current_model_class, current_authorizer, current_model_decorator
59
59
  end
60
60
  end
@@ -5,7 +5,7 @@ module Wallaby
5
5
  class ModelNotFound < NotFound
6
6
  # @return [String] not found error message
7
7
  def message
8
- I18n.t 'errors.not_found.model', model: super
8
+ Locale.t 'errors.not_found.model', model: super
9
9
  end
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module Wallaby
5
5
  class ResourceNotFound < NotFound
6
6
  # @return [String] resource not found error message
7
7
  def message
8
- I18n.t 'errors.not_found.resource', resource: super
8
+ Locale.t 'errors.not_found.resource', resource: super
9
9
  end
10
10
  end
11
11
  end
@@ -66,5 +66,11 @@ module Wallaby
66
66
  options = default_options.merge!(sources.extract_options!.stringify_keys)
67
67
  super(*sources, options)
68
68
  end
69
+
70
+ # @param key
71
+ # @param options [Hash]
72
+ def wt(key, options = {})
73
+ Locale.t key, { translator: method(:t) }.merge(options)
74
+ end
69
75
  end
70
76
  end
@@ -46,9 +46,8 @@ module Wallaby
46
46
  type = metadata[:type]
47
47
  hint = metadata[:hint]
48
48
  # @see http://guides.rubyonrails.org/i18n.html#using-safe-html-translations
49
- hint ||= type && t("hints.#{type}_html", default: '').presence
50
- hint ||= type && t("hints.#{type}", default: '').presence
51
- return unless hint
49
+ hint ||= type && wt("hints.#{type}_html", default: [:"hints.#{type}", ''])
50
+ return if hint.blank?
52
51
 
53
52
  content_tag :p, hint, class: 'help-block'
54
53
  end
@@ -6,7 +6,7 @@ module Wallaby
6
6
  # Just a label
7
7
  # @return [String]
8
8
  def all_label
9
- t 'filters.all'
9
+ wt 'filters.all'
10
10
  end
11
11
 
12
12
  # If `:fields` parameter is given, only display fields that is in `index_field_names`
@@ -58,7 +58,7 @@ module Wallaby
58
58
  # @return [String] Export link for the given model_class.
59
59
  def export_link(model_class, url_params: {})
60
60
  index_link(model_class, url_params: { format: 'csv', page: nil, per: nil, with_query: true }.merge(url_params)) do
61
- t 'links.export', ext: 'CSV'
61
+ wt 'links.export', ext: 'CSV'
62
62
  end
63
63
  end
64
64
 
@@ -44,7 +44,7 @@ module Wallaby
44
44
  html_options, block = LinkOptionsNormalizer.normalize(
45
45
  html_options, block,
46
46
  class: 'resource__create',
47
- block: -> { t 'links.new', model: to_model_label(model_class) }
47
+ block: -> { wt 'links.new', model: to_model_label(model_class) }
48
48
  )
49
49
 
50
50
  url = options[:url] || new_path(model_class, url_params: url_params)
@@ -97,7 +97,7 @@ module Wallaby
97
97
  html_options, block = LinkOptionsNormalizer.normalize(
98
98
  html_options, block,
99
99
  class: 'resource__update',
100
- block: -> { "#{t 'links.edit'} #{decorate(resource).to_label}" }
100
+ block: -> { "#{wt 'links.edit'} #{decorate(resource).to_label}" }
101
101
  )
102
102
 
103
103
  default = options[:readonly] && block.call || nil
@@ -123,11 +123,11 @@ module Wallaby
123
123
  return if unauthorized? :destroy, extract(resource)
124
124
 
125
125
  html_options, block = LinkOptionsNormalizer.normalize(
126
- html_options, block, class: 'resource__destroy', block: -> { t 'links.delete' }
126
+ html_options, block, class: 'resource__destroy', block: -> { wt 'links.delete' }
127
127
  )
128
128
 
129
129
  html_options[:method] ||= :delete
130
- (html_options[:data] ||= {})[:confirm] ||= t 'links.confirm.delete'
130
+ (html_options[:data] ||= {})[:confirm] ||= wt 'links.confirm.delete'
131
131
 
132
132
  url = options[:url] || show_path(resource, is_resource: options[:is_resource], url_params: url_params)
133
133
  link_to url, html_options, &block
@@ -140,7 +140,7 @@ module Wallaby
140
140
  # @yield block to return the link label
141
141
  # @return [String] anchor link of cancel action
142
142
  def cancel_link(html_options: {}, &block)
143
- block ||= -> { t 'links.cancel' }
143
+ block ||= -> { wt 'links.cancel' }
144
144
  link_to 'javascript:history.back()', html_options, &block
145
145
  end
146
146
 
@@ -3,6 +3,20 @@
3
3
  module Wallaby
4
4
  # Helper methods to build custom elements
5
5
  module StylingHelper
6
+ # backforward compatible with FontAwesome 4
7
+ # @see this fa migration document https://fontawesome.com/how-to-use/on-the-web/setup/upgrading-from-version-4
8
+ FONT_AWESOME_MAPPING = {
9
+ 4 => {
10
+ clock: 'clock-o',
11
+ bars: 'navicon',
12
+ calendar: 'calendar-o',
13
+ 'check-square': 'check-square-o',
14
+ square: 'square-o',
15
+ link: 'chain',
16
+ user: 'user-o'
17
+ }
18
+ }.with_indifferent_access.freeze
19
+
6
20
  # Shortcut to build up the HTML options as keyword arguments
7
21
  # @param string_or_array [String, Array<String>]
8
22
  # @param options [Hash]
@@ -54,12 +68,12 @@ module Wallaby
54
68
 
55
69
  # @return [String] grey null
56
70
  def null
57
- muted t 'labels.empty'
71
+ muted wt 'labels.empty'
58
72
  end
59
73
 
60
74
  # @return [String] grey N/A
61
75
  def na
62
- muted t 'labels.na'
76
+ muted wt 'labels.na'
63
77
  end
64
78
 
65
79
  # Grey text
@@ -74,7 +88,7 @@ module Wallaby
74
88
  def fa_map(name, major = nil)
75
89
  @map ||= begin
76
90
  major ||= Gem.loaded_specs['font-awesome-sass'].try(:version).try(:segments).try(:first)
77
- t("fa.v#{major}").with_indifferent_access
91
+ FONT_AWESOME_MAPPING[major] || {}
78
92
  end
79
93
  @map[name] || name
80
94
  end
@@ -61,10 +61,10 @@ module Wallaby
61
61
  class_name.constantize.tap do |klass|
62
62
  next if klass < parent_class
63
63
 
64
- raise InvalidError, I18n.t('wallaby.mode.inherit_required', klass: klass, parent: parent_class)
64
+ raise InvalidError, Locale.t('mode.inherit_required', klass: klass, parent: parent_class)
65
65
  end
66
66
  rescue NameError => e
67
- Rails.logger.error e
67
+ Logger.error e
68
68
  raise NotImplemented, class_name
69
69
  end
70
70
  end
@@ -162,7 +162,7 @@ module Wallaby
162
162
  # @return [String, Symbol] type
163
163
  # @raise [ArgumentError] when type is nil
164
164
  def validate_presence_of(field_name, type)
165
- type || raise(::ArgumentError, I18n.t('errors.invalid.type_required', field_name: field_name))
165
+ type || raise(::ArgumentError, Locale.t('errors.invalid.type_required', field_name: field_name))
166
166
  end
167
167
  end
168
168
  end
@@ -59,7 +59,7 @@ module Wallaby
59
59
  # @param params [ActionController::Parameters]
60
60
  def initialize(model_class, collection, params)
61
61
  @model_class = self.class.model_class || model_class
62
- raise ArgumentError, I18n.t('errors.required', subject: 'model_class') unless @model_class
62
+ raise ArgumentError, Locale.t('errors.required', subject: 'model_class') unless @model_class
63
63
 
64
64
  @collection = collection
65
65
  @params = params
@@ -53,7 +53,7 @@ module Wallaby
53
53
  return model_class unless MODEL_ACTIONS.include? params[:action].to_sym
54
54
  raise ModelNotFound, params[:resources] unless model_class
55
55
  unless Map.mode_map[model_class]
56
- raise UnprocessableEntity, I18n.t('errors.unprocessable_entity.model', model: model_class)
56
+ raise UnprocessableEntity, Locale.t('errors.unprocessable_entity.model', model: model_class)
57
57
  end
58
58
 
59
59
  model_class
@@ -75,7 +75,7 @@ module Wallaby
75
75
  # @raise [ArgumentError] if param model_class is blank
76
76
  def initialize(model_class, authorizer, model_decorator = nil)
77
77
  @model_class = model_class || self.class.model_class
78
- raise ArgumentError, I18n.t('errors.required', subject: 'model_class') unless @model_class
78
+ raise ArgumentError, Locale.t('errors.required', subject: 'model_class') unless @model_class
79
79
 
80
80
  @model_decorator = model_decorator || Map.model_decorator_map(model_class)
81
81
  @authorizer = authorizer
@@ -100,6 +100,7 @@ module Wallaby
100
100
  provider.collection params, authorizer
101
101
  end
102
102
 
103
+ # @!method paginate(query, params)
103
104
  # @note This is a template method that can be overridden by subclasses.
104
105
  # Paginate given {#collection}.
105
106
  # @param query [Enumerable]
@@ -26,7 +26,7 @@ module Wallaby
26
26
  invalid_models = configured_models - @models
27
27
  return if invalid_models.blank?
28
28
 
29
- message = I18n.t 'errors.invalid.models', models: invalid_models.to_sentence
29
+ message = Locale.t 'errors.invalid.models', models: invalid_models.to_sentence
30
30
  raise InvalidError, message
31
31
  end
32
32
 
@@ -23,8 +23,8 @@ module Wallaby
23
23
  # @raise [ArgumentError] if field_name is not provided
24
24
  # @raise [ArgumentError] if object is not decorated
25
25
  def check(locals)
26
- raise ArgumentError, I18n.t('errors.required', subject: 'form') if locals.key?(:form) && locals[:form].blank?
27
- raise ArgumentError, I18n.t('errors.required', subject: 'field_name') if locals[:field_name].blank?
26
+ raise ArgumentError, Locale.t('errors.required', subject: 'form') if locals.key?(:form) && locals[:form].blank?
27
+ raise ArgumentError, Locale.t('errors.required', subject: 'field_name') if locals[:field_name].blank?
28
28
  raise ArgumentError, 'Object is not decorated.' unless locals[:object].is_a? ResourceDecorator
29
29
  end
30
30
 
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Locale related
5
+ module Locale
6
+ class << self
7
+ # Extend translation just for Wallaby
8
+ # so that translation can be prefixed with `wallaby.`
9
+ # @param key [String, Symbol, Array]
10
+ # @param options [Hash] the rest of the arguments
11
+ # @return [String] translation
12
+ def t(key, options = {})
13
+ translator = options.delete(:translator) || I18n.method(:t)
14
+ return translator.call(key, options) unless key.is_a?(String) || key.is_a?(Symbol)
15
+
16
+ new_key, new_defaults = normalize key, options.delete(:default)
17
+
18
+ translator.call(new_key, { default: new_defaults }.merge(options))
19
+ end
20
+
21
+ private
22
+
23
+ # @param key [String, Symbol, Array]
24
+ # @param defaults [String, Symbol, Array]
25
+ # @return [Array]
26
+ def normalize(key, defaults)
27
+ *keys, default = *defaults
28
+
29
+ # default will NOT be considered as one of the key
30
+ # if it is not set or if it is a String (since String means translation for I18n.t)
31
+ unless default.nil? || default.is_a?(String)
32
+ keys << default
33
+ default = nil
34
+ end
35
+
36
+ new_defaults = prefix_defaults_from keys.unshift(key)
37
+ new_key = new_defaults.shift
38
+ new_defaults << default if default
39
+ [new_key, new_defaults]
40
+ end
41
+
42
+ # Duplicate and prefix the keys respectively
43
+ # @param keys [Array]
44
+ # @return [Array] new_keys
45
+ def prefix_defaults_from(keys)
46
+ keys.each_with_object([]) do |k, result|
47
+ result << :"wallaby.#{k}" unless k.to_s.start_with? 'wallaby.'
48
+ result << k.to_sym
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Custom logger
5
+ module Logger
6
+ class << self
7
+ %w(unknown fatal error warn info debug).each do |method_id|
8
+ define_method method_id do |message, replacements = {}|
9
+ message = message.inspect unless message.is_a? String
10
+ sourcing = replacements.delete :sourcing # sourcing can be set to false
11
+
12
+ from = "\nfrom #{caller[sourcing || 0]}" unless sourcing == false
13
+ Rails.logger.public_send(
14
+ method_id, "#{method_id.upcase}: #{format message, replacements}#{from}"
15
+ )
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -34,7 +34,7 @@ module Wallaby
34
34
  class_name = to_model_name resources_name
35
35
  class_name.constantize
36
36
  rescue NameError
37
- Rails.logger.warn I18n.t('errors.not_found.model', model: class_name)
37
+ Logger.warn Locale.t('errors.not_found.model', model: class_name), sourcing: 2
38
38
  nil
39
39
  end
40
40
 
@@ -32,7 +32,7 @@ module Wallaby
32
32
  return unless child && parent
33
33
  return if child < parent
34
34
 
35
- raise ::ArgumentError, I18n.t('errors.invalid.inheritance', klass: child, parent: parent)
35
+ raise ::ArgumentError, Locale.t('errors.invalid.inheritance', klass: child, parent: parent)
36
36
  end
37
37
 
38
38
  # If block is given, run the block. Otherwise, return subject
@@ -8,7 +8,7 @@ module Wallaby
8
8
  # @param caller [String] the line where it's called
9
9
  # @param options [Hash]
10
10
  def self.deprecate(key, caller:, options: {})
11
- warn I18n.t(key, options.merge(from: caller[0]))
11
+ warn Locale.t(key, options.merge(from: caller[0]))
12
12
  end
13
13
 
14
14
  # @see http://stackoverflow.com/a/8710663/1326499
data/lib/wallaby/core.rb CHANGED
@@ -27,6 +27,8 @@ require 'parsers/wallaby/parser'
27
27
 
28
28
  require 'utils/wallaby/field_utils'
29
29
  require 'utils/wallaby/filter_utils'
30
+ require 'utils/wallaby/locale'
31
+ require 'utils/wallaby/logger'
30
32
  require 'utils/wallaby/model_utils'
31
33
  require 'utils/wallaby/module_utils'
32
34
  require 'utils/wallaby/params_utils'
@@ -34,6 +36,10 @@ require 'utils/wallaby/preload_utils'
34
36
  require 'utils/wallaby/test_utils'
35
37
  require 'utils/wallaby/utils'
36
38
 
39
+ require 'concerns/wallaby/application_concern'
40
+ require 'concerns/wallaby/authentication_concern'
41
+ require 'concerns/wallaby/resources_concern'
42
+
37
43
  require 'concerns/wallaby/authorizable'
38
44
  require 'concerns/wallaby/baseable'
39
45
  require 'concerns/wallaby/decoratable'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wallaby
4
4
  module Core
5
- VERSION = '0.2.0' # :nodoc:
5
+ VERSION = '0.2.1' # :nodoc:
6
6
  end
7
7
  end
@@ -19,7 +19,7 @@ module Wallaby
19
19
  # @see http://rmosolgo.github.io/blog/2017/04/12/watching-files-during-rails-development/
20
20
  config.to_prepare do
21
21
  if Rails.env.development? || Rails.configuration.eager_load
22
- Rails.logger.debug ' [WALLABY] Reloading...'
22
+ Logger.debug ' [WALLABY] Reloading...'
23
23
  ::Wallaby::Map.clear
24
24
  ::Wallaby::PreloadUtils.require_all
25
25
  end
@@ -28,14 +28,14 @@ module Wallaby
28
28
 
29
29
  config.before_eager_load do
30
30
  # NOTE: We need to ensure that the core models are loaded before anything else
31
- Rails.logger.debug ' [WALLABY] Preload all model files.'
31
+ Logger.debug ' [WALLABY] Preload all model files.'
32
32
  ::Wallaby::PreloadUtils.require_one 'app/models'
33
33
  end
34
34
 
35
35
  # Preload the rest files
36
36
  config.after_initialize do
37
37
  unless Rails.env.development? || Rails.configuration.eager_load
38
- Rails.logger.debug ' [WALLABY] Preload files after initialize.'
38
+ Logger.debug ' [WALLABY] Preload files after initialize.'
39
39
  ::Wallaby::PreloadUtils.require_all
40
40
  end
41
41
  end
data/lib/wallaby/map.rb CHANGED
@@ -157,7 +157,7 @@ module Wallaby
157
157
  return unless model_class
158
158
 
159
159
  unless mode_map[model_class]
160
- Rails.logger.warn I18n.t('wallaby.map.missing_mode_for_model_class', model: model_class.name)
160
+ Logger.warn Locale.t('map.missing_mode_for_model_class', model: model_class.name), sourcing: 2
161
161
  return
162
162
  end
163
163
  instance_variable_set(variable_name, instance_variable_get(variable_name) || {})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallaby-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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-02-15 00:00:00.000000000 Z
11
+ date: 2020-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -117,9 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - LICENSE
119
119
  - README.md
120
- - app/controllers/wallaby/application_controller.rb
121
120
  - app/controllers/wallaby/resources_controller.rb
122
- - app/controllers/wallaby/secure_controller.rb
123
121
  - app/security/ability.rb
124
122
  - config/locales/wallaby.en.yml
125
123
  - config/locales/wallaby_class.en.yml
@@ -134,6 +132,8 @@ files:
134
132
  - lib/authorizers/wallaby/default_authorization_provider.rb
135
133
  - lib/authorizers/wallaby/model_authorizer.rb
136
134
  - lib/authorizers/wallaby/pundit_authorization_provider.rb
135
+ - lib/concerns/wallaby/application_concern.rb
136
+ - lib/concerns/wallaby/authentication_concern.rb
137
137
  - lib/concerns/wallaby/authorizable.rb
138
138
  - lib/concerns/wallaby/baseable.rb
139
139
  - lib/concerns/wallaby/decoratable.rb
@@ -143,6 +143,7 @@ files:
143
143
  - lib/concerns/wallaby/paginatable.rb
144
144
  - lib/concerns/wallaby/prefixable.rb
145
145
  - lib/concerns/wallaby/resourcable.rb
146
+ - lib/concerns/wallaby/resources_concern.rb
146
147
  - lib/concerns/wallaby/servicable.rb
147
148
  - lib/concerns/wallaby/shared_helpers.rb
148
149
  - lib/decorators/wallaby/resource_decorator.rb
@@ -193,6 +194,8 @@ files:
193
194
  - lib/tree/wallaby/node.rb
194
195
  - lib/utils/wallaby/field_utils.rb
195
196
  - lib/utils/wallaby/filter_utils.rb
197
+ - lib/utils/wallaby/locale.rb
198
+ - lib/utils/wallaby/logger.rb
196
199
  - lib/utils/wallaby/model_utils.rb
197
200
  - lib/utils/wallaby/module_utils.rb
198
201
  - lib/utils/wallaby/params_utils.rb