sequel_polymorphic 0.1.0 → 0.1.1

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: 0ba44333d65bad98de9c9645057d24ddc8513242
4
- data.tar.gz: d44a526e09d8cca923ff8a3e61fe8bd6f9baca1d
3
+ metadata.gz: 6bdda82c3c9fc48e48600a93dc0e0983383a91a8
4
+ data.tar.gz: dca9174fb3c7ced750dc7408d75d4c121051dec0
5
5
  SHA512:
6
- metadata.gz: ccda128f94078c89326d8f98af390f7382d0f79a4927d6836dbca2e35111e77b056c9ebfafeb2e11027488824728f4f9c8e9904c6591ea3832d553eb2f951c08
7
- data.tar.gz: 62157d1f0941c1762aa686ef784439b6f79be198f15839067a6e522e569fe15559bd605efed8f1a736a29c22d16b6a298ca8711efbb67a018396a5fd4f5dc757
6
+ metadata.gz: 9ce3965debd56f264bea496df1804f7af002db6cf9bfa48c0cc68806f999df062b468af24e66bd4f8d618bf24f5e7de994a7dcc79604c12b2008c10f8247cb6b
7
+ data.tar.gz: 4ebafbc03a3ef7a59dc5efb1a89966a22968c26bf01f5a22cbc08c2bdd2797e5fc080cd22917fba981d08dd5e8215dc88de5099c7f3d5d8721438c1f5929c43d
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 Jack Dempsey
1
+ Copyright (c) 2008-2015 Jack Dempsey, Dave Myron, Alexander Kurakin
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,122 +1,47 @@
1
- #Sequel Polymorphic
1
+ # Sequel Polymorphic
2
2
 
