resonance 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/lib/resonance.rb +19 -29
- data/lib/resonance/version.rb +1 -1
- data/spec/dummy/application.rb +2 -2
- 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: b9d54325b0019a5cea69f4f76ceb55217e55e86f
|
4
|
+
data.tar.gz: 866b1fd32593d8e279cc1f6a3b2e7bc6ff21d35c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2fdb2ffd19c860f8ad5e16c96b244d6868d6e7173268ac2bc6e82305d870d9da3196792501069b489d94c4d1ea17b71e91cb6dc7bb137c0e40a37868617695b
|
7
|
+
data.tar.gz: 8d696bccc8430b5d9e2ffc29c77c964e42f42890a8e7ac132aaa750505874e5fa12badff7f289f562aba5c0f5f04b0664cd559647b9c4b37716a0df4a26a3171
|
data/README.md
CHANGED
@@ -51,7 +51,7 @@ Then, define `Resonatable` module to `app/models/concerns/resonatable.rb`:
|
|
51
51
|
module Resonatable
|
52
52
|
include Resonance
|
53
53
|
|
54
|
-
resonate :user,
|
54
|
+
resonate :user, target: :user, action: :follow
|
55
55
|
end
|
56
56
|
```
|
57
57
|
|
@@ -106,7 +106,7 @@ This case adds like feature to User and Post model.
|
|
106
106
|
module Resonatable
|
107
107
|
include Resonance
|
108
108
|
|
109
|
-
resonate :user,
|
109
|
+
resonate :user, target: :post, action: :like
|
110
110
|
end
|
111
111
|
|
112
112
|
class User < ActiveRecord::Base
|
@@ -137,8 +137,8 @@ At the same time, some `resonate` methods are able to be defined:
|
|
137
137
|
module Resonatable
|
138
138
|
include Resonance
|
139
139
|
|
140
|
-
resonate :user,
|
141
|
-
resonate :user,
|
140
|
+
resonate :user, target: :user, action: :follow
|
141
|
+
resonate :user, target: :post, action: :like
|
142
142
|
end
|
143
143
|
```
|
144
144
|
|
@@ -147,7 +147,10 @@ end
|
|
147
147
|
If you want to use other foreign key name, you can define it by `:foreign_key` option.
|
148
148
|
|
149
149
|
```ruby
|
150
|
-
resonate :user,
|
150
|
+
resonate :user,
|
151
|
+
target: :post,
|
152
|
+
action: :like,
|
153
|
+
foreign_key: { target: :post_id } # Default key is `:target_post_id`
|
151
154
|
```
|
152
155
|
|
153
156
|
## Contributing
|
data/lib/resonance.rb
CHANGED
@@ -11,12 +11,20 @@ module Resonance
|
|
11
11
|
module ClassMethods
|
12
12
|
include Resonance::Supports::Converter
|
13
13
|
|
14
|
-
def resonate(
|
15
|
-
|
16
|
-
|
14
|
+
def resonate(source, target: nil, action: nil, foreign_key: {})
|
15
|
+
[source, target, action].each do |role|
|
16
|
+
if role.nil?
|
17
|
+
raise Resonance::Errors::ArgumentError, 'Passed argument is not a valid'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
source = source.to_s
|
22
|
+
target = target.to_s
|
23
|
+
action = action.to_s
|
17
24
|
|
18
|
-
|
19
|
-
|
25
|
+
foreign_key.tap do |key|
|
26
|
+
key[:source] = :"#{source}_id" if key[:source].nil?
|
27
|
+
key[:target] = :"target_#{target}_id" if key[:target].nil?
|
20
28
|
end
|
21
29
|
|
22
30
|
classify(source).class_eval <<-EOS
|
@@ -33,7 +41,7 @@ module Resonance
|
|
33
41
|
return
|
34
42
|
end
|
35
43
|
|
36
|
-
#{pluralize(action)}.create(#{foreign_key}: target.id)
|
44
|
+
#{pluralize(action)}.create(#{foreign_key[:target]}: target.id)
|
37
45
|
end
|
38
46
|
|
39
47
|
def un#{action}(target)
|
@@ -41,17 +49,17 @@ module Resonance
|
|
41
49
|
return
|
42
50
|
end
|
43
51
|
|
44
|
-
#{pluralize(action)}.find_by(#{foreign_key}: target.id).destroy
|
52
|
+
#{pluralize(action)}.find_by(#{foreign_key[:target]}: target.id).destroy
|
45
53
|
end
|
46
54
|
|
47
55
|
def #{progressize(action)}?(target)
|
48
|
-
#{pluralize(action)}.exists?(#{foreign_key}: target.id)
|
56
|
+
#{pluralize(action)}.exists?(#{foreign_key[:target]}: target.id)
|
49
57
|
end
|
50
58
|
EOS
|
51
59
|
|
52
60
|
classify(target).class_eval <<-EOS
|
53
61
|
has_many :#{pluralize(action)}_as_target,
|
54
|
-
foreign_key: :#{foreign_key},
|
62
|
+
foreign_key: :#{foreign_key[:target]},
|
55
63
|
class_name: '#{classify(action)}',
|
56
64
|
dependent: :destroy
|
57
65
|
|
@@ -60,7 +68,7 @@ module Resonance
|
|
60
68
|
source: :#{source}
|
61
69
|
|
62
70
|
def #{pastize(action)}_by?(source)
|
63
|
-
source.#{pluralize(action)}.exists?(#{foreign_key}: id)
|
71
|
+
source.#{pluralize(action)}.exists?(#{foreign_key[:target]}: id)
|
64
72
|
end
|
65
73
|
EOS
|
66
74
|
|
@@ -69,26 +77,8 @@ module Resonance
|
|
69
77
|
|
70
78
|
belongs_to :target_#{target},
|
71
79
|
class_name: '#{classify(target)}',
|
72
|
-
foreign_key: :#{foreign_key}
|
80
|
+
foreign_key: :#{foreign_key[:target]}
|
73
81
|
EOS
|
74
82
|
end
|
75
|
-
|
76
|
-
private
|
77
|
-
|
78
|
-
def source
|
79
|
-
@roles[:source].to_s
|
80
|
-
end
|
81
|
-
|
82
|
-
def target
|
83
|
-
@roles[:target].to_s
|
84
|
-
end
|
85
|
-
|
86
|
-
def action
|
87
|
-
@roles[:action].to_s
|
88
|
-
end
|
89
|
-
|
90
|
-
def foreign_key
|
91
|
-
@options[:foreign_key] || "target_#{target}_id"
|
92
|
-
end
|
93
83
|
end
|
94
84
|
end
|
data/lib/resonance/version.rb
CHANGED
data/spec/dummy/application.rb
CHANGED
@@ -29,8 +29,8 @@ class Like < ActiveRecord::Base; end
|
|
29
29
|
module Resonatable
|
30
30
|
include Resonance
|
31
31
|
|
32
|
-
resonate :user,
|
33
|
-
resonate :user,
|
32
|
+
resonate :user, target: :user, action: :follow
|
33
|
+
resonate :user, target: :post, action: :like, foreign_key: { target: :post_id }
|
34
34
|
end
|
35
35
|
|
36
36
|
class User
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resonance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kami
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|