Module: Pathway::Plugins::SequelModels::InstanceMethods

Extended by:
Forwardable
Defined in:
lib/pathway/plugins/sequel_models.rb

Instance Method Summary collapse

Instance Method Details

#fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false, error_message: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



82
83
84
# 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