acts_as 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c054f6d73100efb1a76894d75b36424e1f302008
4
- data.tar.gz: 8779e7ac0281fe3670eabc205ecfe9a449d305ae
3
+ metadata.gz: 10b787576f55530c920e1d5fd6e448dac4c469d8
4
+ data.tar.gz: bc8a0ff7ed25ca4c478013dca369d1d86e895784
5
5
  SHA512:
6
- metadata.gz: 0a8071e5d1ad40e9a3efbb8f4fba65a6e4d116930659d767027d39cce4ad6ccd05a2b2e5dab8cff73e68723bb55bef5c3425119ae90fd026f5a3382ab8ccb3f5
7
- data.tar.gz: 13ac543c1bb90462c8d32b254458f99662fab738a115ded6c385a2bff51246ad11fb495b08450aace144322e9d1987e201ed0c1dc482c3d160eb9d85355cb2d5
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', autosave: true
36
- acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will ), autosave: true
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
- ## To be considered
89
+ ## Roadmap / Ideas
82
90
 
83
91
  How does the active record join hash-parsing stuff work? EX-
84
92
 
@@ -1,3 +1,3 @@
1
1
  module ActsAs
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
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, type = :belongs_to, with: [], prefix: [], **options)
24
- type == :belongs_to ? belongs_to(association, **options) : has_one(association, **options)
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
- belongs_to :rebel
9
+ has_one :rebel
10
10
  end
11
11
 
12
12
  class ImperialProfile < ActiveRecord::Base
13
- belongs_to :imperial
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', autosave: true
26
- acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will ), autosave: true
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.3
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-01 00:00:00.000000000 Z
11
+ date: 2013-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler