acts_as 0.0.3 → 0.1.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/README.md +12 -4
- data/lib/acts_as/version.rb +1 -1
- data/lib/acts_as.rb +3 -4
- data/spec/acts_as_spec.rb +8 -4
- 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: 10b787576f55530c920e1d5fd6e448dac4c469d8
|
4
|
+
data.tar.gz: bc8a0ff7ed25ca4c478013dca369d1d86e895784
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7b75591316fa01a115948ceb8d611be36e6a8e9ab725e8d1bfafdd91ab70085e4bfc98badd7ebda34242180e13492ba8b52c325c0ffe3b093052d93294cbe6a
|
7
|
+
data.tar.gz: f8c35caa20e70ee99ca90483ce1f2d03714c4e83bbbd79ee6b12381d3f08d1374b5d669e5b976031fae6e1fa9d6b5318567b4c8ba74998de29cfbb06849f7b49
|
data/README.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# ActsAs
|
2
2
|
|
3
|
-
ActiveRecord extension for easy belongs_to composition delegation
|
3
|
+
ActiveRecord extension for easy belongs_to composition and delegation
|
4
|
+
|
5
|
+
* Transparently write to multiple tables from one active record model by proxying attributes and their methods through a second model.
|
6
|
+
* Easily extract a new table from an existing table, but keep the existing API intact without breaking any consumers.
|
7
|
+
* When using STI to maintain easy joins, this is an easy way to proxy unique child attributes through to another table for that child
|
8
|
+
|
9
|
+
This is designed for any belongs_to relationship where lifecycles are tightly coupled and proxying of attribute helpers from belongs_to -> has_one is desired.
|
10
|
+
|
11
|
+
(see example below and /spec for more detail)
|
4
12
|
|
5
13
|
## Installation
|
6
14
|
|
@@ -32,8 +40,8 @@ class User
|
|
32
40
|
end
|
33
41
|
|
34
42
|
class Rebel < User
|
35
|
-
acts_as :profile, class_name: 'RebelProfile'
|
36
|
-
acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will )
|
43
|
+
acts_as :profile, class_name: 'RebelProfile'
|
44
|
+
acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will )
|
37
45
|
end
|
38
46
|
|
39
47
|
# table :clans
|
@@ -78,7 +86,7 @@ Now a whole slew of methods related to ActiveRecord attributes are available for
|
|
78
86
|
rebel.delegate_at_will #=> '10'
|
79
87
|
|
80
88
|
|
81
|
-
##
|
89
|
+
## Roadmap / Ideas
|
82
90
|
|
83
91
|
How does the active record join hash-parsing stuff work? EX-
|
84
92
|
|
data/lib/acts_as/version.rb
CHANGED
data/lib/acts_as.rb
CHANGED
@@ -20,8 +20,8 @@ module ActsAs
|
|
20
20
|
end
|
21
21
|
|
22
22
|
module ClassMethods
|
23
|
-
def acts_as(association,
|
24
|
-
|
23
|
+
def acts_as(association, with: [], prefix: [], **options)
|
24
|
+
belongs_to(association, **options.merge(autosave: true))
|
25
25
|
define_method(association) { |*args| super(*args) || send("build_#{association}", *args) }
|
26
26
|
|
27
27
|
if (association_class = new.send(association).class).table_exists?
|
@@ -60,7 +60,6 @@ module ActsAs
|
|
60
60
|
|
61
61
|
delegate(*(association_fields + association_fields.map { |field| "#{field}=" }), to: one_association)
|
62
62
|
|
63
|
-
#TODO: This feels like a weird place to remember delegated fields
|
64
63
|
acts_as_fields[one_association] = association_fields + prefix
|
65
64
|
end
|
66
65
|
|
@@ -76,4 +75,4 @@ module ActsAs
|
|
76
75
|
end
|
77
76
|
end
|
78
77
|
end
|
79
|
-
end
|
78
|
+
end
|
data/spec/acts_as_spec.rb
CHANGED
@@ -6,11 +6,11 @@ class User < ActiveRecord::Base
|
|
6
6
|
end
|
7
7
|
|
8
8
|
class RebelProfile < ActiveRecord::Base
|
9
|
-
|
9
|
+
has_one :rebel
|
10
10
|
end
|
11
11
|
|
12
12
|
class ImperialProfile < ActiveRecord::Base
|
13
|
-
|
13
|
+
has_one :imperial
|
14
14
|
end
|
15
15
|
|
16
16
|
class Clan < ActiveRecord::Base
|
@@ -22,8 +22,8 @@ class Clan < ActiveRecord::Base
|
|
22
22
|
end
|
23
23
|
|
24
24
|
class Rebel < User
|
25
|
-
acts_as :profile, class_name: 'RebelProfile'
|
26
|
-
acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will )
|
25
|
+
acts_as :profile, class_name: 'RebelProfile'
|
26
|
+
acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will )
|
27
27
|
end
|
28
28
|
|
29
29
|
class Imperial < User
|
@@ -44,6 +44,10 @@ describe ActsAs do
|
|
44
44
|
describe 'with' do
|
45
45
|
it { should respond_to(:delegate_at_will) }
|
46
46
|
its(:delegate_at_will) { should == rebel.clan.delegate_at_will }
|
47
|
+
it 'actually calls the method on the associated object' do
|
48
|
+
rebel.clan.should_receive(:delegate_at_will).once
|
49
|
+
rebel.delegate_at_will
|
50
|
+
end
|
47
51
|
end
|
48
52
|
|
49
53
|
describe 'proxied getters and setters' do
|
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.0
|
4
|
+
version: 0.1.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-05-
|
11
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|