xptavares/activerequest

View on GitHub
lib/active_request/find_by.rb

Summary

Maintainability
A
0 mins
Test Coverage
module ActiveRequest
  module FindBy
    def self.included(base)
      base.extend(ClassMethods)
    end
    module ClassMethods

      def find_by(query)
        where(query).first
      end

      def build_find_by
        attributes.each do |attribute|
          define_singleton_method("find_by_#{attribute}") do |param|
            find_by(attribute => param)
          end
        end
      end
    end
  end
end