diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 73253d5..a182441 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -6,6 +6,7 @@ class SearchController < ApplicationController before_filter :prepare_filter before_filter :check_search_whole_site before_filter :load_search_assets + before_filter :check_valid_assets, :only => [ :assets, :directory ] no_design_blocks @@ -33,6 +34,14 @@ class SearchController < ApplicationController end end + def check_valid_assets + @asset = params[:asset].to_sym + if !SEARCH_IN.map(&:first).include?(@asset) + render :text => 'go away', :status => 403 + return + end + end + public include SearchHelper @@ -83,17 +92,20 @@ class SearchController < ApplicationController attr_reader :category def assets - asset = params[:asset].to_sym - if !SEARCH_IN.map(&:first).include?(asset) - render :text => 'go away', :status => 403 - return - end + @results = { @asset => @finder.recent(@asset, LIST_LIMIT) } + + @asset_name = gettext(SEARCH_IN.find { |entry| entry.first == @asset }[1]) + @names = { @asset => @asset_name } + end + def directory + @results = { @asset => @finder.find_by_initial(@asset, params[:initial]) } - @results = { asset => @finder.recent(asset, LIST_LIMIT) } + # FIXME remove this duplication with assets action + @asset_name = gettext(SEARCH_IN.find { |entry| entry.first == @asset }[1]) + @names = { @asset => @asset_name } - @asset_name = gettext(SEARCH_IN.find { |entry| entry.first == asset }[1]) - @names = { asset => @asset_name } + render :action => 'assets' end def tags diff --git a/app/models/article.rb b/app/models/article.rb index c91440c..6656860 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -112,6 +112,10 @@ class Article < ActiveRecord::Base true end + def self.find_by_initial(initial) + self.find(:all, :order => 'articles.name', :conditions => [ 'articles.name like (?)', initial + '%']) + end + private def sanitize_tag_list diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index cabc833..ec7da7d 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -35,6 +35,10 @@ class CategoryFinder asset_class(asset).find(:all, options_for_find(asset_class(asset), {:limit => limit, :order => "created_at desc, #{asset_table(asset)}.id desc"})) end + def find_by_initial(asset, initial) + asset_class(asset).find(:all, options_for_find_by_initial(asset_class(asset), initial)) + end + def count(asset) asset_class(asset).count(:all, options_for_find(asset_class(asset))) end @@ -62,6 +66,20 @@ class CategoryFinder end end + def options_for_find_by_initial(klass, initial) + # FIXME copy/pasted from options_for_find above !!! + case klass.name + when 'Comment' + {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id in (?) and comments.title like (?)', category_ids, initial + '%']} + when 'Product' + {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.name like (?)', category_ids, initial + '%']} + when 'Article', 'Person', 'Community', 'Enterprise' + {:include => 'categories', :conditions => ['categories.id IN (?) and %s.name like (?)' % klass.table_name, category_ids, initial + '%']} + else + raise "unreconized class #{klass.name}" + end + end + def asset_class(asset) asset.to_s.singularize.camelize.constantize end diff --git a/app/models/comment.rb b/app/models/comment.rb index a5d0f1f..2824808 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -41,4 +41,8 @@ class Comment < ActiveRecord::Base self.find(:all, :order => 'created_at desc, id desc', :limit => limit) end + def self.find_by_initial(initial) + self.find(:all, :order => 'comments.title', :conditions => ['comments.title like (?)', initial + '%']) + end + end diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index 3bafd49..3540dd4 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -34,6 +34,10 @@ class EnvironmentFinder end end + def find_by_initial(asset, initial) + @environment.send(asset).find_by_initial(initial) + end + def count(asset) @environment.send(asset).count end diff --git a/app/models/product.rb b/app/models/product.rb index cbf8b8d..c8eb26c 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -38,4 +38,8 @@ class Product < ActiveRecord::Base self.find(:all, :order => 'id desc', :limit => limit) end + def self.find_by_initial(initial) + self.find(:all, :order => 'products.name', :conditions => [ 'products.name like (?)', initial + '%']) + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index 66b9c1b..95a0de0 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -289,4 +289,8 @@ class Profile < ActiveRecord::Base self.find(:all, :order => 'id desc', :limit => limit) end + def self.find_by_initial(initial) + self.find(:all, :order => 'profiles.name', :conditions => [ 'profiles.name like (?)', (initial + '%') ]) + end + end diff --git a/app/views/search/assets.rhtml b/app/views/search/assets.rhtml index dfa7cad..a086577 100644 --- a/app/views/search/assets.rhtml +++ b/app/views/search/assets.rhtml @@ -1,3 +1,10 @@

