polymorpheus 3.0.0 → 3.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: d0a7b04ed03c563cb471078e8595129ea9e429a7
4
- data.tar.gz: 912c0b0dbf5690c3fdf0ae34b62acec60849203b
3
+ metadata.gz: b35be425d188561def6ede1eef462b1d3dee4835
4
+ data.tar.gz: 5f509e7b56f2966e2062b75640ac73d56a9e32c9
5
5
  SHA512:
6
- metadata.gz: cb4cb74d2cb1381f91478899fe4a5d4bd9d9b8a522cd6410ef2ddc144c1e3750e46f24006a082bd28b5a3907eae06469e45afc6e0ea75bc1013dd3edb15306a0
7
- data.tar.gz: 5d64aae54b033191a7b134c61a7d2bcb2072ba236f2d897021fcb5c71212202c833f02ee873fb38b394be91485b8087e9f40966d360979479020ee6634384d67
6
+ metadata.gz: a70e7b2ef8f96e16b57e295d1285743053bb1719f82d68983fb9a12ac69e3003c26f1c2aeb4cfb806893dceac1e548e86f5e1a740ba630d474788888d01dd0ce
7
+ data.tar.gz: d542e42e2cac59818dd0f3469032b95234e875667825d9e9b4557cd48f6becbab45c43ebf344b6d76d5d6611ef454d57d121ffbc2faa92937ef35bb9efea251c
data/README.md CHANGED
@@ -80,12 +80,14 @@ end
80
80
 
81
81
  ```ruby
82
82
  class Picture < ActiveRecord::Base
83
+ # takes same additional options as belongs_to
83
84
  belongs_to_polymorphic :employee, :product, :as => :imageable
84
85
  validates_polymorph :imageable
85
86
  end
86
87
 
87
88
  class Employee < ActiveRecord::Base
88
- has_many_as_polymorph :pictures
89
+ # takes same additional options as has_many
90
+ has_many_as_polymorph :pictures, inverse_of: employee
89
91
  end
90
92
 
91
93
  class Product < ActiveRecord::Base
@@ -2,9 +2,10 @@ module Polymorpheus
2
2
  module Interface
3
3
  module BelongsToPolymorphic
4
4
  def belongs_to_polymorphic(*association_names, options)
5
- polymorphic_api = options[:as]
5
+ polymorphic_api = options.delete(:as)
6
6
  builder = Polymorpheus::InterfaceBuilder.new(polymorphic_api,
7
- association_names)
7
+ association_names,
8
+ options)
8
9
 
9
10
  # The POLYMORPHEUS_ASSOCIATIONS constant is useful for two reasons:
10
11
  #
@@ -20,7 +21,7 @@ module Polymorpheus
20
21
 
21
22
  # Set belongs_to associations
22
23
  builder.associations.each do |association|
23
- belongs_to association.name.to_sym
24
+ belongs_to association.name.to_sym, association.options
24
25
  end
25
26
 
26
27
  # Exposed interface for introspection
@@ -6,10 +6,10 @@ module Polymorpheus
6
6
  attr_reader :interface_name,
7
7
  :associations
8
8
 
9
- def initialize(interface_name, association_names)
9
+ def initialize(interface_name, association_names, options)
10
10
  @interface_name = interface_name
11
11
  @associations = association_names.map do |association_name|
12
- Polymorpheus::InterfaceBuilder::Association.new(association_name)
12
+ Polymorpheus::InterfaceBuilder::Association.new(association_name, options)
13
13
  end
14
14
  end
15
15
 
@@ -5,10 +5,11 @@ module Polymorpheus
5
5
  include ActiveSupport::Inflector
6
6
 
7
7
  attr_reader :name,
8
- :key
8
+ :key,
9
+ :options
9
10
 
10
- def initialize(name)
11
- @name = name.to_s.downcase
11
+ def initialize(name, options)
12
+ @name, @options = name.to_s.downcase, options
12
13
  @key = "#{@name}_id"
13
14
  end
14
15
 
@@ -1,3 +1,3 @@
1
1
  module Polymorpheus
2
- VERSION = '3.0.0'
2
+ VERSION = '3.1.0'
3
3
  end
@@ -198,3 +198,18 @@ describe '.validates_polymorph' do
198
198
  ["You must specify exactly one of the following: {hero, villain}"]
199
199
  end
200
200
  end
201
+
202
+ describe 'association options' do
203
+ it 'without options' do
204
+ Drawing.new.association(:book).reflection.inverse_of.should == nil
205
+ Drawing.new.association(:binder).reflection.inverse_of.should == nil
206
+ Book.new.association(:drawings).reflection.inverse_of.should == nil
207
+ Binder.new.association(:drawings).reflection.inverse_of.should == nil
208
+ end
209
+ it 'with options' do
210
+ Picture.new.association(:web_page).reflection.inverse_of.name.should == :pictures
211
+ Picture.new.association(:printed_work).reflection.inverse_of.name.should == :pictures
212
+ WebPage.new.association(:pictures).reflection.inverse_of.name.should == :web_page
213
+ PrintedWork.new.association(:pictures).reflection.inverse_of.name.should == :printed_work
214
+ end
215
+ end
@@ -51,3 +51,23 @@ end
51
51
  # (Unless this is LOTR, but let's ignore that for now.)
52
52
  class Tree < ActiveRecord::Base
53
53
  end
54
+
55
+ class Drawing < ActiveRecord::Base
56
+ belongs_to_polymorphic :book, :binder, as: :one_way_rel
57
+ end
58
+ class Book < ActiveRecord::Base
59
+ has_many_as_polymorph :drawings
60
+ end
61
+ class Binder < ActiveRecord::Base
62
+ has_many_as_polymorph :drawings
63
+ end
64
+
65
+ class Picture < ActiveRecord::Base
66
+ belongs_to_polymorphic :web_page, :printed_work, as: :inverted_rel, inverse_of: :pictures
67
+ end
68
+ class WebPage < ActiveRecord::Base
69
+ has_many_as_polymorph :pictures, inverse_of: :web_page
70
+ end
71
+ class PrintedWork < ActiveRecord::Base
72
+ has_many_as_polymorph :pictures, inverse_of: :printed_work
73
+ end
@@ -15,6 +15,12 @@ ActiveRecord::Schema.define do
15
15
  create_table :alien_demigods
16
16
  create_table :supervillains
17
17
  create_table :trees
18
+ create_table :drawings
19
+ create_table :books
20
+ create_table :binders
21
+ create_table :pictures
22
+ create_table :web_pages
23
+ create_table :printed_works
18
24
 
19
25
  create_table :story_arcs do |t|
20
26
  t.integer :hero_id
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorpheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barun Singh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-17 00:00:00.000000000 Z
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord