module Pathway::Plugins::SequelModels::InstanceMethods

Public Instance Methods

fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false, error_message: nil) click to toggle source
# File lib/pathway/plugins/sequel_models.rb, line 65
def fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false, error_message: nil)
  error_message ||= if (from == model_class)
                      model_not_found
                    elsif from.respond_to?(:name) || from.respond_to?(:model)
                      from_name = (from.respond_to?(:name) ? from : from.model).name
                      Inflector.humanize(Inflector.underscore(Inflector.demodulize(from_name))) + ' not found'
                    end

  if state[to].nil? || overwrite
    wrap_if_present(state[:input][using], message: error_message)
      .then { |key| find_model_with(key, from, search_by, error_message) }
      .then { |model| state.update(to => model) }
  else
    state
  end
end
find_model_with(key, dataset = model_class, column = search_field, error_message = nil) click to toggle source
# File lib/pathway/plugins/sequel_models.rb, line 82
def find_model_with(key, dataset = model_class, column = search_field, error_message = nil)
  wrap_if_present(dataset.first(column => key), message: error_message)
end