one_touch 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/one_touch/models/acts_as_favor.rb +1 -1
- data/lib/one_touch/version.rb +1 -1
- data/one_touch.gemspec +2 -2
- data/readme.md +27 -30
- metadata +6 -6
@@ -8,7 +8,7 @@ module OneTouch
|
|
8
8
|
class_attribute :cons_context, :cons_context_match, :context_relation, :instance_writer => false
|
9
9
|
self.cons_context = self::CONTEXT
|
10
10
|
self.cons_context_match = self::CONTEXT_MATCH
|
11
|
-
self.context_relation = self::CONTEXT_RELATION if defined? CONTEXT_RELATION
|
11
|
+
self.context_relation = self::CONTEXT_RELATION if defined? self::CONTEXT_RELATION
|
12
12
|
|
13
13
|
# Seems there are many hook work to do, oppose hook, belongs hook ....
|
14
14
|
# All needed hook should be just about relations between contexts
|
data/lib/one_touch/version.rb
CHANGED
data/one_touch.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["raykin"]
|
9
9
|
s.email = ["raykincoldxiao@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{
|
11
|
+
s.summary = %q{ It is used for recording relation between resources }
|
12
|
+
s.description = %q{ Make one click operation simple }
|
13
13
|
|
14
14
|
s.rubyforge_project = "one_touch"
|
15
15
|
|
data/readme.md
CHANGED
@@ -13,63 +13,58 @@ version ~> 2 will support Rails3.1 and need squeel
|
|
13
13
|
Suppose we have User, Post and Tag model.We also has a Favor model which record the relations between users or between user and post.Favor model could be anyother model.
|
14
14
|
|
15
15
|
Then we define:
|
16
|
-
```ruby
|
17
|
-
class User; as_favor_host; as_favorable; end;
|
18
16
|
|
17
|
+
class User; as_favor_host; as_favorable; end;
|
19
18
|
class Post; as_favorable; end;
|
20
|
-
|
21
19
|
class Tag; as_favorable; end;
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
# Don't forget context column
|
20
|
+
Make sure Favor has defined constants CONTEXT and CONTEXT_MATCH before acts_as_favor
|
21
|
+
The Favor columns depend on the relations you defined
|
22
|
+
In this case, it would be id, user_id, favorable_id, favorable_type, context.
|
23
|
+
Don't forget context column.
|
27
24
|
class Favor
|
28
25
|
CONTEXT = Set.new(%w[focus favorite follow agree oppose buy subscribe])
|
29
26
|
CONTEXT_MATCH = { :focus => [:Tag, :Post], :follow => [:User] }
|
30
27
|
|
31
28
|
acts_as_favor
|
32
29
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
The generated relations between them are:
|
31
|
+
User has_many :favors, :class_name => "Favor", :as => :host
|
32
|
+
Post has_many :be_favors, :class_name => "Favor", :as => :favorable
|
33
|
+
Favor belongs_to :host, :class_name => "User"; belongs_to :favorable, :polymorphic => true
|
34
|
+
This is default setting.
|
35
|
+
|
39
36
|
You can define the relation by yourself.
|
40
|
-
|
37
|
+
|
41
38
|
class Favor
|
42
39
|
belongs_to :host, :polymorphic => true
|
43
40
|
belongs_to :favorable, :polymorphic => true
|
44
41
|
acts_as_favor :no_default_relations => true
|
45
42
|
end
|
46
|
-
|
47
|
-
|
43
|
+
Make sure the relation name are host and favorable, they can not be changed
|
44
|
+
|
48
45
|
Or you want Favor model to be another name, Bridge.
|
49
|
-
|
46
|
+
|
50
47
|
class User
|
51
48
|
as_favor_host :klass => "Bridge"
|
52
49
|
end
|
53
|
-
|
50
|
+
|
54
51
|
In short, there are four relation names can not be configured, favors, be_favors, host and favorable.
|
55
52
|
And if you change the Favor model name, make sure it consists in all applications.
|
56
53
|
|
57
54
|
## Features
|
58
55
|
Favor.add_context and Favor.del_context can add or del favors. Ex:
|
59
|
-
|
60
|
-
# @host focus on @favorable
|
56
|
+
|
57
|
+
# add a record that @host focus on @favorable
|
61
58
|
Favor.add_focus(@host, @favorable)
|
62
59
|
#=> success return true, fail return error @favor object
|
63
60
|
#=> could also
|
64
61
|
|
65
|
-
# @host remove focus on @favorable
|
62
|
+
# remove a record. @host remove focus on @favorable
|
66
63
|
Favor.del_focus(@host, @favorable)
|
67
64
|
|
68
|
-
```ruby
|
69
|
-
|
70
65
|
### Query Methods
|
71
66
|
The most useful methods would be: favor_to, favored_by, allstars. They are all class methods.
|
72
|
-
|
67
|
+
|
73
68
|
User.favor_to(@user1, :follow) #=> return guys(an ActiveRelation object) who follows @user1, so you can define fans
|
74
69
|
def fans
|
75
70
|
User.favor_to(self, :follow)
|
@@ -84,27 +79,26 @@ The most useful methods would be: favor_to, favored_by, allstars. They are all c
|
|
84
79
|
@user1.focus_tags
|
85
80
|
# => return the tags which was focused by @user1
|
86
81
|
@user1.favor?(@tag1, :focus) #=> return true if @user1 focus on @tag1
|
87
|
-
|
82
|
+
|
88
83
|
Actually the favor_to and favored_by are fundmental methods which many other methods based on
|
89
84
|
|
90
85
|
#### Analyze Relation Data
|
91
86
|
|
92
87
|
Use Favor.host_relations to get the relations of Users.
|
93
88
|
But first you should define the relation mark rule.
|
94
|
-
|
89
|
+
|
95
90
|
class Favor
|
96
91
|
def self.relation_rules
|
97
92
|
{ [:agree, :Answer] => 3, [:follow, :User] => 2, [:focus, :Tag] => 1 }
|
98
93
|
end
|
99
94
|
end
|
100
|
-
```ruby
|
101
95
|
|
102
96
|
The results like:
|
103
|
-
|
97
|
+
|
104
98
|
Favor.host_relations
|
105
99
|
# It a Hash, the first element is
|
106
100
|
# => @user1 => { @user2 => 100, @user3 => 50, .... }
|
107
|
-
|
101
|
+
|
108
102
|
This means @user1 is more close to @user2 than @user3.Maybe the answers that @user1 and @user2 both agree are more than the answers that @user1 and @user2 both agree.Actually it depends on your mark rule, but it really shows there must be more common betweens @user1 and @user2.
|
109
103
|
##### Becare running host_relations is really time consumer, so run it in backend job and keep the results.
|
110
104
|
|
@@ -120,3 +114,6 @@ This means @user1 is more close to @user2 than @user3.Maybe the answers that @us
|
|
120
114
|
### Abstract Requirement
|
121
115
|
Connect any two resource in one model.
|
122
116
|
I really want to implement this gem as more abstract requirement.That means four relation names can be configured, But's not easy.
|
117
|
+
|
118
|
+
### License
|
119
|
+
MIT
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: one_touch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-14 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &25054060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,8 +21,8 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
description: ! '
|
24
|
+
version_requirements: *25054060
|
25
|
+
description: ! ' Make one click operation simple '
|
26
26
|
email:
|
27
27
|
- raykincoldxiao@gmail.com
|
28
28
|
executables: []
|
@@ -76,6 +76,6 @@ rubyforge_project: one_touch
|
|
76
76
|
rubygems_version: 1.8.11
|
77
77
|
signing_key:
|
78
78
|
specification_version: 3
|
79
|
-
summary:
|
79
|
+
summary: It is used for recording relation between resources
|
80
80
|
test_files: []
|
81
81
|
has_rdoc:
|