From e3914b814f10b33a01e592ff8721f5782c713678 Mon Sep 17 00:00:00 2001 From: jonatack Date: Thu, 23 May 2013 13:33:00 +0200 Subject: [PATCH] Merge further elements from spectator's Rails 3 comptability work, merge pull request #208 from pdf/reorder, use Rails 4 active-record distinct method which replaces uniq --- Gemfile | 2 +- .../adapters/active_record/3.0/context.rb | 19 ++++--------------- .../adapters/active_record/3.1/context.rb | 18 ++++-------------- .../adapters/active_record/3.2/context.rb | 2 +- lib/ransack/adapters/active_record/context.rb | 19 +++++++++++++++++++ lib/ransack/context.rb | 14 ++++++++++++++ ransack.gemspec | 2 +- spec/ransack/search_spec.rb | 5 +++++ spec/support/schema.rb | 1 + 9 files changed, 50 insertions(+), 32 deletions(-) diff --git a/Gemfile b/Gemfile index bb3e0d9..0727fdb 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ gemspec gem 'rake' -rails = ENV['RAILS'] || '4-0-stable' +rails = ENV['RAILS'] || '3-2-stable' gem 'arel' gem 'polyamorous', '0.6.2' diff --git a/lib/ransack/adapters/active_record/3.0/context.rb b/lib/ransack/adapters/active_record/3.0/context.rb index 74cd08e..b05b049 100644 --- a/lib/ransack/adapters/active_record/3.0/context.rb +++ b/lib/ransack/adapters/active_record/3.0/context.rb @@ -24,9 +24,10 @@ module Ransack viz = Visitor.new relation = @object.where(viz.accept(search.base)) if search.sorts.any? - relation = relation.except(:order).order(viz.accept(search.sorts)) + relation = relation.except(:order).reorder(viz.accept(search.sorts)) end - opts[:distinct] ? relation.select("DISTINCT #{@klass.quoted_table_name}.*") : relation + opts[:distinct] ? relation.select( + "DISTINCT #{@klass.quoted_table_name}.*") : relation end def attribute_method?(str, klass = @klass) @@ -51,19 +52,7 @@ module Ransack def table_for(parent) parent.table end - - def klassify(obj) - if Class === obj && ::ActiveRecord::Base > obj - obj - elsif obj.respond_to? :klass - obj.klass - elsif obj.respond_to? :base_klass - obj.base_klass - else - raise ArgumentError, "Don't know how to klassify #{obj}" - end - end - + def type_for(attr) return nil unless attr && attr.valid? klassify(attr.parent).columns_hash[attr.arel_attribute.name.to_s].type diff --git a/lib/ransack/adapters/active_record/3.1/context.rb b/lib/ransack/adapters/active_record/3.1/context.rb index 50eb13c..8cecf22 100644 --- a/lib/ransack/adapters/active_record/3.1/context.rb +++ b/lib/ransack/adapters/active_record/3.1/context.rb @@ -1,4 +1,5 @@ require 'ransack/context' +require 'ransack/adapters/active_record/compat' require 'polyamorous' module Ransack @@ -22,9 +23,10 @@ module Ransack viz = Visitor.new relation = @object.where(viz.accept(search.base)) if search.sorts.any? - relation = relation.except(:order).order(viz.accept(search.sorts)) + relation = relation.except(:order).reorder(viz.accept(search.sorts)) end - opts[:distinct] ? relation.select("DISTINCT #{@klass.quoted_table_name}.*") : relation + opts[:distinct] ? relation.select( + "DISTINCT #{@klass.quoted_table_name}.*") : relation end def attribute_method?(str, klass = @klass) @@ -50,18 +52,6 @@ module Ransack parent.table end - def klassify(obj) - if Class === obj && ::ActiveRecord::Base > obj - obj - elsif obj.respond_to? :klass - obj.klass - elsif obj.respond_to? :base_klass - obj.base_klass - else - raise ArgumentError, "Don't know how to klassify #{obj}" - end - end - def type_for(attr) return nil unless attr && attr.valid? name = attr.arel_attribute.name.to_s diff --git a/lib/ransack/adapters/active_record/3.2/context.rb b/lib/ransack/adapters/active_record/3.2/context.rb index 7dc91bc..49e8589 100644 --- a/lib/ransack/adapters/active_record/3.2/context.rb +++ b/lib/ransack/adapters/active_record/3.2/context.rb @@ -33,7 +33,7 @@ module Ransack viz = Visitor.new relation = @object.where(viz.accept(search.base)) if search.sorts.any? - relation = relation.except(:order).order(viz.accept(search.sorts)) + relation = relation.except(:order).reorder(viz.accept(search.sorts)) end opts[:distinct] ? relation.uniq : relation end diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 50b0f0a..188c032 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -17,6 +17,25 @@ module Ransack object.all end + def type_for(attr) + return nil unless attr && attr.valid? + name = attr.arel_attribute.name.to_s + table = attr.arel_attribute.relation.table_name + + schema_cache = @engine.connection.schema_cache + raise "No table named #{table} exists" unless schema_cache.table_exists?(table) + schema_cache.columns_hash(table)[name].type + end + + def evaluate(search, opts = {}) + viz = Visitor.new + relation = @object.where(viz.accept(search.base)) + if search.sorts.any? + relation = relation.except(:order).reorder(viz.accept(search.sorts)) + end + opts[:distinct] ? relation.distinct : relation + end + end end end diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 63ca954..b9df917 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -44,6 +44,20 @@ module Ransack end end + def klassify(obj) + if Class === obj && ::ActiveRecord::Base > obj + obj + elsif obj.respond_to? :klass + obj.klass + elsif obj.respond_to? :active_record # rails 3 + obj.active_record + elsif obj.respond_to? :base_klass # rails 4 + obj.base_klass + else + raise ArgumentError, "Don't know how to klassify #{obj}" + end + end + # Convert a string representing a chain of associations and an attribute # into the attribute itself def contextualize(str) diff --git a/ransack.gemspec b/ransack.gemspec index 1d469a4..6a40287 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.add_dependency 'activerecord' s.add_dependency 'actionpack' - s.add_dependency 'polyamorous' + s.add_dependency 'polyamorous', '~> 0.6.0' s.add_development_dependency 'rspec', '~> 2.8.0' s.add_development_dependency 'machinist', '~> 1.0.6' s.add_development_dependency 'faker', '~> 0.9.5' diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 88e3e48..d5d3ded 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -217,6 +217,11 @@ module Ransack id_sort.dir.should eq 'desc' name_sort.dir.should eq 'asc' end + + it 'overrides existing sort' do + @s.sorts = 'id asc' + @s.result.first.id.should eq 1 + end end describe '#method_missing' do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index cdf1355..c202c7e 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -6,6 +6,7 @@ ActiveRecord::Base.establish_connection( ) class Person < ActiveRecord::Base + default_scope order('id DESC') belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id has_many :children, :class_name => 'Person', :foreign_key => :parent_id has_many :articles