Module: ForestAdminDatasourceMambuPayments::Plugins::Helpers
- Defined in:
- lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb
Overview
Shared helpers for Mambu Payments plugins: input normalization, host record id resolution, and per-id rescue logic for bulk transitions.
Constant Summary collapse
- ActionScope =
ForestAdminDatasourceCustomizer::Decorators::Action::Types::ActionScope
- SCOPE_KEYS =
%i[single bulk].freeze
- SCOPES =
{ single: ActionScope::SINGLE, bulk: ActionScope::BULK }.freeze
Class Method Summary collapse
-
.each_with_rescue(ids, label) ⇒ Object
Per-id rescue so a single API failure doesn’t abort the remaining ids.
- .normalize_scopes(value) ⇒ Object
- .present?(value) ⇒ Boolean
-
.require_datasource!(datasource_customizer, plugin_class) ⇒ Object
Relation plugins must be installed at the datasource level (‘@agent.use(plugin, {})`) so they can customize several collections at once.
- .resolve_ids(context, field) ⇒ Object
- .to_int(value) ⇒ Object
- .write_back(context, field, value) ⇒ Object
- .write_back_warning(writeback) ⇒ Object
Class Method Details
.each_with_rescue(ids, label) ⇒ Object
Per-id rescue so a single API failure doesn’t abort the remaining ids.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 48 def each_with_rescue(ids, label) succeeded = [] failed = [] ids.each do |id| yield id succeeded << id rescue StandardError => e ForestAdminDatasourceMambuPayments.logger.warn( "[forest_admin_datasource_mambu_payments] #{label} failed for ##{id}: #{e.class}: #{e.}" ) failed << [id, "#{e.class}: #{e.}"] end [succeeded, failed] end |
.normalize_scopes(value) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 25 def normalize_scopes(value) list = Array(value).map(&:to_sym).uniq list = SCOPE_KEYS if list.empty? unknown = list - SCOPE_KEYS return list if unknown.empty? raise ForestAdminDatasourceToolkit::Exceptions::ForestException, "Unknown scopes: #{unknown.join(", ")}. Allowed: #{SCOPE_KEYS.join(", ")}." end |
.present?(value) ⇒ Boolean
63 64 65 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 63 def present?(value) !value.nil? && value.to_s != '' end |
.require_datasource!(datasource_customizer, plugin_class) ⇒ Object
Relation plugins must be installed at the datasource level (‘@agent.use(plugin, {})`) so they can customize several collections at once. Raises a clear error when a caller installs one on a single collection instead.
17 18 19 20 21 22 23 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 17 def require_datasource!(datasource_customizer, plugin_class) return if datasource_customizer name = plugin_class.is_a?(Class) ? plugin_class.name.split('::').last : plugin_class raise ArgumentError, "#{name} must be installed at the datasource level via @agent.use(plugin, {})" end |
.resolve_ids(context, field) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 35 def resolve_ids(context, field) records = context.get_records([field]) records = [records].compact unless records.is_a?(Array) records.filter_map { |r| r[field] || r[field.to_sym] } rescue StandardError => e ForestAdminDatasourceMambuPayments.logger.warn( "[forest_admin_datasource_mambu_payments] failed to resolve ids from '#{field}': " \ "#{e.class}: #{e.}" ) [] end |
.to_int(value) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 67 def to_int(value) return nil unless present?(value) Integer(value.to_s) rescue ArgumentError, TypeError nil end |
.write_back(context, field, value) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 75 def write_back(context, field, value) return :skipped if field.nil? || value.nil? context.collection.update(context.filter, { field => value }) :ok rescue StandardError => e ForestAdminDatasourceMambuPayments.logger.warn( "[forest_admin_datasource_mambu_payments] write-back to '#{field}' failed: #{e.class}: #{e.}" ) [:failed, "#{e.class}: #{e.}"] end |
.write_back_warning(writeback) ⇒ Object
87 88 89 90 91 |
# File 'lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb', line 87 def write_back_warning(writeback) return nil unless writeback.is_a?(Array) && writeback.first == :failed " (warning: could not write the id back to the host record: #{writeback.last})" end |