acts_as 0.0.2 → 0.0.3

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: ff4949b83af1313de02dd223cfa667b8914d03c2
4
- data.tar.gz: 950527f10b751bcbeeda69c7816a51924a1de69a
3
+ metadata.gz: c054f6d73100efb1a76894d75b36424e1f302008
4
+ data.tar.gz: 8779e7ac0281fe3670eabc205ecfe9a449d305ae
5
5
  SHA512:
6
- metadata.gz: b024ee340ef12fa6bc5aaa3380de1993ba5fbc349fec88f2a93f39632a0c39ca7a030da2905f45eae69764d4269bca0db46901ceaf18881a4e9832bc2f7ece72
7
- data.tar.gz: 186fc903daca73350d57ab4a30ecfd2454a4a567153735a437f478033d892d0ed524b7f4fc62a9508a50bb746c4a340e3f1ada2068bfb5ccdad2d53ec497bfc4
6
+ metadata.gz: 0a8071e5d1ad40e9a3efbb8f4fba65a6e4d116930659d767027d39cce4ad6ccd05a2b2e5dab8cff73e68723bb55bef5c3425119ae90fd026f5a3382ab8ccb3f5
7
+ data.tar.gz: 13ac543c1bb90462c8d32b254458f99662fab738a115ded6c385a2bff51246ad11fb495b08450aace144322e9d1987e201ed0c1dc482c3d160eb9d85355cb2d5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActsAs
2
2
 
3
- ActiveRecord extension for easy 1:1 composition delegation
3
+ ActiveRecord extension for easy belongs_to composition delegation
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,18 +20,20 @@ Or install it yourself as:
20
20
 
21
21
  ```ruby
22
22
 
23
+ # This pattern encourages foreign keys to be stored on the STI's root table for easy reads.
24
+ #
23
25
  # table :users
24
26
  # name :string
27
+ # clan_id :integer
28
+ # profile_id :integer
25
29
  #
26
30
  class User
27
31
  include ActsAs
28
32
  end
29
33
 
30
34
  class Rebel < User
31
- has_one :profile, class_name: 'RebelProfile', autosave: true
32
- belongs_to :clan, autosave: true
33
- acts_as :profile
34
- acts_as :clan, prefix: %w( name ), whitelist: %w( delegate_at_will )
35
+ acts_as :profile, class_name: 'RebelProfile', autosave: true
36
+ acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will ), autosave: true
35
37
  end
36
38
 
37
39
  # table :clans
@@ -48,11 +50,10 @@ class Clan < ActiveRecord::Base
48
50
  end
49
51
 
50
52
  # table :rebel_profiles
51
- # rebel_id :integer
52
53
  # serial_data :string
53
54
  #
54
55
  class RebelProfile < ActiveRecord::Base
55
- belongs_to :rebel
56
+ has_one :rebel
56
57
  end
57
58
 
58
59
  ```