3
- A simple plugin for Sequel::Model's that lets you easily create polymorphic associations.
3
+ A simple plugin for [Sequel](http://sequel.jeremyevans.net) that lets you easily create polymorphic associations.
4
4
 
5
- ##ActiveRecord Style
5
+ Version 4.x is required.
6
6
 
7
- ```ruby
8
- class Asset < ActiveRecord::Base
9
- belongs_to :attachable, :polymorphic => true
10
- end
11
-
12
- class Post < ActiveRecord::Base
13
- has_many :assets, :as => :attachable
14
- end
7
+ ## Usage examples
15
8
 
16
- class Note < ActiveRecord::Base
17
- has_many :assets, :as => :attachable
18
- end
19
-
20
- @asset.attachable = @post
21
- @asset.attachable = @note
22
- ```
23
-
24
- ## Sequel (without plugin)
25
-
26
- In Sequel you would do the following:
9
+ ### Models
27
10
 
28
11
  ```ruby
29
- class Asset < Sequel::Model
30
- many_to_one :attachable, :reciprocal=>:assets, \
31
- :dataset=>(proc do
32
- klass = attachable_type.constantize
33
- klass.filter(klass.primary_key=>attachable_id)
34
- end), \
35
- :eager_loader=>(proc do |key_hash, assets, associations|
36
- id_map = {}
37
- assets.each do |asset|
38
- asset.associations[:attachable] = nil
39
- ((id_map[asset.attachable_type] ||= {})[asset.attachable_id] ||= []) << asset
40
- end
41
- id_map.each do |klass_name, id_map|
42
- klass = klass_name.constantize
43
- klass.filter(klass.primary_key=>id_map.keys).all do |attach|
44
- id_map[attach.pk].each do |asset|
45
- asset.associations[:attachable] = attach
46
- end
47
- end
48
- end
49
- end)
50
-
51
- private
12
+ Sequel::Model.plugin(:polymorphic)
52
13
 
53
- def _attachable=(attachable)
54
- self[:attachable_id] = (attachable.pk if attachable)
55
- self[:attachable_type] = (attachable.class.name if attachable)
56
- end
57
- end
58
-
59
- class Post < Sequel::Model
60
- one_to_many :assets, :key=>:attachable_id do |ds|
61
- ds.filter(:attachable_type=>'Post')
62
- end
63
-
64
- private
65
-
66
- def _add_asset(asset)
67
- asset.attachable_id = pk
68
- asset.attachable_type = 'Post'
69
- asset.save
70
- end
71
- def _remove_asset(asset)
72
- asset.attachable_id = nil
73
- asset.attachable_type = nil
74
- asset.save
75
- end
76
- def _remove_all_assets
77
- Asset.filter(:attachable_id=>pk, :attachable_type=>'Post')\
78
- .update(:attachable_id=>nil, :attachable_type=>nil)
79
- end
14
+ class Asset < Sequel::Model
15
+ many_to_one :attachable, :polymorphic => true
80
16
  end
81
17
 
82
18
  class Note < Sequel::Model
83
- one_to_many :assets, :key=>:attachable_id do |ds|
84
- ds.filter(:attachable_type=>'Note')
85
- end
86
-
87
- private
88
-
89
- def _add_asset(asset)
90
- asset.attachable_id = pk
91
- asset.attachable_type = 'Note'
92
- asset.save
93
- end
94
- def _remove_asset(asset)
95
- asset.attachable_id = nil
96
- asset.attachable_type = nil
97
- asset.save
98
- end
99
- def _remove_all_assets
100
- Asset.filter(:attachable_id=>pk, :attachable_type=>'Note')\
101
- .update(:attachable_id=>nil, :attachable_type=>nil)
102
- end
19
+ one_to_many :assets, :as => :attachable
103
20
  end
104
21
 
105
- @asset.attachable = @post
106
- @asset.attachable = @note
22
+ class Post < Sequel::Model
23
+ one_to_many :assets, :as => :attachable
24
+ end
107
25
  ```
108
26
 
109
- Thats quite a bit of code. With sequel_polymorphic you can now do:
27
+ ### Schema
110
28
 
111
- ## Polymorphic
29
+ Include the polymorphic columns in your DB schema:
112
30
 
113
31
  ```ruby
114
- class Note < Sequel::Model
115
- one_to_many :assets, :as => :attachable
32
+ Sequel.migration do
33
+ change do
34
+ create_table :assets do
35
+ # ...
36
+ Integer :attachable_id
37
+ String :attachable_type
38
+ # ...
39
+ index [:attachable_id, :attachable_type]
40
+ end
41
+ end
116
42
  end
43
+ ```
117
44
 
118
- class Asset < Sequel::Model
119
- many_to_one :attachable, :polymorphic => true
120
- end
45
+ ### More usage examples
121
46
 
122
- ```
47
+ See [specs](https://github.com/jackdempsey/sequel_polymorphic/tree/master/spec).
@@ -22,6 +22,7 @@ module Sequel
22
22
 
23
23
  associate(:many_to_one, able,
24
24
  :reciprocal => plural_model.to_sym,
25
+ :reciprocal_type => :many_to_one,
25
26
  :setter => (proc do |able_instance|
26
27
  self[:"#{able}_id"] = (able_instance.pk if able_instance)
27
28
  self[:"#{able}_type"] = (able_instance.class.name if able_instance)
@@ -69,6 +70,7 @@ module Sequel
69
70
  associate(:one_to_many, collection_name,
70
71
  :key => able_id,
71
72
  :reciprocal => able,
73
+ :reciprocal_type => :one_to_many,
72
74
  :conditions => {able_type => self.to_s},
73
75
  :adder => proc { |many_of_instance| many_of_instance.update(able_id => pk, able_type => self.class.to_s) },
74
76
  :remover => proc { |many_of_instance| many_of_instance.update(able_id => nil, able_type => nil) },
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_polymorphic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Dempsey
8
8
  - Dave Myron
9
+ - Alexander Kurakin
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2014-09-09 00:00:00.000000000 Z
13
+ date: 2015-06-09 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: sequel
@@ -25,18 +26,47 @@ dependencies:
25
26
  - - "~>"
26
27
  - !ruby/object:Gem::Version
27
28
  version: '4.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: 2.14.1
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 2.14.1
43
+ - !ruby/object:Gem::Dependency
44
+ name: sqlite3
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.3.10
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: 1.3.10
28
57
  description: A gem that provides Sequel::Models with polymorphic association capabilities
29
- email: therealdave.myron@gmail.com
58
+ email:
59
+ - jack.dempsey@gmail.com
60
+ - therealdave.myron@gmail.com
61
+ - kuraga333@mail.ru
30
62
  executables: []
31
63
  extensions: []
32
64
  extra_rdoc_files:
33
65
  - README.md
34
66
  - LICENSE
35
- - TODO
36
67
  files:
37
68
  - LICENSE
38
69
  - README.md
39
- - TODO
40
70
  - lib/sequel_polymorphic.rb
41
71
  - lib/sequel_polymorphic/sequel_polymorphic.rb
42
72
  homepage: https://github.com/jackdempsey/sequel_polymorphic
@@ -51,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
81
  requirements:
52
82
  - - ">="
53
83
  - !ruby/object:Gem::Version
54
- version: '0'
84
+ version: 1.8.7
55
85
  required_rubygems_version: !ruby/object:Gem::Requirement
56
86
  requirements:
57
87
  - - ">="
@@ -59,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
89
  version: '0'
60
90
  requirements: []
61
91
  rubyforge_project:
62
- rubygems_version: 2.4.1
92
+ rubygems_version: 2.4.5
63
93
  signing_key:
64
94
  specification_version: 4
65
95
  summary: A gem that provides Sequel::Models with polymorphic association capabilities
data/TODO DELETED
File without changes