Module: Wallaby::Utils
- Defined in:
- lib/utils/wallaby/utils.rb
Overview
Utils
Class Method Summary collapse
-
.anonymous_class?(klass) ⇒ Boolean
Whether a class is anonymous.
-
.clone(object) ⇒ Object
A clone object.
-
.find_filter_name(filter_name, filters) ⇒ String, Symbol
Find filter name in the following precedences: - filter name argument - filters that has been marked as default - ‘:all`.
-
.preload(file_pattern) ⇒ Object
Preload files.
-
.preload_all ⇒ Object
Preload files.
-
.to_field_label(field_name, metadata) ⇒ String
Field label.
-
.to_model_class(resources_name) ⇒ Class
Convert resources name (e.g. ‘namespace::products`) into model class (e.g. `Namespace::Product`).
-
.to_model_label(model_class) ⇒ String
Convert model class (e.g. ‘Namespace::Product`) into model label (e.g. `Namespace / Product`).
-
.to_model_name(resources_name) ⇒ String
Convert resources name (e.g. ‘namespace::products`) into model name (e.g. `Namespace::Product`).
-
.to_partial_name(action_name) ⇒ String
Return ‘form` for `new/create/edit/update`.
-
.to_resources_name(model_class) ⇒ String
Convert model class (e.g. ‘Namespace::Product`) into resources name (e.g. `namespace::products`).
-
.translate_class(object, key, options = {}) ⇒ String
Help to translate a message for a class.
Class Method Details
.anonymous_class?(klass) ⇒ Boolean
6 7 8 |
# File 'lib/utils/wallaby/utils.rb', line 6 def self.anonymous_class?(klass) klass.name.blank? || klass.to_s.start_with?('#<Class') end |
.clone(object) ⇒ Object
Returns a clone object.
94 95 96 |
# File 'lib/utils/wallaby/utils.rb', line 94 def self.clone(object) ::Marshal.load(::Marshal.dump(object)) end |
.find_filter_name(filter_name, filters) ⇒ String, Symbol
Find filter name in the following precedences:
-
filter name argument
-
filters that has been marked as default
-
‘:all`
70 71 72 73 74 |
# File 'lib/utils/wallaby/utils.rb', line 70 def self.find_filter_name(filter_name, filters) filter_name || # from params filters.find { |_k, v| v[:default] }.try(:first) || # from default value :all # last resort end |
.preload(file_pattern) ⇒ Object
Preload files
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/utils/wallaby/utils.rb', line 109 def self.preload(file_pattern) Dir[file_pattern].each do |file_path| begin # rubocop:disable Style/RedundantBegin name = file_path[%r{app/[^/]+/(.+)\.rb}, 1].gsub('concerns/', '') class_name = name.classify class_name.constantize unless Module.const_defined? class_name rescue NameError, LoadError => e Rails.logger.debug " [WALLABY] Preload warning: #{e.}" Rails.logger.debug e.backtrace.slice(0, 5) end end end |
.preload_all ⇒ Object
Preload files
99 100 101 102 103 104 105 106 |
# File 'lib/utils/wallaby/utils.rb', line 99 def self.preload_all ::Wallaby::ApplicationController.to_s preload 'app/models/**/*.rb' preload 'app/decorators/**/*.rb' preload 'app/controllers/**/*.rb' preload 'app/servicers/**/*.rb' preload 'app/**/*.rb' end |
.to_field_label(field_name, metadata) ⇒ String
79 80 81 82 |
# File 'lib/utils/wallaby/utils.rb', line 79 def self.to_field_label(field_name, ) field_name = field_name.to_s if field_name.is_a? Symbol [:label] || field_name.humanize end |
.to_model_class(resources_name) ⇒ Class
Convert resources name (e.g. ‘namespace::products`) into model class (e.g. `Namespace::Product`)
53 54 55 56 57 58 59 60 61 |
# File 'lib/utils/wallaby/utils.rb', line 53 def self.to_model_class(resources_name) return if resources_name.blank? class_name = to_model_name resources_name # NOTE: do not use if statement instead of rescue here # we want the class_name to be eagerly loaded class_name.constantize rescue NameError => _error raise ModelNotFound, class_name end |
.to_model_label(model_class) ⇒ String
Convert model class (e.g. ‘Namespace::Product`) into model label (e.g. `Namespace / Product`)
34 35 36 37 38 |
# File 'lib/utils/wallaby/utils.rb', line 34 def self.to_model_label(model_class) return EMPTY_STRING if model_class.blank? model_class_name = to_model_name model_class model_class_name.titleize.gsub(SLASH, SPACE + SLASH + SPACE) end |
.to_model_name(resources_name) ⇒ String
Convert resources name (e.g. ‘namespace::products`) into model name (e.g. `Namespace::Product`)
44 45 46 47 |
# File 'lib/utils/wallaby/utils.rb', line 44 def self.to_model_name(resources_name) return EMPTY_STRING if resources_name.blank? resources_name.to_s.singularize.gsub(COLONS, SLASH).camelize end |
.to_partial_name(action_name) ⇒ String
Return ‘form` for `new/create/edit/update`
87 88 89 |
# File 'lib/utils/wallaby/utils.rb', line 87 def self.to_partial_name(action_name) FORM_ACTIONS.include?(action_name) ? 'form' : action_name end |
.to_resources_name(model_class) ⇒ String
Convert model class (e.g. ‘Namespace::Product`) into resources name (e.g. `namespace::products`)
25 26 27 28 |
# File 'lib/utils/wallaby/utils.rb', line 25 def self.to_resources_name(model_class) return EMPTY_STRING if model_class.blank? model_class.to_s.underscore.gsub(SLASH, COLONS).pluralize end |
.translate_class(object, key, options = {}) ⇒ String
Help to translate a message for a class
15 16 17 18 19 |
# File 'lib/utils/wallaby/utils.rb', line 15 def self.translate_class(object, key, = {}) klass = object.is_a?(Class) ? object : object.class key = [klass.name, key].join(SLASH).underscore.gsub(SLASH, DOT) I18n.t key, end |