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
This commit is contained in:
parent
97e94e9f44
commit
e3914b814f
2
Gemfile
2
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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue