Created
April 25, 2012 14:38
-
-
Save electronicbites/2490230 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ORIG_ARTIST = "Jichael Mackson" | |
Media.where(:artist => ORIG_ARTIST).each(&:destroy) | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
require 'factory_girl' | |
begin | |
FactoryGirl.define do | |
factory :media do | |
comment "dummy_foo" | |
artist ORIG_ARTIST | |
title "Yet another title" | |
album_string "Never good enough" | |
year "1987" | |
length_in_seconds "324.4" | |
end | |
end | |
rescue FactoryGirl::DuplicateDefinitionError | |
end | |
#create the media | |
media = FactoryGirl.create(:media) | |
#now searching | |
artist = "mackson" | |
title = "title" | |
## taken from rake task code: | |
parser = OptimisticMediaParser.new(artist, title, :include_raw => true) | |
artists, titles = parser.parse | |
artists_condition = ""; | |
titles_conditions = "" | |
match = Array.new | |
select_stmt = ["artist, title"] | |
select_stmt_lev_titles = Array.new(titles.count) {|i| | |
"levenshtein(substr(title,0,255), :title_#{i})" | |
} | |
select_stmt_lev_artists = Array.new(artists.count) {|i| | |
"levenshtein(substr(artist,0,255), :artist_#{i})" | |
} | |
condition_stmt_soundex_titles = Array.new(titles.count) {|i| | |
"dmetaphone_alt(substr(title,0,255)) = dmetaphone_alt(:title_#{i})" | |
} | |
condition_stmt_soundex_artists = Array.new(artists.count) {|i| | |
"dmetaphone_alt(substr(artist,0,255)) = dmetaphone_alt(:artist_#{i})" | |
} | |
values = begin | |
title_values = Hash.new | |
titles.each_with_index {|t, i| | |
title_values[:"title_#{i}"] = t[0..255] | |
} | |
artist_values = Hash.new | |
artists.each_with_index {|a, i| | |
title_values[:"artist_#{i}"] = a[0..255] | |
} | |
title_values.merge(artist_values) | |
end | |
select = ActiveRecord::Base.send(:sanitize_sql_array, | |
[(select_stmt + select_stmt_lev_titles + select_stmt_lev_artists).reject{|s| s.blank?}.join(','), values] | |
) | |
filter_bad_match = (select_stmt_lev_titles + select_stmt_lev_artists).map {|s| | |
"#{s} < 1" | |
}.join(' OR ') | |
condition = ActiveRecord::Base.send(:sanitize_sql_array, | |
[ | |
'(' + | |
([condition_stmt_soundex_titles.join(' OR '), condition_stmt_soundex_artists.join(' OR '), filter_bad_match].reject{|s| s.blank?}.join(') AND (')) + | |
')', | |
values | |
] | |
) | |
order_by = ActiveRecord::Base.send(:sanitize_sql_array, | |
[(select_stmt_lev_titles + select_stmt_lev_artists).join(' + '), values] | |
) | |
Media.select(select).where(condition).order(order_by).first ## ==> nil BUT WHY? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment