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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 331305a38b4977b14717964024b46dee217d8d97
4
- data.tar.gz: 5877875beb1520d6d7d5bd432fe2a939676fef14
3
+ metadata.gz: 514a5965f8825f4c4bc1f2cb8155585c042f891c
4
+ data.tar.gz: 81513cdac4a29c051fd3be2240e0b9cc1a3f74b7
5
5
  SHA512:
6
- metadata.gz: 960ba568858dea22691683756c693a5000b2399b3909903729239ed68e968c8ab50d39f232c2193a247f96ef2bbfe4214ca51a35a91886177835469491d989b1
7
- data.tar.gz: c4635cb0ef46a62abbf6b66843c1c82c7eaf464d85adab78ea8f9f75576010c4a90585943872204ff02a0181496356f450d094b293518445977f4a9aeb0bf26c
6
+ metadata.gz: fa3a00abcba6a4fe27727232a9a3b142d5934343106417147d703fe2707a973793c642ff861fb7f0bcb3de54cbb668946b03cfaaa14286e0251f7b88f0430ae0
7
+ data.tar.gz: 0608465f2b7ffa2f0027df6bed6113253da3acafdc3c37e2ea590d564af6c045702e2374d6cce49c75e700886e2ab512bf7a49356344002db34deeb12183edff
@@ -1,3 +1,3 @@
1
1
  module ActsAs
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
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.acts_as_fields.select do |association, fields|
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
@@ -27,4 +27,9 @@ ActiveRecord::Migration.create_table :clans do |t|
27
27
  t.integer :strength, default: 50
28
28
  t.boolean :cool
29
29
  t.timestamps
30
+ end
31
+
32
+ ActiveRecord::Migration.create_table :x_wings do |t|
33
+ t.integer :rebel_id
34
+ t.timestamps
30
35
  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.2.2
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-06-24 00:00:00.000000000 Z
11
+ date: 2013-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler