ransack 0.5.6 → 0.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ransack/adapters/active_record.rb +0 -8
- data/lib/ransack/adapters/active_record/3.0/context.rb +2 -2
- data/lib/ransack/adapters/active_record/context.rb +2 -2
- data/lib/ransack/context.rb +8 -7
- data/lib/ransack/search.rb +1 -1
- data/lib/ransack/version.rb +1 -1
- data/ransack.gemspec +1 -1
- data/spec/support/schema.rb +1 -1
- metadata +19 -23
- data/lib/ransack/adapters/active_record/3.0/join_association.rb +0 -44
- data/lib/ransack/adapters/active_record/3.0/join_dependency.rb +0 -60
- data/lib/ransack/adapters/active_record/join_association.rb +0 -44
- data/lib/ransack/adapters/active_record/join_dependency.rb +0 -60
@@ -3,15 +3,7 @@ ActiveRecord::Base.extend Ransack::Adapters::ActiveRecord::Base
|
|
3
3
|
|
4
4
|
case ActiveRecord::VERSION::STRING
|
5
5
|
when /^3\.0\./
|
6
|
-
require 'ransack/adapters/active_record/3.0/join_dependency'
|
7
|
-
require 'ransack/adapters/active_record/3.0/join_association'
|
8
6
|
require 'ransack/adapters/active_record/3.0/context'
|
9
|
-
|
10
|
-
ActiveRecord::Associations::ClassMethods::JoinDependency.send :include, Ransack::Adapters::ActiveRecord::JoinDependency
|
11
7
|
else
|
12
|
-
require 'ransack/adapters/active_record/join_dependency'
|
13
|
-
require 'ransack/adapters/active_record/join_association'
|
14
8
|
require 'ransack/adapters/active_record/context'
|
15
|
-
|
16
|
-
ActiveRecord::Associations::JoinDependency.send :include, Ransack::Adapters::ActiveRecord::JoinDependency
|
17
9
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'ransack/context'
|
2
|
-
require '
|
2
|
+
require 'polyamorous'
|
3
3
|
require 'ransack/adapters/active_record/3.0/compat'
|
4
4
|
|
5
5
|
module Ransack
|
@@ -138,7 +138,7 @@ module Ransack
|
|
138
138
|
(!klass || assoc.reflection.klass == klass)
|
139
139
|
end
|
140
140
|
unless found_association
|
141
|
-
@join_dependency.send(:
|
141
|
+
@join_dependency.send(:build, Polyamorous::Join.new(name, @join_type, klass), parent)
|
142
142
|
found_association = @join_dependency.join_associations.last
|
143
143
|
# Leverage the stashed association functionality in AR
|
144
144
|
@object = @object.joins(found_association)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'ransack/context'
|
2
|
-
require '
|
2
|
+
require 'polyamorous'
|
3
3
|
|
4
4
|
module Ransack
|
5
5
|
module Adapters
|
@@ -143,7 +143,7 @@ module Ransack
|
|
143
143
|
(!klass || assoc.reflection.klass == klass)
|
144
144
|
end
|
145
145
|
unless found_association
|
146
|
-
@join_dependency.send(:
|
146
|
+
@join_dependency.send(:build, Polyamorous::Join.new(name, @join_type, klass), parent)
|
147
147
|
found_association = @join_dependency.join_associations.last
|
148
148
|
# Leverage the stashed association functionality in AR
|
149
149
|
@object = @object.joins(found_association)
|
data/lib/ransack/context.rb
CHANGED
@@ -7,30 +7,31 @@ module Ransack
|
|
7
7
|
|
8
8
|
class << self
|
9
9
|
|
10
|
-
def for(object)
|
11
|
-
context = Class === object ? for_class(object) : for_object(object)
|
10
|
+
def for(object, options = {})
|
11
|
+
context = Class === object ? for_class(object, options) : for_object(object, options)
|
12
12
|
context or raise ArgumentError, "Don't know what context to use for #{object}"
|
13
13
|
end
|
14
14
|
|
15
|
-
def for_class(klass)
|
15
|
+
def for_class(klass, options = {})
|
16
16
|
if klass < ActiveRecord::Base
|
17
|
-
Adapters::ActiveRecord::Context.new(klass)
|
17
|
+
Adapters::ActiveRecord::Context.new(klass, options)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def for_object(object)
|
21
|
+
def for_object(object, options = {})
|
22
22
|
case object
|
23
23
|
when ActiveRecord::Relation
|
24
|
-
Adapters::ActiveRecord::Context.new(object.klass)
|
24
|
+
Adapters::ActiveRecord::Context.new(object.klass, options)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
end
|
29
29
|
|
30
|
-
def initialize(object)
|
30
|
+
def initialize(object, options = {})
|
31
31
|
@object = object.scoped
|
32
32
|
@klass = @object.klass
|
33
33
|
@join_dependency = join_dependency(@object)
|
34
|
+
@join_type = options[:join_type] || Arel::OuterJoin
|
34
35
|
@base = @join_dependency.join_base
|
35
36
|
@engine = @base.arel_engine
|
36
37
|
@arel_visitor = Arel::Visitors.visitor_for @engine
|
data/lib/ransack/search.rb
CHANGED
@@ -15,7 +15,7 @@ module Ransack
|
|
15
15
|
|
16
16
|
def initialize(object, params = {}, options = {})
|
17
17
|
params ||= {}
|
18
|
-
@context = Context.for(object)
|
18
|
+
@context = Context.for(object, options)
|
19
19
|
@context.auth_object = options[:auth_object]
|
20
20
|
@base = Nodes::Grouping.new(@context, 'and')
|
21
21
|
build(params.with_indifferent_access)
|
data/lib/ransack/version.rb
CHANGED
data/ransack.gemspec
CHANGED
@@ -15,8 +15,8 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rubyforge_project = "ransack"
|
16
16
|
|
17
17
|
s.add_dependency 'activerecord', '~> 3.0'
|
18
|
-
s.add_dependency 'activesupport', '~> 3.0'
|
19
18
|
s.add_dependency 'actionpack', '~> 3.0'
|
19
|
+
s.add_dependency 'polyamorous', '~> 0.5.0'
|
20
20
|
s.add_development_dependency 'rspec', '~> 2.6.0'
|
21
21
|
s.add_development_dependency 'machinist', '~> 1.0.6'
|
22
22
|
s.add_development_dependency 'faker', '~> 0.9.5'
|
data/spec/support/schema.rb
CHANGED
@@ -11,7 +11,7 @@ class Person < ActiveRecord::Base
|
|
11
11
|
has_many :articles
|
12
12
|
has_many :comments
|
13
13
|
has_many :authored_article_comments, :through => :articles,
|
14
|
-
:
|
14
|
+
:source => :comments, :foreign_key => :person_id
|
15
15
|
has_many :notes, :as => :notable
|
16
16
|
|
17
17
|
ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ransack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-03 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70314605054500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70314605054500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: actionpack
|
27
|
+
requirement: &70314605053560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,21 +32,21 @@ dependencies:
|
|
32
32
|
version: '3.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70314605053560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
37
|
+
name: polyamorous
|
38
|
+
requirement: &70314605053020 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
43
|
+
version: 0.5.0
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70314605053020
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70314605052340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 2.6.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70314605052340
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: machinist
|
60
|
-
requirement: &
|
60
|
+
requirement: &70314605051080 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.0.6
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70314605051080
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: faker
|
71
|
-
requirement: &
|
71
|
+
requirement: &70314605050120 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.9.5
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70314605050120
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: sqlite3
|
82
|
-
requirement: &
|
82
|
+
requirement: &70314605049340 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: 1.3.3
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70314605049340
|
91
91
|
description: Ransack is the successor to the MetaSearch gem. It improves and expands
|
92
92
|
upon MetaSearch's functionality, but does not have a 100%-compatible API.
|
93
93
|
email:
|
@@ -106,12 +106,8 @@ files:
|
|
106
106
|
- lib/ransack/adapters/active_record.rb
|
107
107
|
- lib/ransack/adapters/active_record/3.0/compat.rb
|
108
108
|
- lib/ransack/adapters/active_record/3.0/context.rb
|
109
|
-
- lib/ransack/adapters/active_record/3.0/join_association.rb
|
110
|
-
- lib/ransack/adapters/active_record/3.0/join_dependency.rb
|
111
109
|
- lib/ransack/adapters/active_record/base.rb
|
112
110
|
- lib/ransack/adapters/active_record/context.rb
|
113
|
-
- lib/ransack/adapters/active_record/join_association.rb
|
114
|
-
- lib/ransack/adapters/active_record/join_dependency.rb
|
115
111
|
- lib/ransack/configuration.rb
|
116
112
|
- lib/ransack/constants.rb
|
117
113
|
- lib/ransack/context.rb
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module ActiveRecord
|
6
|
-
class JoinAssociation < ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
|
7
|
-
|
8
|
-
def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
|
9
|
-
if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
|
10
|
-
swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
|
11
|
-
super(reflection, join_dependency, parent)
|
12
|
-
end
|
13
|
-
else
|
14
|
-
super(reflection, join_dependency, parent)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def swapping_reflection_klass(reflection, klass)
|
19
|
-
reflection = reflection.clone
|
20
|
-
original_polymorphic = reflection.options.delete(:polymorphic)
|
21
|
-
reflection.instance_variable_set(:@klass, klass)
|
22
|
-
yield reflection
|
23
|
-
ensure
|
24
|
-
reflection.options[:polymorphic] = original_polymorphic
|
25
|
-
end
|
26
|
-
|
27
|
-
def ==(other)
|
28
|
-
super && active_record == other.active_record
|
29
|
-
end
|
30
|
-
|
31
|
-
def build_constraint(reflection, table, key, foreign_table, foreign_key)
|
32
|
-
if reflection.options[:polymorphic]
|
33
|
-
super.and(
|
34
|
-
foreign_table[reflection.foreign_type].eq(reflection.klass.name)
|
35
|
-
)
|
36
|
-
else
|
37
|
-
super
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module ActiveRecord
|
6
|
-
module JoinDependency
|
7
|
-
|
8
|
-
def self.included(base)
|
9
|
-
base.class_eval do
|
10
|
-
# Squeel's graft is more capable than Ransack's.
|
11
|
-
alias_method_chain :graft, :ransack unless defined?(Squeel)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def graft_with_ransack(*associations)
|
16
|
-
associations.each do |association|
|
17
|
-
join_associations.detect {|a| association == a} ||
|
18
|
-
build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass)
|
19
|
-
end
|
20
|
-
self
|
21
|
-
end
|
22
|
-
|
23
|
-
# Should only be called by Ransack, and only with a single association name
|
24
|
-
def build_polymorphic(association, parent = nil, join_type = Arel::OuterJoin, klass = nil)
|
25
|
-
parent ||= joins.last
|
26
|
-
reflection = parent.reflections[association] or
|
27
|
-
raise ::ActiveRecord::ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?"
|
28
|
-
unless join_association = find_join_association_respecting_polymorphism(reflection, parent, klass)
|
29
|
-
@reflections << reflection
|
30
|
-
join_association = build_join_association_respecting_polymorphism(reflection, parent, klass)
|
31
|
-
join_association.join_type = join_type
|
32
|
-
@joins << join_association
|
33
|
-
cache_joined_association(join_association)
|
34
|
-
end
|
35
|
-
|
36
|
-
join_association
|
37
|
-
end
|
38
|
-
|
39
|
-
def find_join_association_respecting_polymorphism(reflection, parent, klass)
|
40
|
-
if association = find_join_association(reflection, parent)
|
41
|
-
unless reflection.options[:polymorphic]
|
42
|
-
association
|
43
|
-
else
|
44
|
-
association if association.active_record == klass
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def build_join_association_respecting_polymorphism(reflection, parent, klass = nil)
|
50
|
-
if reflection.options[:polymorphic] && klass
|
51
|
-
JoinAssociation.new(reflection, self, parent, klass)
|
52
|
-
else
|
53
|
-
JoinAssociation.new(reflection, self, parent)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module ActiveRecord
|
6
|
-
class JoinAssociation < ::ActiveRecord::Associations::JoinDependency::JoinAssociation
|
7
|
-
|
8
|
-
def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
|
9
|
-
if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
|
10
|
-
swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
|
11
|
-
super(reflection, join_dependency, parent)
|
12
|
-
end
|
13
|
-
else
|
14
|
-
super(reflection, join_dependency, parent)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def swapping_reflection_klass(reflection, klass)
|
19
|
-
reflection = reflection.clone
|
20
|
-
original_polymorphic = reflection.options.delete(:polymorphic)
|
21
|
-
reflection.instance_variable_set(:@klass, klass)
|
22
|
-
yield reflection
|
23
|
-
ensure
|
24
|
-
reflection.options[:polymorphic] = original_polymorphic
|
25
|
-
end
|
26
|
-
|
27
|
-
def ==(other)
|
28
|
-
super && active_record == other.active_record
|
29
|
-
end
|
30
|
-
|
31
|
-
def build_constraint(reflection, table, key, foreign_table, foreign_key)
|
32
|
-
if reflection.options[:polymorphic]
|
33
|
-
super.and(
|
34
|
-
foreign_table[reflection.foreign_type].eq(reflection.klass.name)
|
35
|
-
)
|
36
|
-
else
|
37
|
-
super
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module ActiveRecord
|
6
|
-
module JoinDependency
|
7
|
-
|
8
|
-
def self.included(base)
|
9
|
-
base.class_eval do
|
10
|
-
# Squeel's graft is more capable than Ransack's.
|
11
|
-
alias_method_chain :graft, :ransack unless defined?(Squeel)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def graft_with_ransack(*associations)
|
16
|
-
associations.each do |association|
|
17
|
-
join_associations.detect {|a| association == a} ||
|
18
|
-
build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass)
|
19
|
-
end
|
20
|
-
self
|
21
|
-
end
|
22
|
-
|
23
|
-
# Should only be called by Ransack, and only with a single association name
|
24
|
-
def build_polymorphic(association, parent = nil, join_type = Arel::OuterJoin, klass = nil)
|
25
|
-
parent ||= join_parts.last
|
26
|
-
reflection = parent.reflections[association] or
|
27
|
-
raise ::ActiveRecord::ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?"
|
28
|
-
unless join_association = find_join_association_respecting_polymorphism(reflection, parent, klass)
|
29
|
-
@reflections << reflection
|
30
|
-
join_association = build_join_association_respecting_polymorphism(reflection, parent, klass)
|
31
|
-
join_association.join_type = join_type
|
32
|
-
@join_parts << join_association
|
33
|
-
cache_joined_association(join_association)
|
34
|
-
end
|
35
|
-
|
36
|
-
join_association
|
37
|
-
end
|
38
|
-
|
39
|
-
def find_join_association_respecting_polymorphism(reflection, parent, klass)
|
40
|
-
if association = find_join_association(reflection, parent)
|
41
|
-
unless reflection.options[:polymorphic]
|
42
|
-
association
|
43
|
-
else
|
44
|
-
association if association.active_record == klass
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def build_join_association_respecting_polymorphism(reflection, parent, klass = nil)
|
50
|
-
if reflection.options[:polymorphic] && klass
|
51
|
-
JoinAssociation.new(reflection, self, parent, klass)
|
52
|
-
else
|
53
|
-
JoinAssociation.new(reflection, self, parent)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|