From c126d2e9111a3ddbac8f91218e6fb7e7054727c2 Mon Sep 17 00:00:00 2001 From: Andrey Koleshko Date: Sat, 17 Nov 2012 16:53:02 +0300 Subject: [PATCH 1/3] Add rake and sqlite3 gems to gemfile to run tests When I'm trying to run tests in terminal with `rake` command I see the following errors: * rake should be included to Gemfile * sqlite3 should be included in the Gemfile This changes fix these issues --- Gemfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 5c6c4af..f91ae1e 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,9 @@ gem "activerecord", "~> 3.1" gem "activesupport", "~> 3.1" gem "polyamorous", "~> 0.5.0" gem "actionpack", "~> 3.1" +gem 'rake' +gem 'sqlite3' group :development do gem "shoulda", "~> 2.11" -end \ No newline at end of file +end From 0179a920aabc857a0df6c1d4aadbf24b1c3dc0b7 Mon Sep 17 00:00:00 2001 From: Andrey Koleshko Date: Sat, 17 Nov 2012 17:23:03 +0300 Subject: [PATCH 2/3] Separated validator for :in where When I pass in where array like ["", ""] it must not generate select like select * from somthing where id in (NULL) --- Gemfile | 1 + lib/meta_search/where.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index f91ae1e..550f1a0 100644 --- a/Gemfile +++ b/Gemfile @@ -11,4 +11,5 @@ gem 'sqlite3' group :development do gem "shoulda", "~> 2.11" + gem 'debugger' end diff --git a/lib/meta_search/where.rb b/lib/meta_search/where.rb index c071d04..5f17ebb 100644 --- a/lib/meta_search/where.rb +++ b/lib/meta_search/where.rb @@ -215,7 +215,7 @@ def create_where_from_args(*args) unless opts[:formatter].respond_to?(:call) raise ArgumentError, "Invalid formatter for #{opts[:name]}, should be a Proc or String." end - opts[:validator] ||= Proc.new {|param| !param.blank?} + opts[:validator] ||= validator_for_name(opts[:name]) unless opts[:validator].respond_to?(:call) raise ArgumentError, "Invalid validator for #{opts[:name]}, should be a Proc." end @@ -257,6 +257,15 @@ def create_where_compounds_for(where) ) end end + + def validator_for_name(name) + case name.to_sym + when :in + Proc.new {|param| param.is_a?(Array) && param.reject(&:blank?).present?} + else + Proc.new {|param| !param.blank?} + end + end end end From fd9e9c9e007bdd17261245f89e6b41aebcb3b193 Mon Sep 17 00:00:00 2001 From: Andrey Koleshko Date: Thu, 12 Dec 2013 21:46:02 +0300 Subject: [PATCH 3/3] Integration with rails 4 --- Gemfile | 14 +- README.rdoc | 2 +- lib/meta_search/builder.rb | 8 +- lib/meta_search/helpers/form_builder.rb | 2 +- lib/meta_search/helpers/form_helper.rb | 4 +- meta_search.gemspec | 40 +++-- test/fixtures/company.rb | 2 +- test/fixtures/data_type.rb | 1 - test/fixtures/data_types.yml | 6 +- test/fixtures/developer.rb | 4 +- test/helper.rb | 4 +- test/test_search.rb | 226 ++++++++++++------------ test/test_view_helpers.rb | 22 ++- 13 files changed, 175 insertions(+), 160 deletions(-) diff --git a/Gemfile b/Gemfile index 550f1a0..dcbda34 100644 --- a/Gemfile +++ b/Gemfile @@ -1,15 +1,15 @@ -source :rubygems +source 'https://rubygems.org' # Don't do a "gemspec" here. Seriously. It jacks up Jeweler. -gem "activerecord", "~> 3.1" -gem "activesupport", "~> 3.1" -gem "polyamorous", "~> 0.5.0" -gem "actionpack", "~> 3.1" +gem "activerecord", "~> 4.0.1" +gem "activesupport", "~> 4.0.1" +gem "polyamorous", "~> 0.6.4" +gem "actionpack", "~> 4.0.1" gem 'rake' -gem 'sqlite3' group :development do + gem 'sqlite3' gem "shoulda", "~> 2.11" - gem 'debugger' + gem 'jeweler' end diff --git a/README.rdoc b/README.rdoc index 4b60e80..f98b554 100644 --- a/README.rdoc +++ b/README.rdoc @@ -30,7 +30,7 @@ In your controller: def index @search = Article.search(params[:search]) - @articles = @search.all # load all matching records + @articles = @search.to_a # load all matching records # @articles = @search.relation # Retrieve the relation, to lazy-load in view # @articles = @search.paginate(:page => params[:page]) # Who doesn't love will_paginate? end diff --git a/lib/meta_search/builder.rb b/lib/meta_search/builder.rb index 024a080..9421160 100644 --- a/lib/meta_search/builder.rb +++ b/lib/meta_search/builder.rb @@ -30,7 +30,7 @@ class Builder # for how it will expose this model and its associations to your controllers/views. def initialize(base_or_relation, opts = {}) opts = opts.dup - @relation = base_or_relation.scoped + @relation = base_or_relation.all @base = @relation.klass @search_key = (opts.delete(:search_key) || 'search').to_s @options = opts # Let's just hang on to other options for use in authorization blocks @@ -51,13 +51,13 @@ def get_association(assoc, base = @base) def get_attribute(name, parent = @join_dependency.join_base) attribute = nil - if get_column(name, parent.active_record) + if get_column(name, parent.base_klass) attribute = parent.table[name] elsif (segments = name.to_s.split(/_/)).size > 1 remainder = [] found_assoc = nil while remainder.unshift(segments.pop) && segments.size > 0 && !found_assoc do - if found_assoc = get_association(segments.join('_'), parent.active_record) + if found_assoc = get_association(segments.join('_'), parent.base_klass) if found_assoc.options[:polymorphic] unless delimiter = remainder.index('type') raise PolymorphicAssociationMissingTypeError, "Polymorphic association specified without a type" @@ -82,7 +82,7 @@ def get_attribute(name, parent = @join_dependency.join_base) # MetaSearch::Where def build(option_hash) opts = option_hash.dup || {} - @relation = @base.scoped + @relation = @base.all opts.stringify_keys! opts = collapse_multiparameter_options(opts) assign_attributes(opts) diff --git a/lib/meta_search/helpers/form_builder.rb b/lib/meta_search/helpers/form_builder.rb index 944da5e..d63dc39 100644 --- a/lib/meta_search/helpers/form_builder.rb +++ b/lib/meta_search/helpers/form_builder.rb @@ -114,7 +114,7 @@ def checks(method, choices = [], options = {}, &block) # # Example: # - # <% f.collection_checks :head_sizes_in, HeadSize.all, + # <% f.collection_checks :head_sizes_in, HeadSize.to_a, # :id, :name, :class => 'headcheck' do |check| %> # <%= check.box %> <%= check.label %> # <% end %> diff --git a/lib/meta_search/helpers/form_helper.rb b/lib/meta_search/helpers/form_helper.rb index 5ed7f79..8b5de1d 100644 --- a/lib/meta_search/helpers/form_helper.rb +++ b/lib/meta_search/helpers/form_helper.rb @@ -1,14 +1,14 @@ module MetaSearch module Helpers module FormHelper - def apply_form_for_options!(object_or_array, options) + def apply_form_for_options!(object_or_array, _record, options) if object_or_array.is_a?(MetaSearch::Builder) builder = object_or_array options[:url] ||= polymorphic_path(object_or_array.base) elsif object_or_array.is_a?(Array) && (builder = object_or_array.detect {|o| o.is_a?(MetaSearch::Builder)}) options[:url] ||= polymorphic_path(object_or_array.map {|o| o.is_a?(MetaSearch::Builder) ? o.base : o}) else - super + super return end diff --git a/meta_search.gemspec b/meta_search.gemspec index c53c7f3..1286bca 100644 --- a/meta_search.gemspec +++ b/meta_search.gemspec @@ -2,6 +2,7 @@ # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- +# stub: meta_search 1.1.3 ruby lib Gem::Specification.new do |s| s.name = "meta_search" @@ -9,7 +10,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Ernie Miller"] - s.date = "2012-02-02" + s.date = "2013-12-25" s.description = "\n Allows simple search forms to be created against an AR3 model\n and its associations, has useful view helpers for sort links\n and multiparameter fields as well.\n " s.email = "ernie@metautonomo.us" s.extra_rdoc_files = [ @@ -60,31 +61,40 @@ Gem::Specification.new do |s| s.homepage = "http://metautonomo.us/projects/metasearch/" s.post_install_message = "\n*** Thanks for installing MetaSearch! ***\nBe sure to check out http://metautonomo.us/projects/metasearch/ for a\nwalkthrough of MetaSearch's features, and click the donate button if\nyou're feeling especially appreciative. It'd help me justify this\n\"open source\" stuff to my lovely wife. :)\n\n" s.require_paths = ["lib"] - s.rubygems_version = "1.8.15" + s.rubygems_version = "2.2.0.preview.1" s.summary = "Object-based searching (and more) for simply creating search forms." if s.respond_to? :specification_version then - s.specification_version = 3 + s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_runtime_dependency(%q, ["~> 3.1"]) - s.add_runtime_dependency(%q, ["~> 3.1"]) - s.add_runtime_dependency(%q, ["~> 0.5.0"]) - s.add_runtime_dependency(%q, ["~> 3.1"]) + s.add_runtime_dependency(%q, ["~> 4.0.1"]) + s.add_runtime_dependency(%q, ["~> 4.0.1"]) + s.add_runtime_dependency(%q, ["~> 0.6.4"]) + s.add_runtime_dependency(%q, ["~> 4.0.1"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, ["~> 2.11"]) + s.add_development_dependency(%q, [">= 0"]) else - s.add_dependency(%q, ["~> 3.1"]) - s.add_dependency(%q, ["~> 3.1"]) - s.add_dependency(%q, ["~> 0.5.0"]) - s.add_dependency(%q, ["~> 3.1"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, ["~> 0.6.4"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.11"]) + s.add_dependency(%q, [">= 0"]) end else - s.add_dependency(%q, ["~> 3.1"]) - s.add_dependency(%q, ["~> 3.1"]) - s.add_dependency(%q, ["~> 0.5.0"]) - s.add_dependency(%q, ["~> 3.1"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, ["~> 0.6.4"]) + s.add_dependency(%q, ["~> 4.0.1"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.11"]) + s.add_dependency(%q, [">= 0"]) end end diff --git a/test/fixtures/company.rb b/test/fixtures/company.rb index db42ea8..82b2eb8 100644 --- a/test/fixtures/company.rb +++ b/test/fixtures/company.rb @@ -1,7 +1,7 @@ class Company < ActiveRecord::Base has_many :developers has_many :developer_notes, :through => :developers, :source => :notes - has_many :slackers, :class_name => "Developer", :conditions => {:slacker => true} + has_many :slackers, -> { where(:slacker => true) }, :class_name => "Developer" has_many :notes, :as => :notable has_many :data_types diff --git a/test/fixtures/data_type.rb b/test/fixtures/data_type.rb index 6f294fd..3fafd0d 100644 --- a/test/fixtures/data_type.rb +++ b/test/fixtures/data_type.rb @@ -1,5 +1,4 @@ class DataType < ActiveRecord::Base belongs_to :company attr_unsearchable :str - attr_protected :str end \ No newline at end of file diff --git a/test/fixtures/data_types.yml b/test/fixtures/data_types.yml index e8f81f3..5ad7688 100644 --- a/test/fixtures/data_types.yml +++ b/test/fixtures/data_types.yml @@ -6,9 +6,9 @@ dt_<%= n %>: int : <%= n ** 3 %> flt : <%= n.to_f / 2.0 %> dec : <%= n.to_f ** (n + 0.1) %> - dtm : <%= (Time.local(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %> - tms : <%= (Time.local(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %> - tim : <%= Time.local(2000, 01, 01, n+8, n).in_time_zone.to_s(:db) %> + dtm : <%= (Time.utc(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %> + tms : <%= (Time.utc(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %> + tim : <%= Time.utc(2000, 01, 01, n+8, n).in_time_zone.to_s(:db) %> dat : <%= (Date.new(2009, 12, 24) + n).strftime("%Y-%m-%d") %> bin : <%= "BLOB#{n}" * n %> bln : <%= n % 2 > 0 ? true : false %> diff --git a/test/fixtures/developer.rb b/test/fixtures/developer.rb index 4ca38d4..f9ecb41 100644 --- a/test/fixtures/developer.rb +++ b/test/fixtures/developer.rb @@ -6,6 +6,6 @@ class Developer < ActiveRecord::Base attr_searchable :name, :salary, :if => proc {|s| !s.options[:user] || s.options[:user] == 'privileged'} assoc_searchable :notes, :projects, :company, :if => proc {|s| !s.options[:user] || s.options[:user] == 'privileged'} - scope :sort_by_salary_and_name_asc, order('salary ASC, name ASC') - scope :sort_by_salary_and_name_desc, order('salary DESC, name DESC') + scope :sort_by_salary_and_name_asc, -> { order('salary ASC, name ASC') } + scope :sort_by_salary_and_name_desc, -> { order('salary DESC, name DESC') } end \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index 4c42f10..f39e491 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -19,12 +19,12 @@ dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies dep.autoload_paths.unshift FIXTURES_PATH -ActiveRecord::Base.silence do +ActiveRecord::Base.logger.quietly do ActiveRecord::Migration.verbose = false load File.join(FIXTURES_PATH, 'schema.rb') end -ActiveRecord::Fixtures.create_fixtures(FIXTURES_PATH, ActiveRecord::Base.connection.tables) +ActiveRecord::FixtureSet.create_fixtures(FIXTURES_PATH, ActiveRecord::Base.connection.tables) I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml')] diff --git a/test/test_search.rb b/test/test_search.rb index b0fb22d..2e207f6 100644 --- a/test/test_search.rb +++ b/test/test_search.rb @@ -81,11 +81,11 @@ class TestSearch < Test::Unit::TestCase context "without any opts" do should "find a null entry when searching notes" do - assert_equal 1, @s.notes_note_is_null(true).all.size + assert_equal 1, @s.notes_note_is_null(true).to_a.size end should "find no non-null entry when searching notes" do - assert_equal 0, @s.notes_note_is_not_null(true).all.size + assert_equal 0, @s.notes_note_is_not_null(true).to_a.size end end @@ -95,11 +95,11 @@ class TestSearch < Test::Unit::TestCase end should "find a null entry when searching notes" do - assert_equal 1, @s.notes_note_is_null(true).all.size + assert_equal 1, @s.notes_note_is_null(true).to_a.size end should "find no non-null entry when searching notes" do - assert_equal 0, @s.notes_note_is_not_null(true).all.size + assert_equal 0, @s.notes_note_is_not_null(true).to_a.size end end @@ -109,11 +109,11 @@ class TestSearch < Test::Unit::TestCase end should "find no null entry when searching notes" do - assert_equal 0, @s.notes_note_is_null(true).all.size + assert_equal 0, @s.notes_note_is_null(true).to_a.size end should "find no non-null entry when searching notes" do - assert_equal 0, @s.notes_note_is_not_null(true).all.size + assert_equal 0, @s.notes_note_is_not_null(true).to_a.size end end @@ -121,10 +121,10 @@ class TestSearch < Test::Unit::TestCase end [{:name => 'Company', :object => Company}, - {:name => 'Company as a Relation', :object => Company.scoped}].each do |object| + {:name => 'Company as a Relation', :object => Company.all}].each do |object| context_a_search_against object[:name], object[:object] do should "have a relation attribute which is an ActiveRecord::Relation" do - assert_equal ActiveRecord::Relation, @s.relation.class + assert_equal ActiveRecord::Relation::ActiveRecord_Relation_Company, @s.relation.class end should "have a base attribute which is a Class inheriting from ActiveRecord::Base" do @@ -211,27 +211,27 @@ class TestSearch < Test::Unit::TestCase should "not raise an error, just ignore sorting" do assert_nothing_raised do - assert_equal Company.all, @s.all + assert_equal Company.all, @s.relation.to_a end end end should "sort by name in ascending order" do @s.meta_sort = 'name.asc' - assert_equal Company.order('name asc').all, - @s.all + assert_equal Company.order('name asc').to_a, + @s.relation.to_a end should "sort by name in ascending order as a method call" do @s.meta_sort 'name.asc' - assert_equal Company.order('name asc').all, - @s.all + assert_equal Company.order('name asc').to_a, + @s.relation.to_a end should "sort by name in descending order" do @s.meta_sort = 'name.desc' - assert_equal Company.order('name desc').all, - @s.all + assert_equal Company.order('name desc').to_a, + @s.relation.to_a end context "where name contains optical" do @@ -240,15 +240,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Advanced Optical Solutions" do - assert_contains @s.all, Company.where(:name => 'Advanced Optical Solutions').first + assert_contains @s.relation.to_a, Company.where(:name => 'Advanced Optical Solutions').first end should "not return a company named Initech" do - assert_does_not_contain @s.all, Company.where(:name => "Initech").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Initech").first end end @@ -258,15 +258,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Advanced Optical Solutions" do - assert_contains @s.all, Company.where(:name => 'Advanced Optical Solutions').first + assert_contains @s.relation.to_a, Company.where(:name => 'Advanced Optical Solutions').first end should "not return a company named Initech" do - assert_does_not_contain @s.all, Company.where(:name => "Initech").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Initech").first end end @@ -276,15 +276,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Mission Data" do - assert_contains @s.all, Company.where(:name => 'Mission Data').first + assert_contains @s.relation.to_a, Company.where(:name => 'Mission Data').first end should "not return a company named Initech" do - assert_does_not_contain @s.all, Company.where(:name => "Initech").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Initech").first end context "and slackers salary is greater than $70k" do @@ -293,7 +293,7 @@ class TestSearch < Test::Unit::TestCase end should "return no results" do - assert_equal 0, @s.all.size + assert_equal 0, @s.relation.to_a.size end should "join developers twice" do @@ -312,15 +312,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Advanced Optical Solutions" do - assert_contains @s.all, Company.where(:name => 'Advanced Optical Solutions').first + assert_contains @s.relation.to_a, Company.where(:name => 'Advanced Optical Solutions').first end should "not return a company named Mission Data" do - assert_does_not_contain @s.all, Company.where(:name => "Mission Data").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Mission Data").first end end @@ -330,15 +330,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Advanced Optical Solutions" do - assert_contains @s.all, Company.where(:name => 'Advanced Optical Solutions').first + assert_contains @s.relation.to_a, Company.where(:name => 'Advanced Optical Solutions').first end should "not return a company named Mission Data" do - assert_does_not_contain @s.all, Company.where(:name => "Mission Data").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Mission Data").first end end @@ -348,16 +348,16 @@ class TestSearch < Test::Unit::TestCase end should "return two results, one of which is a duplicate due to joins" do - assert_equal 2, @s.all.size - assert_equal 1, @s.all.uniq.size + assert_equal 2, @s.relation.to_a.size + assert_equal 1, @s.relation.to_a.uniq.size end should "return a company named Advanced Optical Solutions" do - assert_contains @s.all, Company.where(:name => 'Advanced Optical Solutions').first + assert_contains @s.relation.to_a, Company.where(:name => 'Advanced Optical Solutions').first end should "not return a company named Mission Data" do - assert_does_not_contain @s.all, Company.where(:name => "Mission Data").first + assert_does_not_contain @s.relation.to_a, Company.where(:name => "Mission Data").first end end @@ -367,11 +367,11 @@ class TestSearch < Test::Unit::TestCase end should "return 1 result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Initech" do - assert_contains @s.all, Company.where(:name => 'Initech').first + assert_contains @s.relation.to_a, Company.where(:name => 'Initech').first end end @@ -381,11 +381,11 @@ class TestSearch < Test::Unit::TestCase end should "return 1 result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Initech" do - assert_contains @s.all, Company.where(:name => 'Initech').first + assert_contains @s.relation.to_a, Company.where(:name => 'Initech').first end end @@ -395,11 +395,11 @@ class TestSearch < Test::Unit::TestCase end should "return 1 result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a company named Initech" do - assert_contains @s.all, Company.where(:name => 'Initech').first + assert_contains @s.relation.to_a, Company.where(:name => 'Initech').first end end @@ -418,7 +418,7 @@ class TestSearch < Test::Unit::TestCase end [{:name => 'Developer', :object => Developer}, - {:name => 'Developer as a Relation', :object => Developer.scoped}].each do |object| + {:name => 'Developer as a Relation', :object => Developer.all}].each do |object| context_a_search_against object[:name], object[:object] do should "exclude the column named company_id" do assert_nil @s.get_column(:company_id) @@ -434,8 +434,8 @@ class TestSearch < Test::Unit::TestCase end should "sort by company name in ascending order" do - assert_equal Developer.joins(:company).order('companies.name asc').all, - @s.all + assert_equal Developer.joins(:company).order('companies.name asc').to_a, + @s.relation.to_a end end @@ -445,8 +445,8 @@ class TestSearch < Test::Unit::TestCase end should "sort by company name in descending order" do - assert_equal Developer.joins(:company).order('companies.name desc').all, - @s.all + assert_equal Developer.joins(:company).order('companies.name desc').to_a, + @s.relation.to_a end end @@ -456,8 +456,8 @@ class TestSearch < Test::Unit::TestCase end should "sort by salary and name in descending order" do - assert_equal Developer.order('salary DESC, name DESC').all, - @s.all + assert_equal Developer.order('salary DESC, name DESC').to_a, + @s.relation.to_a end end @@ -467,7 +467,7 @@ class TestSearch < Test::Unit::TestCase end should "return Peter Gibbons" do - assert_contains @s.all, Developer.where(:name => 'Peter Gibbons').first + assert_contains @s.relation.to_a, Developer.where(:name => 'Peter Gibbons').first end end @@ -477,7 +477,7 @@ class TestSearch < Test::Unit::TestCase end should "return Michael Bolton and all employees of Mission Data" do - assert_equal @s.all, Developer.where(:name => 'Michael Bolton').all + + assert_equal @s.relation.to_a, Developer.where(:name => 'Michael Bolton').to_a + Company.where(:name => 'Mission Data').first.developers end end @@ -488,15 +488,15 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a developer named Ernie Miller" do - assert_contains @s.all, Developer.where(:name => 'Ernie Miller').first + assert_contains @s.relation.to_a, Developer.where(:name => 'Ernie Miller').first end should "not return a developer named Herb Myers" do - assert_does_not_contain @s.all, Developer.where(:name => "Herb Myers").first + assert_does_not_contain @s.relation.to_a, Developer.where(:name => "Herb Myers").first end end @@ -506,15 +506,15 @@ class TestSearch < Test::Unit::TestCase end should "return three results" do - assert_equal 3, @s.all.size + assert_equal 3, @s.relation.to_a.size end should "return a developer named Ernie Miller" do - assert_contains @s.all, Developer.where(:name => 'Ernie Miller').first + assert_contains @s.relation.to_a, Developer.where(:name => 'Ernie Miller').first end should "not return a developer named Samir Nagheenanajar" do - assert_does_not_contain @s.all, Developer.where(:name => "Samir Nagheenanajar").first + assert_does_not_contain @s.relation.to_a, Developer.where(:name => "Samir Nagheenanajar").first end end @@ -524,11 +524,11 @@ class TestSearch < Test::Unit::TestCase end should "return eight results" do - assert_equal 8, @s.all.size + assert_equal 8, @s.relation.to_a.size end should "not return a developer named Ernie Miller" do - assert_does_not_contain @s.all, Developer.where(:name => "Ernie Miller").first + assert_does_not_contain @s.relation.to_a, Developer.where(:name => "Ernie Miller").first end end @@ -538,15 +538,15 @@ class TestSearch < Test::Unit::TestCase end should "return two results" do - assert_equal 2, @s.all.size + assert_equal 2, @s.relation.to_a.size end should "return a developer named Samir Nagheenanajar" do - assert_contains @s.all, Developer.where(:name => "Samir Nagheenanajar").first + assert_contains @s.relation.to_a, Developer.where(:name => "Samir Nagheenanajar").first end should "not return a developer named Ernie Miller" do - assert_does_not_contain @s.all, Developer.where(:name => 'Ernie Miller').first + assert_does_not_contain @s.relation.to_a, Developer.where(:name => 'Ernie Miller').first end end @@ -556,11 +556,11 @@ class TestSearch < Test::Unit::TestCase end should "return three results" do - assert_equal 3, @s.all.size + assert_equal 3, @s.relation.to_a.size end should "return these developers" do - assert_same_elements @s.all.collect {|d| d.name}, + assert_same_elements @s.relation.to_a.collect {|d| d.name}, ['Peter Gibbons', 'Michael Bolton', 'Samir Nagheenanajar'] end end @@ -571,7 +571,7 @@ class TestSearch < Test::Unit::TestCase end should "return no results" do - assert_equal 0, @s.all.size + assert_equal 0, @s.relation.to_a.size end end @@ -581,22 +581,22 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "return a developer named Ernie Miller" do - assert_contains @s.all, Developer.where(:name => 'Ernie Miller').first + assert_contains @s.relation.to_a, Developer.where(:name => 'Ernie Miller').first end should "not return a developer named Herb Myers" do - assert_does_not_contain @s.all, Developer.where(:name => "Herb Myers").first + assert_does_not_contain @s.relation.to_a, Developer.where(:name => "Herb Myers").first end end end end [{:name => 'DataType', :object => DataType}, - {:name => 'DataType as a Relation', :object => DataType.scoped}].each do |object| + {:name => 'DataType as a Relation', :object => DataType.all}].each do |object| context_a_search_against object[:name], object[:object] do should "raise an error on a contains search against a boolean column" do assert_raise NoMethodError do @@ -610,11 +610,11 @@ class TestSearch < Test::Unit::TestCase end should "return five results" do - assert_equal 5, @s.all.size + assert_equal 5, @s.relation.to_a.size end should "contain no results with a false boolean column" do - assert_does_not_contain @s.all.collect {|r| r.bln}, false + assert_does_not_contain @s.relation.to_a.collect {|r| r.bln}, false end end @@ -624,11 +624,11 @@ class TestSearch < Test::Unit::TestCase end should "return five results" do - assert_equal 5, @s.all.size + assert_equal 5, @s.relation.to_a.size end should "contain no results with a false boolean column" do - assert_does_not_contain @s.all.collect {|r| r.bln}, false + assert_does_not_contain @s.relation.to_a.collect {|r| r.bln}, false end end @@ -638,11 +638,11 @@ class TestSearch < Test::Unit::TestCase end should "return four results" do - assert_equal 4, @s.all.size + assert_equal 4, @s.relation.to_a.size end should "contain no results with a true boolean column" do - assert_does_not_contain @s.all.collect {|r| r.bln}, true + assert_does_not_contain @s.relation.to_a.collect {|r| r.bln}, true end end @@ -652,11 +652,11 @@ class TestSearch < Test::Unit::TestCase end should "return four results" do - assert_equal 4, @s.all.size + assert_equal 4, @s.relation.to_a.size end should "contain no results with a true boolean column" do - assert_does_not_contain @s.all.collect {|r| r.bln}, true + assert_does_not_contain @s.relation.to_a.collect {|r| r.bln}, true end end @@ -666,7 +666,7 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "contain a result with Christmas 2009 as its date" do @@ -680,7 +680,7 @@ class TestSearch < Test::Unit::TestCase end should "return one result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "contain a result with Christmas 2009 as its date" do @@ -695,11 +695,11 @@ class TestSearch < Test::Unit::TestCase end should "return three results" do - assert_equal 3, @s.all.size + assert_equal 3, @s.relation.to_a.size end should "not contain results with time column before or after constraints" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| r.tim < Time.parse('2000-01-01 13:00') || r.tim > Time.parse('2000-01-01 15:30') } end @@ -711,11 +711,11 @@ class TestSearch < Test::Unit::TestCase end should "return two results" do - assert_equal 2, @s.all.size + assert_equal 2, @s.relation.to_a.size end should "not contain results with timestamp column before 2010" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| r.tms < Time.utc(2010, 1, 1) } end @@ -727,11 +727,11 @@ class TestSearch < Test::Unit::TestCase end should "return seven results" do - assert_equal 7, @s.all.size + assert_equal 7, @s.relation.to_a.size end should "not contain results with timestamp in 2010" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| r.tms >= Time.utc(2010, 1, 1) } end @@ -743,11 +743,11 @@ class TestSearch < Test::Unit::TestCase end should "return four results" do - assert_equal 4, @s.all.size + assert_equal 4, @s.relation.to_a.size end should "not contain results with decimal column <= 5000" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| r.dec <= 5000 } end @@ -760,11 +760,11 @@ class TestSearch < Test::Unit::TestCase end should "return three results" do - assert_equal 3, @s.all.size + assert_equal 3, @s.relation.to_a.size end should "not contain results with float column outside constraints" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| r.flt < 2.5 || r.flt > 3.5 } end @@ -776,11 +776,11 @@ class TestSearch < Test::Unit::TestCase end should "return three results" do - assert_equal 3, @s.all.size + assert_equal 3, @s.relation.to_a.size end should "not contain results outside the specified set" do - assert_equal [], @s.all.select {|r| + assert_equal [], @s.relation.to_a.select {|r| ![1, 8, 729].include?(r.int) } end @@ -792,11 +792,11 @@ class TestSearch < Test::Unit::TestCase end should "return six results" do - assert_equal 6, @s.all.size + assert_equal 6, @s.relation.to_a.size end should "not contain results outside the specified set" do - assert_equal [], @s.all.reject {|r| + assert_equal [], @s.relation.to_a.reject {|r| ![1, 8, 729].include?(r.int) } end @@ -807,7 +807,7 @@ class TestSearch < Test::Unit::TestCase context_a_search_against "a relation with existing criteria and joins", Company.where(:name => "Initech").joins(:developers) do should "return the same results as a non-searched relation with no search terms" do - assert_equal Company.where(:name => "Initech").joins(:developers).all, @s.all + assert_equal Company.where(:name => "Initech").joins(:developers).to_a, @s.relation.to_a end context "with a search against the joined association's data" do @@ -820,7 +820,7 @@ class TestSearch < Test::Unit::TestCase end should "return a filtered result set based on the criteria of the searched relation" do - assert_equal Company.where(:name => 'Initech').all, @s.all.uniq + assert_equal Company.where(:name => 'Initech').to_a, @s.relation.to_a.uniq end end end @@ -829,12 +829,12 @@ class TestSearch < Test::Unit::TestCase Company.where(:name => "Initech").first.developers do should "not raise an error" do assert_nothing_raised do - @s.all + @s.relation.to_a end end should "return all developers for that company without conditions" do - assert_equal Company.where(:name => 'Initech').first.developers.all, @s.all + assert_equal Company.where(:name => 'Initech').first.developers.to_a, @s.relation.to_a end should "allow conditions on the search" do @@ -848,12 +848,12 @@ class TestSearch < Test::Unit::TestCase Company.where(:name => "Initech").first.developer_notes do should "not raise an error" do assert_nothing_raised do - @s.all + @s.relation.to_a end end should "return all developer notes for that company without conditions" do - assert_equal Company.where(:name => 'Initech').first.developer_notes.all, @s.all + assert_equal Company.where(:name => 'Initech').first.developer_notes.to_a, @s.relation.to_a end should "allow conditions on the search" do @@ -864,7 +864,7 @@ class TestSearch < Test::Unit::TestCase end [{:name => 'Project', :object => Project}, - {:name => 'Project as a Relation', :object => Project.scoped}].each do |object| + {:name => 'Project as a Relation', :object => Project.all}].each do |object| context_a_search_against object[:name], object[:object] do context "where name is present" do setup do @@ -872,11 +872,11 @@ class TestSearch < Test::Unit::TestCase end should "return 5 results" do - assert_equal 5, @s.all.size + assert_equal 5, @s.relation.to_a.size end should "contain no results with a blank name column" do - assert_equal 0, @s.all.select {|r| r.name.blank?}.size + assert_equal 0, @s.relation.to_a.select {|r| r.name.blank?}.size end end @@ -886,11 +886,11 @@ class TestSearch < Test::Unit::TestCase end should "return 2 results" do - assert_equal 2, @s.all.size + assert_equal 2, @s.relation.to_a.size end should "contain no results with a present name column" do - assert_equal 0, @s.all.select {|r| r.name.present?}.size + assert_equal 0, @s.relation.to_a.select {|r| r.name.present?}.size end end @@ -900,11 +900,11 @@ class TestSearch < Test::Unit::TestCase end should "return 1 result" do - assert_equal 1, @s.all.size + assert_equal 1, @s.relation.to_a.size end should "contain no results with a non-null name column" do - assert_equal 0, @s.all.select {|r| r.name != nil}.size + assert_equal 0, @s.relation.to_a.select {|r| r.name != nil}.size end end @@ -914,11 +914,11 @@ class TestSearch < Test::Unit::TestCase end should "return 6 results" do - assert_equal 6, @s.all.size + assert_equal 6, @s.relation.to_a.size end should "contain no results with a null name column" do - assert_equal 0, @s.all.select {|r| r.name = nil}.size + assert_equal 0, @s.relation.to_a.select {|r| r.name = nil}.size end end @@ -928,35 +928,35 @@ class TestSearch < Test::Unit::TestCase end should "return 2 results" do - assert_equal 2, @s.all.size + assert_equal 2, @s.relation.to_a.size end should "contain no results with notes" do - assert_equal 0, @s.all.select {|r| r.notes.size > 0}.size + assert_equal 0, @s.relation.to_a.select {|r| r.notes.size > 0}.size end end end end [{:name => 'Note', :object => Note}, - {:name => 'Note as a Relation', :object => Note.scoped}].each do |object| + {:name => 'Note as a Relation', :object => Note.all}].each do |object| context_a_search_against object[:name], object[:object] do should "allow search on polymorphic belongs_to associations" do @s.notable_project_type_name_contains = 'MetaSearch' - assert_equal Project.find_by_name('MetaSearch Development').notes, @s.all + assert_equal Project.find_by_name('MetaSearch Development').notes, @s.relation.to_a end should "allow search on multiple polymorphic belongs_to associations" do @s.notable_project_type_name_or_notable_developer_type_name_starts_with = 'M' assert_equal Project.find_by_name('MetaSearch Development').notes + Developer.find_by_name('Michael Bolton').notes, - @s.all + @s.relation.to_a end should "allow traversal of polymorphic associations" do @s.notable_developer_type_company_name_starts_with = 'M' assert_equal Company.find_by_name('Mission Data').developers.map(&:notes).flatten.sort {|a, b| a.id <=>b.id}, - @s.all.sort {|a, b| a.id <=> b.id} + @s.relation.to_a.sort {|a, b| a.id <=> b.id} end should "raise an error when attempting to search against polymorphic belongs_to association without a type" do diff --git a/test/test_view_helpers.rb b/test/test_view_helpers.rb index b9a1d72..32e0836 100644 --- a/test/test_view_helpers.rb +++ b/test/test_view_helpers.rb @@ -6,6 +6,12 @@ class TestViewHelpers < ActionView::TestCase tests MetaSearch::Helpers::FormHelper include MetaSearch::Helpers::UrlHelper + include Shoulda::InstanceMethods + extend Shoulda::ClassMethods + include Shoulda::Assertions + extend Shoulda::Macros + include Shoulda::Helpers + def self.router @router ||= begin router = ActionDispatch::Routing::RouteSet.new @@ -14,7 +20,7 @@ def self.router resources :companies resources :projects resources :notes - match ':controller(/:action(/:id(.:format)))' + get ':controller(/:action(/:id(.:format)))' end router end @@ -50,7 +56,7 @@ def setup end should "use the default localization for predicates" do - assert_match /Name isn't null/, @f1.label(:name_is_not_null) + assert_match /Name isn\&\#39\;t null/, @f1.label(:name_is_not_null) end context "in the Flanders locale" do @@ -92,7 +98,7 @@ def setup end assert_match /