Module: Wallaby::Utils
- Defined in:
- lib/utils/wallaby/utils.rb
Overview
Utils
Class Method Summary collapse
- .clone(object) ⇒ Object
- .find_filter_name(filter_name, filters) ⇒ Object
- .preload(file_pattern) ⇒ Object
- .preload_all ⇒ Object
- .to_field_label(field_name, metadata) ⇒ Object
- .to_hash(array) ⇒ Object
- .to_model_class(resources_name) ⇒ Object
- .to_model_label(model_class) ⇒ Object
- .to_model_name(resources_name) ⇒ Object
- .to_partial_name(action_name) ⇒ Object
- .to_resources_name(model_class) ⇒ Object
Class Method Details
.clone(object) ⇒ Object
50 51 52 |
# File 'lib/utils/wallaby/utils.rb', line 50 def self.clone(object) ::Marshal.load(::Marshal.dump(object)) end |
.find_filter_name(filter_name, filters) ⇒ Object
30 31 32 33 34 |
# File 'lib/utils/wallaby/utils.rb', line 30 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
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/utils/wallaby/utils.rb', line 63 def self.preload(file_pattern) Dir[file_pattern].each do |file_path| begin 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 ">>>>>>>>> PRELOAD ERROR: #{e.}" Rails.logger.debug e.backtrace.slice(0, 5) end end end |
.preload_all ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/utils/wallaby/utils.rb', line 54 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) ⇒ Object
36 37 38 39 |
# File 'lib/utils/wallaby/utils.rb', line 36 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_hash(array) ⇒ Object
45 46 47 |
# File 'lib/utils/wallaby/utils.rb', line 45 def self.to_hash(array) Hash[*array.flatten(1)] end |
.to_model_class(resources_name) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/utils/wallaby/utils.rb', line 20 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) ⇒ Object
9 10 11 12 13 |
# File 'lib/utils/wallaby/utils.rb', line 9 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) ⇒ Object
15 16 17 18 |
# File 'lib/utils/wallaby/utils.rb', line 15 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) ⇒ Object
41 42 43 |
# File 'lib/utils/wallaby/utils.rb', line 41 def self.to_partial_name(action_name) FORM_ACTIONS.include?(action_name) ? 'form' : action_name end |
.to_resources_name(model_class) ⇒ Object
4 5 6 7 |
# File 'lib/utils/wallaby/utils.rb', line 4 def self.to_resources_name(model_class) return EMPTY_STRING if model_class.blank? model_class.to_s.underscore.gsub(SLASH, COLONS).pluralize end |