Sha256: b7923fba98ec00dac6d3f6480842aba578451dced072d14fff216c269814d80f

Contents?: true

Size: 1022 Bytes

Versions: 26

Compression:

Stored size: 1022 Bytes

Contents

require 'active_support'

class LHS::Record

  module FindBy
    extend ActiveSupport::Concern

    module ClassMethods
      # Fetch some record by parameters
      def find_by(params = {}, options = nil)
        _find_by(params, options)
      rescue LHC::NotFound
        nil
      end

      # Raise if no record was found
      def find_by!(params = {}, options = nil)
        _find_by(params, options)
      end

      private

      def _find_by(params, options = {})
        raise(LHS::Unprocessable.new, 'Cannot find Record without an ID') if params.any? && params.all? { |_, value| value.blank? }
        options ||= {}
        params = params.dup.merge(limit: 1).merge(options.fetch(:params, {}))
        data = request(options.merge(params: params))
        if data && data._proxy.is_a?(LHS::Collection)
          data.first || raise(LHC::NotFound.new('No item was found.', data._request.response))
        elsif data
          data._record.new(data.unwrap_nested_item)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
lhs-15.2.3 lib/lhs/concerns/record/find_by.rb
lhs-15.2.3.pre.favorites.1 lib/lhs/concerns/record/find_by.rb
lhs-15.2.2.pre.favorites.1 lib/lhs/concerns/record/find_by.rb
lhs-15.2.2 lib/lhs/concerns/record/find_by.rb
lhs-15.2.1 lib/lhs/concerns/record/find_by.rb
lhs-15.2.0 lib/lhs/concerns/record/find_by.rb
lhs-15.1.1 lib/lhs/concerns/record/find_by.rb
lhs-15.1.0 lib/lhs/concerns/record/find_by.rb
lhs-15.0.2 lib/lhs/concerns/record/find_by.rb
lhs-15.0.1 lib/lhs/concerns/record/find_by.rb
lhs-15.0.0 lib/lhs/concerns/record/find_by.rb
lhs-14.6.5 lib/lhs/concerns/record/find_by.rb
lhs-14.6.4 lib/lhs/concerns/record/find_by.rb
lhs-14.6.3 lib/lhs/concerns/record/find_by.rb
lhs-14.6.2 lib/lhs/concerns/record/find_by.rb
lhs-14.6.1 lib/lhs/concerns/record/find_by.rb
lhs-14.6.0 lib/lhs/concerns/record/find_by.rb
lhs-14.5.0 lib/lhs/concerns/record/find_by.rb
lhs-14.4.0 lib/lhs/concerns/record/find_by.rb
lhs-14.3.4 lib/lhs/concerns/record/find_by.rb