acts_as 0.2.2 → 0.3.0
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.
- checksums.yaml +4 -4
- data/lib/acts_as/version.rb +1 -1
- data/lib/acts_as.rb +32 -5
- data/spec/acts_as_spec.rb +17 -0
- data/spec/support/active_record.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 514a5965f8825f4c4bc1f2cb8155585c042f891c
|
4
|
+
data.tar.gz: 81513cdac4a29c051fd3be2240e0b9cc1a3f74b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa3a00abcba6a4fe27727232a9a3b142d5934343106417147d703fe2707a973793c642ff861fb7f0bcb3de54cbb668946b03cfaaa14286e0251f7b88f0430ae0
|
7
|
+
data.tar.gz: 0608465f2b7ffa2f0027df6bed6113253da3acafdc3c37e2ea590d564af6c045702e2374d6cce49c75e700886e2ab512bf7a49356344002db34deeb12183edff
|
data/lib/acts_as/version.rb
CHANGED
data/lib/acts_as.rb
CHANGED
@@ -25,12 +25,8 @@ module ActsAs
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
private
|
29
|
-
|
30
28
|
def acts_as_field_match?(method)
|
31
|
-
@association_match = self.class.
|
32
|
-
fields.select { |f| method.to_s.include?(f) }.any?
|
33
|
-
end.keys.first
|
29
|
+
@association_match = self.class.acts_as_fields_match(method)
|
34
30
|
@association_match && send(@association_match).respond_to?(method)
|
35
31
|
end
|
36
32
|
|
@@ -49,6 +45,37 @@ module ActsAs
|
|
49
45
|
@acts_as_fields ||= {}
|
50
46
|
end
|
51
47
|
|
48
|
+
def acts_as_fields_match(method)
|
49
|
+
acts_as_fields.select do |association, fields|
|
50
|
+
fields.select { |f| method.to_s.include?(f) }.any?
|
51
|
+
end.keys.first
|
52
|
+
end
|
53
|
+
|
54
|
+
def where(opts, *rest)
|
55
|
+
return self if opts.blank?
|
56
|
+
relation = super
|
57
|
+
#TODO support nested attribute joins like Guns.where(rebels: {strength: 10}))
|
58
|
+
# for now, only first level joins will happen automagically
|
59
|
+
detected_associations = opts.keys.map {|attr| acts_as_fields_match(attr) }
|
60
|
+
.reject {|attr| attr.nil?}
|
61
|
+
return relation.joins(detected_associations) if detected_associations.any?
|
62
|
+
relation
|
63
|
+
end
|
64
|
+
|
65
|
+
def expand_hash_conditions_for_aggregates(attrs)
|
66
|
+
attrs = super(attrs)
|
67
|
+
expanded_attrs = {}
|
68
|
+
|
69
|
+
attrs.each do |attr, value|
|
70
|
+
if (association = acts_as_fields_match(attr))
|
71
|
+
expanded_attrs[new.send(association).class.table_name] = { attr => value }
|
72
|
+
else
|
73
|
+
expanded_attrs[attr] = value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
expanded_attrs
|
77
|
+
end
|
78
|
+
|
52
79
|
private
|
53
80
|
|
54
81
|
def override_method_missing
|
data/spec/acts_as_spec.rb
CHANGED
@@ -21,6 +21,10 @@ class Clan < ActiveRecord::Base
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
class XWing < ActiveRecord::Base
|
25
|
+
belongs_to :rebel
|
26
|
+
end
|
27
|
+
|
24
28
|
class Rebel < User
|
25
29
|
acts_as :profile, class_name: 'RebelProfile'
|
26
30
|
acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will )
|
@@ -98,4 +102,17 @@ describe ActsAs do
|
|
98
102
|
rebel.strength.should == 23
|
99
103
|
end
|
100
104
|
end
|
105
|
+
|
106
|
+
describe 'automagic .where hash syntax helpers' do
|
107
|
+
it 'should auto-expand acted attributes' do
|
108
|
+
Rebel.where(strength: rebel.strength).should include(rebel)
|
109
|
+
Rebel.where(strength: rebel.strength, name: 'Jimbo').should_not include(rebel)
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should auto-expand acted attribuets that are nested as well' do
|
113
|
+
pending 'support nested attributes'
|
114
|
+
xwing = XWing.create!(rebel: rebel)
|
115
|
+
XWing.joins(rebel: :clan).where(rebel: {strength: rebel.strength}).should include(xwing)
|
116
|
+
end
|
117
|
+
end
|
101
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winfred
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|