module Formalism::ModelForms::Find

Base form for model finding

Public Class Methods

new(params_or_instance = {}) click to toggle source
Calls superclass method Formalism::ModelForms::Base::new
# File lib/formalism/model_forms/find.rb, line 12
def initialize(params_or_instance = {})
        if params_or_instance.is_a?(Hash)
                super(params_or_instance)
        else
                @initialized_with_instance = true
                self.instance = params_or_instance
                super()
        end
end

Public Instance Methods

before_retry() click to toggle source
# File lib/formalism/model_forms/find.rb, line 22
def before_retry
        return if @initialized_with_instance

        super

        return unless instance_variable_defined? :@instance

        remove_instance_variable :@instance
end

Private Instance Methods

dataset() click to toggle source
# File lib/formalism/model_forms/find.rb, line 62
        def dataset
        ## `Dataset#all` for `EAGER` with `.first` in Find forms
        (@cached ? model : filtered_non_cached_dataset).all
end
execute() click to toggle source
# File lib/formalism/model_forms/find.rb, line 34
def execute
        self.instance = @cached ? find_cached : find_non_cached
end
filtered_non_cached_dataset() click to toggle source
# File lib/formalism/model_forms/find.rb, line 67
        def filtered_non_cached_dataset
        unfiltered_non_cached_dataset.where(conditions_for_dataset)
end
find_cached() click to toggle source
# File lib/formalism/model_forms/find.rb, line 45
def find_cached
        dataset.find do |instance|
                fields_and_nested_forms.all? do |key, value|
                        compare_value instance[key], value
                end
        end
end
find_instance() click to toggle source
# File lib/formalism/model_forms/find.rb, line 38
def find_instance
        return unless valid?

        ## `run` can return `nil` if form is not `runnable`
        run&.result
end
find_non_cached() click to toggle source
# File lib/formalism/model_forms/find.rb, line 53
def find_non_cached
        dataset.first
rescue Sequel::DatabaseError => e
        ## I'm not sure that there is safe to use
        ## `Sequel::DatabaseError`
        raise e unless e.cause.is_a? PG::DataException
        raise e if e.cause.is_a? PG::InvalidTextRepresentation
end
list_form_class() click to toggle source
# File lib/formalism/model_forms/find.rb, line 75
def list_form_class
        return unless namespace.const_defined?(:List) && namespace::List.is_a?(Class)

        namespace::List
end
unfiltered_non_cached_dataset() click to toggle source
# File lib/formalism/model_forms/find.rb, line 71
        def unfiltered_non_cached_dataset
        (list_form_class&.new(params) || model).dataset
end