<%= @category ? (_('%{asset_name} in %{category}') % { :asset_name => @asset_name, :category => @category.name}) : @asset_name %>

+
+ <%= link_to_unless_current(_('Recent'), :action => 'assets') %> +   + <%= (?a..?z).map { |initial| link_to_unless_current(('' << initial).upcase, :action => 'directory', :initial => ('' << initial)) }.join('   ') %> +
+
+ <%= render :partial => 'display_results' %> diff --git a/config/routes.rb b/config/routes.rb index a10d0e9..c8e77ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,6 +32,7 @@ ActionController::Routing::Routes.draw do |map| # categories index map.category 'cat/*category_path', :controller => 'search', :action => 'category_index' map.assets 'assets/:asset/*category_path', :controller => 'search', :action => 'assets' + map.directory 'directory/:asset/:initial/*category_path', :controller => 'search', :action => 'directory' # search map.connect 'search/:action/*category_path', :controller => 'search' diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 9c7535a..ae5967f 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -16,7 +16,7 @@ class SearchControllerTest < Test::Unit::TestCase def test_local_files_reference assert_local_files_reference end - + def test_valid_xhtml assert_valid_xhtml end @@ -77,7 +77,7 @@ class SearchControllerTest < Test::Unit::TestCase assert_includes assigns(:results)[:articles], art1 assert_not_includes assigns(:results)[:articles], art2 end - + # 'assets' outside any category should 'list articles in general' do person = create_user('testuser').person @@ -92,7 +92,7 @@ class SearchControllerTest < Test::Unit::TestCase assert_includes assigns(:results)[:articles], art1 assert_includes assigns(:results)[:articles], art2 end - + # 'assets' inside a category should 'list articles in a specific category' do person = create_user('testuser').person @@ -131,7 +131,7 @@ class SearchControllerTest < Test::Unit::TestCase # not in category art2 = person.articles.build(:name => 'another article to be found') - art2.save! + art2.save! comment2 = art2.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment2.save! get :index, :category_path => ['my-category'], :query => 'found', :find_in => [ 'comments' ] @@ -176,7 +176,7 @@ class SearchControllerTest < Test::Unit::TestCase get 'index', :query => 'teste', :find_in => [ 'enterprises' ] assert_includes assigns(:results)[:enterprises], ent end - + should 'find enterprises in a specified category' do # in category @@ -235,7 +235,7 @@ class SearchControllerTest < Test::Unit::TestCase p2 = create_user('test2').person get :assets, :asset => 'people' - + assert_equal [p2,p1], assigns(:results)[:people].instance_variable_get('@results') end @@ -350,9 +350,9 @@ class SearchControllerTest < Test::Unit::TestCase article = person.articles.create!(:name => 'display article') comment = article.comments.create!(:title => 'display comment', :body => '...', :author => person) community = Community.create!(:name => 'display community', :identifier => 'an_bea_comm') - + get :index, :query => 'display' - + names = { :articles => 'Articles', :comments => 'Comments', @@ -539,7 +539,7 @@ class SearchControllerTest < Test::Unit::TestCase should 'search in categoty hierachy' do parent = Category.create!(:name => 'Parent Category', :environment => Environment.default) child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent) - + p = create_user('test_profile').person p.categories << child p.save! @@ -558,5 +558,194 @@ class SearchControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'input', :attributes => { :name => 'find_in[]', :value => 'enterprises', :checked => 'checked' } assert_no_tag :tag => 'input', :attributes => { :name => 'find_in[]', :value => 'products', :checked => 'checked' } end - + + ############## directory #################### + should 'link to people directory in index' do + get :assets, :asset => 'people' + assert_tag :tag => 'a', :attributes => { :href => '/directory/people/a'}, :content => 'A' + assert_tag :tag => 'a', :attributes => { :href => '/directory/people/b'}, :content => 'B' + end + + should 'display link in people directory to other initials but not to the same' do + get :directory, :asset => 'people', :initial => 'r' + assert_tag :tag => 'a', :attributes => { :href => '/directory/people/a' } + assert_no_tag :tag => 'a', :attributes => { :href => '/directory/people/r' } + end + + should 'display link to recent people while in directory' do + get :directory, :asset => 'people', :initial => 'a' + assert_tag :tag => 'a', :attributes => { :href => '/assets/people' }, :content => 'Recent' + end + + ############### directory for every kind of asset ################# + should 'display people with a given initial' do + included = create_user('fergunson').person + not_included = create_user('yanerson').person + + get :directory, :asset => 'people', :initial => 'f' + assert_includes assigns(:results)[:people], included + assert_not_includes assigns(:results)[:people], not_included + end + + should 'display communities with a given initial' do + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) + c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default) + + get :directory, :asset => 'communities', :initial => 'a' + + assert_includes assigns(:results)[:communities], c1 + assert_not_includes assigns(:results)[:communities], c2 + end + + should 'display enterprises with a given initial' do + ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1') + ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2') + + get :directory, :asset => 'enterprises', :initial => 'a' + + assert_includes assigns(:results)[:enterprises], ent1 + assert_not_includes assigns(:results)[:enterprises], ent2 + end + + should 'display products with a given initial' do + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') + prod1 = ent.products.create!(:name => 'a beautiful product') + prod2 = ent.products.create!(:name => 'beautiful product (another)') + + get :directory, :asset => 'products', :initial => 'a' + + assert_includes assigns(:results)[:products], prod1 + assert_not_includes assigns(:results)[:products], prod2 + end + + should 'display articles with a given initial' do + person = create_user('teste').person + art1 = person.articles.build(:name => 'an article to be found'); art1.save! + art2 = person.articles.build(:name => 'better article'); art2.save! + + get :directory, :asset => 'articles', :initial => 'a' + + assert_includes assigns(:results)[:articles], art1 + assert_not_includes assigns(:results)[:articles], art2 + end + + should 'display comments with a given initial' do + person = create_user('teste').person + art = person.articles.build(:name => 'an article to be found'); art.save! + + comment1 = art.comments.build(:title => 'a comment to be found', :body => 'hfyfyh', :author => person); comment1.save! + comment2 = art.comments.build(:title => 'better comment, but not found', :body => 'hfyfyh', :author => person); comment2.save! + + get :directory, :asset => 'comments', :initial => 'a' + + assert_includes assigns(:results)[:comments], comment1 + assert_not_includes assigns(:results)[:comments], comment2 + end + + + should 'display people with a given initial, under a specific category' do + + in_category_and_with_initial = create_user('fergunson').person + in_category_and_with_initial.categories << @category + + in_category_but_without_initial = create_user('yanerson').person + in_category_but_without_initial.categories << @category + + not_in_category_but_with_initial = create_user('fergy').person + not_in_category_and_without_initial = create_user('xalanxalan').person + + get :directory, :asset => 'people', :initial => 'f', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:people], in_category_and_with_initial + assert_not_includes assigns(:results)[:people], in_category_but_without_initial + assert_not_includes assigns(:results)[:people], not_in_category_but_with_initial + assert_not_includes assigns(:results)[:people], not_in_category_and_without_initial + end + + should 'display communities with a given initial, under a specific category' do + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default); c1.categories << @category + c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default); c2.categories << @category + + c3 = Community.create!(:name => 'another beautiful community', :identifier => 'lalala', :environment => Environment.default); + c4 = Community.create!(:name => 'damn beautiful community (another)', :identifier => 'lelele', :environment => Environment.default) + + get :directory, :asset => 'communities', :initial => 'a', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:communities], c1 + assert_not_includes assigns(:results)[:communities], c2 + assert_not_includes assigns(:results)[:communities], c3 + assert_not_includes assigns(:results)[:communities], c4 + end + + should 'display enterprises with a given initial, under a specific category' do + ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1'); ent1.categories << @category + ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2'); ent1.categories << @category + ent3 = Enterprise.create!(:name => 'aaaa1111', :identifier => 'teste1111') + ent4 = Enterprise.create!(:name => 'ddddd', :identifier => 'teste2222') + + get :directory, :asset => 'enterprises', :initial => 'a', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:enterprises], ent1 + assert_not_includes assigns(:results)[:enterprises], ent2 + assert_not_includes assigns(:results)[:enterprises], ent3 + assert_not_includes assigns(:results)[:enterprises], ent4 + end + + should 'display products with a given initial, under a specific category' do + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') + ent.categories << @category + prod1 = ent.products.create!(:name => 'a beautiful product') + prod2 = ent.products.create!(:name => 'beautiful product (another)') + + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') + prod3 = ent2.products.create!(:name => 'another product') + prod4 = ent2.products.create!(:name => 'damn product (another)') + + get :directory, :asset => 'products', :initial => 'a', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:products], prod1 + assert_not_includes assigns(:results)[:products], prod2 + assert_not_includes assigns(:results)[:products], prod3 + assert_not_includes assigns(:results)[:products], prod4 + end + + should 'display articles with a given initial, under a specific category' do + person = create_user('teste').person + art1 = person.articles.build(:name => 'an article to be found'); art1.save! + art1.categories << @category + art2 = person.articles.build(:name => 'better article'); art2.save! + art2.categories << @category + + art3 = person.articles.build(:name => 'another article to be found'); art3.save! + art4 = person.articles.build(:name => 'damn article'); art4.save! + + + get :directory, :asset => 'articles', :initial => 'a', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:articles], art1 + assert_not_includes assigns(:results)[:articles], art2 + assert_not_includes assigns(:results)[:articles], art3 + assert_not_includes assigns(:results)[:articles], art4 + end + + should 'display comments with a given initial, under a specific category' do + person = create_user('teste').person + art = person.articles.build(:name => 'an article to be found'); art.save! + art.categories << @category + comment1 = art.comments.build(:title => 'a comment to be found', :body => 'hfyfyh', :author => person); comment1.save! + comment2 = art.comments.build(:title => 'better comment, but not found', :body => 'hfyfyh', :author => person); comment2.save! + + art2 = person.articles.build(:name => 'another article to be found'); art2.save! + comment3 = art2.comments.build(:title => 'a comment to be found', :body => 'hfyfyh', :author => person); comment1.save! + comment4 = art2.comments.build(:title => 'better comment, but not found', :body => 'hfyfyh', :author => person); comment2.save! + + + get :directory, :asset => 'comments', :initial => 'a', :category_path => [ 'my-category' ] + + assert_includes assigns(:results)[:comments], comment1 + assert_not_includes assigns(:results)[:comments], comment2 + assert_not_includes assigns(:results)[:comments], comment3 + assert_not_includes assigns(:results)[:comments], comment4 + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 70aa7d6..613c4fb 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -102,4 +102,8 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/assets/my-asset/a/b/c', :controller => 'search', :action => 'assets', :asset => 'my-asset', :category_path => ['a', 'b', 'c']) end + def test_directory_routing + assert_routing('/directory/my-asset/f/a/b/c', :controller => 'search', :action => 'directory', :asset => 'my-asset', :initial => 'f', :category_path => [ 'a', 'b', 'c']) + end + end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 6d0d408..2381c9e 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -217,4 +217,16 @@ class ArticleTest < Test::Unit::TestCase assert_equal [articles[1], articles[0]], person.articles.most_commented(2) end + should 'find by initial' do + person = create_user('testuser').person + + a1 = person.articles.create!(:name => 'An nice article') + a2 = person.articles.create!(:name => 'Better stay off here') + + list = Article.find_by_initial('a') + + assert_includes list, a1 + assert_not_includes list, a2 + end + end diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 3d48feb..14f4795 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -157,4 +157,72 @@ class CategoryFinderTest < ActiveSupport::TestCase # should respect the order (more commented comes first) assert_equal [articles[1], articles[0]], @finder.most_commented_articles(2) end + + should 'find people by initial' do + p1 = create_user('aaaa').person; p1.categories << @category + p2 = create_user('bbbb').person; p2.categories << @category + + list = CategoryFinder.new(@category).find_by_initial(:people, 'a') + + assert_includes list, p1 + assert_not_includes list, p2 + end + + should 'find enterprises by initial' do + ent1 = Enterprise.create!(:name => 'aaaa', :identifier => 'aaaa'); ent1.categories << @category + ent2 = Enterprise.create!(:name => 'bbbb', :identifier => 'bbbb'); ent2.categories << @category + + list = CategoryFinder.new(@category).find_by_initial(:enterprises, 'a') + + assert_includes list, ent1 + assert_not_includes list, ent2 + end + + should 'find communities by initial' do + comm1 = Community.create!(:name => 'aaaa', :identifier => 'aaaa'); comm1.categories << @category + comm2 = Community.create!(:name => 'bbbb', :identifier => 'bbbb'); comm2.categories << @category + + list = CategoryFinder.new(@category).find_by_initial(:communities, 'a') + + assert_includes list, comm1 + assert_not_includes list, comm2 + end + + should 'find products by initial' do + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent') + ent.categories << @category + + p1 = ent.products.create!(:name => 'A product') + p2 = ent.products.create!(:name => 'Better product') + + list = CategoryFinder.new(@category).find_by_initial(:products, 'a') + + assert_includes list, p1 + assert_not_includes list, p2 + end + + should 'find articles by initial' do + person = create_user('testuser').person + a1 = person.articles.create!(:name => 'aaaa', :body => '...', :categories => [@category]) + a2 = person.articles.create!(:name => 'bbbb', :body => '...', :categories => [@category]) + + list = CategoryFinder.new(@category).find_by_initial(:articles, 'a') + + assert_includes list, a1 + assert_not_includes list, a2 + end + + should 'find comments by initial' do + person = create_user('testuser').person + a1 = person.articles.create!(:name => 'aaaa', :body => '...', :categories => [@category]) + + c1 = a1.comments.create!(:title => 'aaaaa', :body => '...', :author => person) + c2 = a1.comments.create!(:title => 'bbbbb', :body => '...', :author => person) + + list = CategoryFinder.new(@category).find_by_initial(:comments, 'a') + + assert_includes list, c1 + assert_not_includes list, c2 + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index afa9ba0..cad72a3 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -143,4 +143,16 @@ class CommentTest < Test::Unit::TestCase assert c.errors.invalid?(:email) end + should 'find by initial' do + owner = create_user('testuser').person + article = owner.articles.create!(:name => 'test', :body => '...') + c1 = article.comments.create!(:title => "A comment", :body => '...', :author => owner) + c2 = article.comments.create!(:title => "Better one", :body => '...', :author => owner) + + list = Comment.find_by_initial('a') + + assert_includes list, c1 + assert_not_includes list, c2 + end + end diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index 207f4ac..d79cafa 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' class EnvironmentFinderTest < ActiveSupport::TestCase all_fixtures - + should 'find articles' do person = create_user('teste').person art = person.articles.build(:name => 'an article to be found'); art.save! @@ -65,4 +65,70 @@ class EnvironmentFinderTest < ActiveSupport::TestCase assert_equal count+1, finder.count('enterprises') end + should 'find articles by initial' do + person = create_user('teste').person + art1 = person.articles.create!(:name => 'an article to be found') + art2 = person.articles.create!(:name => 'blah: an article that cannot be found') + found = EnvironmentFinder.new(Environment.default).find_by_initial(:articles, 'a') + + assert_includes found, art1 + assert_not_includes found, art2 + end + + should 'find people by initial' do + finder = EnvironmentFinder.new(Environment.default) + p1 = create_user('alalala').person + p2 = create_user('blablabla').person + + found = finder.find_by_initial(:people, 'a') + assert_includes found, p1 + assert_not_includes found, p2 + end + + should 'find communities by initial' do + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) + c2 = Community.create!(:name => 'b: another beautiful community', :identifier => 'bbbbb', :environment => Environment.default) + + found = EnvironmentFinder.new(Environment.default).find_by_initial(:communities, 'a') + + assert_includes found, c1 + assert_not_includes found, c2 + end + + should 'find comments by initial' do + person = create_user('teste').person + art = person.articles.build(:name => 'an article to be found'); art.save! + + comment1 = art.comments.build(:title => 'a comment to be found', :body => 'some sample text', :author => person); comment1.save! + comment2 = art.comments.build(:title => 'b: a comment to be found', :body => 'some sample text', :author => person); comment2.save! + + found = EnvironmentFinder.new(Environment.default).find_by_initial(:comments, 'a') + + assert_includes found, comment1 + assert_not_includes found, comment2 + end + + should 'find products by initial' do + finder = EnvironmentFinder.new(Environment.default) + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') + prod1 = ent.products.create!(:name => 'a beautiful product') + prod2 = ent.products.create!(:name => 'b: a beautiful product') + + found = finder.find_by_initial(:products, 'a') + + assert_includes found, prod1 + assert_not_includes found, prod2 + end + + should 'find enterprises by initial' do + finder = EnvironmentFinder.new(Environment.default) + ent1 = Enterprise.create!(:name => 'aaaa', :identifier => 'aaaa') + ent2 = Enterprise.create!(:name => 'bbbb', :identifier => 'bbbb') + + found = finder.find_by_initial(:enterprises, 'a') + + assert_includes found, ent1 + assert_not_includes found, ent2 + end + end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index e623223..09287f5 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -55,4 +55,16 @@ class ProductTest < Test::Unit::TestCase end end + should 'find by initial' do + p1 = Product.create!(:name => 'a test product') + p2 = Product.create!(:name => 'A Capitalize Product') + p3 = Product.create!(:name => 'b-class test product') + + list = Product.find_by_initial('a') + + assert_includes list, p1 + assert_includes list, p2 + assert_not_includes list, p3 + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 514add4..fe39b3b 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -432,6 +432,16 @@ class ProfileTest < Test::Unit::TestCase assert profile.articles.find_by_path('feed').advertise? end + should 'find by initial' do + inside = Profile.create!(:name => 'A person', :identifier => 'aperson') + outside = Profile.create!(:name => 'B Movie', :identifier => 'bmovie') + + list = Profile.find_by_initial('a') + + assert_includes list, inside + assert_not_includes list, outside + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2