@@ -84,7 +85,7 @@ How does the active record join hash-parsing stuff work? EX-
84
85
  Rebel.joins(:clan).where(clan: {cool: true)
85
86
 
86
87
  Can we make this work for ruby-sql autojoins? Is that even a good idea?
87
- Rebel.where(cool: true)
88
+ Rebel.where(cool: true) #auto-joins :clan
88
89
 
89
90
  ## Contributing
90
91
 
@@ -1,3 +1,3 @@
1
1
  module ActsAs
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/acts_as.rb CHANGED
@@ -4,52 +4,54 @@ module ActsAs
4
4
  class ActsAs::ActiveRecordOnly < StandardError; end
5
5
 
6
6
  PREFIX = %w(id created_at updated_at)
7
- ACTING_FOR = {}
8
7
 
9
8
  def self.included(base)
9
+ raise ActiveRecordOnly unless base < ActiveRecord::Base
10
10
  base.extend ClassMethods
11
11
  end
12
12
 
13
13
  private
14
14
 
15
15
  def acts_as_field_match?(method)
16
- @association_match = ACTING_FOR.select do |association, fields|
16
+ @association_match = self.class.acts_as_fields.select do |association, fields|
17
17
  fields.select { |f| method.to_s.include?(f) }.any?
18
18
  end.keys.first
19
19
  @association_match && send(@association_match).respond_to?(method)
20
20
  end
21
21
 
22
22
  module ClassMethods
23
- def acts_as(one_association, prefix: [], whitelist: [])
24
- define_method(one_association) do |*args|
25
- super(*args) || send("build_#{one_association}", *args)
26
- end
23
+ def acts_as(association, type = :belongs_to, with: [], prefix: [], **options)
24
+ type == :belongs_to ? belongs_to(association, **options) : has_one(association, **options)
25
+ define_method(association) { |*args| super(*args) || send("build_#{association}", *args) }
27
26
 
27
+ if (association_class = new.send(association).class).table_exists?
28
+ whitelist_and_delegate_fields(association_class, association, prefix, with)
29
+ override_method_missing
30
+ end
31
+ end
28
32
 
29
- association_class = new.send(one_association).class
30
- if association_class.table_exists?
33
+ def acts_as_fields
34
+ @acts_as_fields ||= {}
35
+ end
31
36
 
32
- whitelist_and_delegate_fields(association_class, one_association, prefix, whitelist)
37
+ private
33
38
 
34
- define_method :method_missing do |method, *args, &block|
35
- if acts_as_field_match?(method) then
36
- send(@association_match).send(method, *args, &block)
37
- else
38
- super(method, *args, &block)
39
- end
39
+ def override_method_missing
40
+ define_method :method_missing do |method, *args, &block|
41
+ if acts_as_field_match?(method)
42
+ send(@association_match).send(method, *args, &block)
43
+ else
44
+ super(method, *args, &block)
40
45
  end
46
+ end
41
47
 
42
- define_method :respond_to? do |method, *args, &block|
43
- acts_as_field_match?(method) || super(method, *args, &block)
44
- end
48
+ define_method :respond_to? do |method, *args, &block|
49
+ acts_as_field_match?(method) || super(method, *args, &block)
45
50
  end
46
51
  end
47
52
 
48
-
49
- private
50
-
51
- def whitelist_and_delegate_fields(association_class, one_association, prefix, whitelist)
52
- association_fields = association_class.columns.map(&:name) - PREFIX - prefix + whitelist
53
+ def whitelist_and_delegate_fields(association_class, one_association, prefix, with)
54
+ association_fields = association_class.columns.map(&:name) - PREFIX - prefix + with
53
55
 
54
56
  build_prefix_methods(one_association, prefix)
55
57
 
@@ -59,7 +61,7 @@ module ActsAs
59
61
  delegate(*(association_fields + association_fields.map { |field| "#{field}=" }), to: one_association)
60
62
 
61
63
  #TODO: This feels like a weird place to remember delegated fields
62
- ACTING_FOR[one_association] = association_fields + prefix
64
+ acts_as_fields[one_association] = association_fields + prefix
63
65
  end
64
66
 
65
67
  def build_prefix_methods(one_association, prefix)
@@ -22,25 +22,26 @@ class Clan < ActiveRecord::Base
22
22
  end
23
23
 
24
24
  class Rebel < User
25
- has_one :profile, class_name: 'RebelProfile', autosave: true
26
- belongs_to :clan, autosave: true
27
- acts_as :profile
28
- acts_as :clan, prefix: %w( name ), whitelist: %w( delegate_at_will )
25
+ acts_as :profile, class_name: 'RebelProfile', autosave: true
26
+ acts_as :clan, prefix: %w( name ), with: %w( delegate_at_will ), autosave: true
29
27
  end
30
28
 
31
29
  class Imperial < User
32
- has_one :profile, class_name: 'ImperialProfile'
33
- acts_as :profile
30
+ acts_as :profile, class_name: 'ImperialProfile'
34
31
  end
35
32
 
36
-
37
-
38
33
  describe ActsAs do
39
34
 
40
35
  let(:rebel) { Rebel.create(name: "Leia", clan_name: "Organa") }
41
36
  subject { rebel }
42
37
 
43
- describe 'whitelist' do
38
+ it 'raises exception for non-ActiveRecord::Base extensions' do
39
+ expect {
40
+ class MyObject; include ActsAs; end
41
+ }.to raise_error(ActsAs::ActiveRecordOnly)
42
+ end
43
+
44
+ describe 'with' do
44
45
  it { should respond_to(:delegate_at_will) }
45
46
  its(:delegate_at_will) { should == rebel.clan.delegate_at_will }
46
47
  end
@@ -8,17 +8,16 @@ ActiveRecord::Migration.create_table :users do |t|
8
8
  t.string :name
9
9
  t.string :type
10
10
  t.integer :clan_id
11
+ t.integer :profile_id
11
12
  t.timestamps
12
13
  end
13
14
 
14
15
  ActiveRecord::Migration.create_table :rebel_profiles do |t|
15
- t.integer :rebel_id
16
16
  t.string :serial_data
17
17
  t.timestamps
18
18
  end
19
19
 
20
20
  ActiveRecord::Migration.create_table :imperial_profiles do |t|
21
- t.integer :imperial_id
22
21
  t.string :analog_data
23
22
  t.timestamps
24
23
  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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - winfred
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-28 00:00:00.000000000 Z
11
+ date: 2013-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,7 @@ files:
53
53
  - acts_as.gemspec
54
54
  - lib/acts_as.rb
55
55
  - lib/acts_as/version.rb
56
- - spec/acts_for_spec.rb
56
+ - spec/acts_as_spec.rb
57
57
  - spec/spec_helper.rb
58
58
  - spec/support/active_record.rb
59
59
  homepage: http://github.com/wnadeau/acts_as
@@ -81,6 +81,6 @@ signing_key:
81
81
  specification_version: 4
82
82
  summary: delegate an entire 1:1 association worth of active record field-related helpers
83
83
  test_files:
84
- - spec/acts_for_spec.rb
84
+ - spec/acts_as_spec.rb
85
85
  - spec/spec_helper.rb
86
86
  - spec/support/active_record.rb