Sha256: 1c435628e0222456015fa2510fd2d45aad288f5c1c45cdfe6dc6f82c1fa81853

Contents?: true

Size: 686 Bytes

Versions: 49

Compression:

Stored size: 686 Bytes

Contents

ActiveRecord::Base.class_eval do

  def self.find_by_anything(identifier)
    matchable_columns = columns.reject { |column| [:binary, :boolean].include?(column.type) }
    query_clauses = matchable_columns.collect do |column|
      qualified_column_name = "#{table_name}.#{column.name}"
      column_as_string = "CAST(#{qualified_column_name} AS CHAR)"
      "#{column_as_string} = ?"
    end
    bindings = [identifier] * query_clauses.size
    find(:first, :conditions => [query_clauses.join(' OR '), *bindings])
  end
  
  def self.find_by_anything!(identifier)
    find_by_anything(identifier) or raise ActiveRecord::RecordNotFound, "No column equals \"#{identifier}\""
  end

end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
upjs-rails-0.14.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.13.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.5 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.4 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.3 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.2 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.1 spec_app/features/support/find_by_anything.rb
upjs-rails-0.12.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.11.1 spec_app/features/support/find_by_anything.rb
upjs-rails-0.11.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.5 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.4 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.3 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.2 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.1 spec_app/features/support/find_by_anything.rb
upjs-rails-0.10.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.9.1 spec_app/features/support/find_by_anything.rb
upjs-rails-0.9.0 spec_app/features/support/find_by_anything.rb
upjs-rails-0.8.2 spec_app/features/support/find_by_anything.rb
upjs-rails-0.8.1 spec_app/features/support/find_by_anything.rb