diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index f297491fd..5a465dbee 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -13,10 +13,15 @@ class ArtistsController < ApplicationController end def index - @artists = Artist.build_relation(params).paginate(:per_page => 25, :page => params[:page]) + @search = Artist.search(params[:search]) + @artists = @search.paginate(:page => params[:page]) respond_with(@artists) end + def search + @search = Artist.search(params[:search]) + end + def show @artist = Artist.find(params[:id]) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 7a64dd3a2..2a14c5079 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -13,6 +13,10 @@ class TagsController < ApplicationController respond_with(@tags) end + def search + @search = Tag.search(params[:search]) + end + def show @tag = Tag.find(params[:id]) respond_with(@tag) diff --git a/app/models/artist.rb b/app/models/artist.rb index 83ec6553a..6560b1510 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -12,6 +12,9 @@ class Artist < ActiveRecord::Base has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name" accepts_nested_attributes_for :wiki_page attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes + scope :url_match, lambda {|string| where(["id in (?)", Artist.find_all_by_url(string).map(&:id)])} + scope :other_names_match, lambda {|string| where(["other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string)])} + search_method :url_match, :other_names_match module UrlMethods module ClassMethods @@ -78,58 +81,6 @@ class Artist < ActiveRecord::Base end end - module SearchMethods - def find_by_name_or_id(params) - if params[:name] - find_by_name(params[:name]) - else - find(params[:id]) - end - end - - def find_by_any_name(name) - build_relation(:name => name).first - end - - def build_relation(params) - relation = Artist.where("is_active = TRUE") - - case params[:name] - when /^http/ - relation = relation.where("id IN (?)", find_all_by_url(params[:name]).map(&:id)) - - when /name:(.+)/ - escaped_name = Artist.normalize_name($1).to_escaped_for_sql_like - relation = relation.where(["name LIKE ? ESCAPE E'\\\\'", escaped_name]) - - when /other:(.+)/ - escaped_name = Artist.normalize_name($1) - relation = relation.where(["other_names_index @@ to_tsquery('danbooru', ?)", escaped_name]) - - when /group:(.+)/ - escaped_name = Artist.normalize_name($1).to_escaped_for_sql_like - relation = relation.where(["group_name LIKE ? ESCAPE E'\\\\'", escaped_name]) - - when /./ - normalized_name = Artist.normalize_name($1) - escaped_name = normalized_name.to_escaped_for_sql_like - relation = relation.where(["name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', ?) OR group_name LIKE ? ESCAPE E'\\\\'", escaped_name, normalized_name, escaped_name]) - end - - if params[:id] - relation = relation.where(["id = ?", params[:id]]) - end - - if params[:order] == "date" - relation = relation.order("updated_at DESC") - else - relation = relation.order("name") - end - - relation - end - end - module VersionMethods def create_version ArtistVersion.create( @@ -209,7 +160,6 @@ class Artist < ActiveRecord::Base include UrlMethods include NameMethods include GroupMethods - extend SearchMethods include VersionMethods extend FactoryMethods include NoteMethods diff --git a/app/views/artists/_search.html.erb b/app/views/artists/_search.html.erb deleted file mode 100644 index 072712489..000000000 --- a/app/views/artists/_search.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -