one_touch 1.2.0 → 1.3.0
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/Gemfile +2 -1
- data/lib/one_touch/models/active_record_extension.rb +4 -13
- data/lib/one_touch/models/acts_as_favor.rb +26 -3
- data/lib/one_touch/models/after_as_favor.rb +31 -0
- data/lib/one_touch/models/as_favor_host.rb +8 -1
- data/lib/one_touch/models/as_favorable.rb +2 -2
- data/lib/one_touch/version.rb +1 -1
- data/one_touch.gemspec +2 -2
- data/readme.md +7 -2
- data/spec/fake_app.rb +1 -1
- data/spec/models/acts_as_favor_spec.rb +7 -2
- metadata +17 -5
data/Gemfile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__),'acts_as_favor')
|
2
|
+
require File.join(File.dirname(__FILE__),'after_as_favor')
|
2
3
|
require File.join(File.dirname(__FILE__),'as_favor_host')
|
3
4
|
require File.join(File.dirname(__FILE__),'as_favorable')
|
4
5
|
require File.join(File.dirname(__FILE__),'relations')
|
@@ -50,24 +51,14 @@ module OneTouch
|
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
54
|
+
include AfterAsFavor
|
55
|
+
|
53
56
|
# TODO:
|
54
57
|
# I should add more files to make here more clear
|
55
58
|
# Some methods should be include first
|
56
59
|
# And others should be include later
|
57
60
|
# This code should be in another file
|
58
61
|
#
|
59
|
-
if self.host_relation_type == :single
|
60
|
-
scope :host_as, lambda { |ahost| where(:host_id => ahost.id) }
|
61
|
-
else
|
62
|
-
scope :host_as, lambda { |ahost| where(:host_id => ahost.id, :host_type => ahost.class.name)}
|
63
|
-
scope :join_host, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{table_name}.host_id AND #{table_name}.host_type = '#{type.to_s}'") }
|
64
|
-
end
|
65
|
-
if self.favorable_relation_type == :single
|
66
|
-
scope :favorable_as, lambda { |target| where(:favorable_id => target.id) }
|
67
|
-
else
|
68
|
-
scope :favorable_as, lambda { |target| where(:favorable_id => target.id, :favorable_type => target.class.name) }
|
69
|
-
scope :join_favorable, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{table_name}.favorable_id AND #{table_name}.favorable_type = '#{type.to_s}'") }
|
70
|
-
end
|
71
62
|
|
72
63
|
if opt[:include_host_relation_module]
|
73
64
|
class_attribute :include_host_relation_module, :instance_writer => false
|
@@ -134,7 +125,7 @@ module OneTouch
|
|
134
125
|
|
135
126
|
# Programmer Doc
|
136
127
|
# To maintain the connection betwwen host, favorable and favor class,
|
137
|
-
#
|
128
|
+
# defined class_attribute in their super class AR::Base
|
138
129
|
# I don't know if there is any good solution to not disturb AR::Base
|
139
130
|
def assign_cattr_in_favor_class(cattr_name,opt)
|
140
131
|
class_attribute :favor_klass, :favor_class, :instance_writer => false
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
1
|
module OneTouch
|
3
2
|
module ActsAsFavor
|
4
3
|
extend ActiveSupport::Concern
|
@@ -72,12 +71,29 @@ module OneTouch
|
|
72
71
|
# just the user click one button or touch one link, then a favor was created
|
73
72
|
def generate_add_methods(name)
|
74
73
|
method_name = "add_#{name}".to_sym
|
75
|
-
define_singleton_method method_name do |operator,resource,other_field_option={}|
|
74
|
+
define_singleton_method method_name do | operator, resource, other_field_option={} |
|
76
75
|
build_hash = other_field_option.merge(context: name)
|
77
76
|
favor = operator.favors.build build_hash
|
78
77
|
favor.favorable = resource
|
79
78
|
yield favor if block_given?
|
80
|
-
favor.save
|
79
|
+
if favor.save
|
80
|
+
favor_to_key = "#{resource.class.name}_favor_to_#{resource.class.name}_#{resource.id}_#{name}"
|
81
|
+
::Rails.cache.delete favor_to_key
|
82
|
+
end
|
83
|
+
favor # return saved favor, favor.error should not be blank when save is false
|
84
|
+
end
|
85
|
+
method_name = "add_#{name}!".to_sym
|
86
|
+
define_singleton_method method_name do | operator, resource, other_field_option={} |
|
87
|
+
if block_given?
|
88
|
+
result = send "add_#{name}", operator, resource, other_field_option, &block
|
89
|
+
else
|
90
|
+
result = send "add_#{name}", operator, resource, other_field_option
|
91
|
+
end
|
92
|
+
if result.errors.present?
|
93
|
+
raise(ActiveRecord::RecordInvalid.new(result))
|
94
|
+
else
|
95
|
+
result
|
96
|
+
end
|
81
97
|
end
|
82
98
|
end
|
83
99
|
|
@@ -90,6 +106,8 @@ module OneTouch
|
|
90
106
|
query_hash = other_field_option.merge(context: name)
|
91
107
|
favor = ope.favors.where(query_hash).favorable_as(res)
|
92
108
|
if favor.present?
|
109
|
+
favor_to_key = "#{res.class.name}_favor_to_#{res.class.name}_#{res.id}_#{name}"
|
110
|
+
::Rails.cache.delete favor_to_key
|
93
111
|
favor.first.destroy
|
94
112
|
else
|
95
113
|
false
|
@@ -116,6 +134,11 @@ module OneTouch
|
|
116
134
|
def media_to_host
|
117
135
|
:host
|
118
136
|
end
|
137
|
+
|
138
|
+
def build_by_three_elements(h, f, context)
|
139
|
+
new(:host => h, :favorable => f, :context => context)
|
140
|
+
end
|
141
|
+
|
119
142
|
end # end ClassMethods
|
120
143
|
|
121
144
|
private
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module OneTouch
|
2
|
+
module AfterAsFavor
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
|
7
|
+
if host_relation_type == :single
|
8
|
+
scope :host_as, lambda { |ahost| where(:host_id => ahost.id) }
|
9
|
+
else
|
10
|
+
scope :host_as, lambda { |ahost| where(:host_id => ahost.id, :host_type => ahost.class.name)}
|
11
|
+
scope :join_host, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{table_name}.host_id AND #{table_name}.host_type = '#{type.to_s}'") }
|
12
|
+
end
|
13
|
+
if favorable_relation_type == :single
|
14
|
+
scope :favorable_as, lambda { |target| where(:favorable_id => target.id) }
|
15
|
+
else
|
16
|
+
scope :favorable_as, lambda { |target| where(:favorable_id => target.id, :favorable_type => target.class.name) }
|
17
|
+
scope :join_favorable, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{table_name}.favorable_id AND #{table_name}.favorable_type = '#{type.to_s}'") }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
|
24
|
+
def find_by_three_elements(h,f,c)
|
25
|
+
host_as(h).favorable_as(f).context_as(c).first
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end # End AfterAsFavor
|
31
|
+
end
|
@@ -21,6 +21,13 @@ module OneTouch
|
|
21
21
|
host_context_as(context).merge(Favor.favorable_as favorable)
|
22
22
|
end
|
23
23
|
|
24
|
+
def cached_favor_to(favorable,context=default_context_in_host)
|
25
|
+
key = "#{self.name}_favor_to_#{favorable.class.name}_#{favorable.id}_#{context}"
|
26
|
+
Rails.cache.fetch key do
|
27
|
+
favor_to(favorable, context)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
24
31
|
def after_load_acts_as_favor
|
25
32
|
build_context_klass_methods
|
26
33
|
end
|
@@ -41,7 +48,7 @@ module OneTouch
|
|
41
48
|
# find the favor to target with specific context
|
42
49
|
# return ARelation object
|
43
50
|
def find_favor(target,context)
|
44
|
-
favors
|
51
|
+
favors.scoped.merge(Favor.context_as(context).favorable_as(target))
|
45
52
|
end
|
46
53
|
|
47
54
|
# check favor exist or not
|
@@ -18,7 +18,7 @@ module OneTouch
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def favored_by(user,context = default_context_in_favorable)
|
21
|
-
favorable_context_as(context)
|
21
|
+
favorable_context_as(context).merge(Favor.host_as(user))
|
22
22
|
end
|
23
23
|
|
24
24
|
def unfavored_by(user,context = default_context_in_favorable)
|
@@ -39,7 +39,7 @@ module OneTouch
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def favored_by?(ahost, context= default_context_in_favorable)
|
42
|
-
(be_favors
|
42
|
+
(be_favors.scoped.merge(Favor.host_as(ahost).context_as(context))).present?
|
43
43
|
end
|
44
44
|
|
45
45
|
private
|
data/lib/one_touch/version.rb
CHANGED
data/one_touch.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
|
14
14
|
s.rubyforge_project = "one_touch"
|
15
15
|
|
16
|
-
s.add_runtime_dependency 'rails', '
|
17
|
-
|
16
|
+
s.add_runtime_dependency 'rails', '> 3.0'
|
17
|
+
s.add_development_dependency 'rails', '> 3.0'
|
18
18
|
|
19
19
|
s.files = `git ls-files`.split("\n")
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/readme.md
CHANGED
@@ -60,11 +60,16 @@ And if you change the Favor model name, make sure it consists in all application
|
|
60
60
|
|
61
61
|
# add a record that @host focus on @favorable
|
62
62
|
Favor.add_focus(@host, @favorable)
|
63
|
-
#=>
|
64
|
-
|
63
|
+
#=> return @favor object, @favor.errors returns error msg if add favor was failed, you can use @favor.errors.blank? to check if favor was added successfully
|
64
|
+
|
65
|
+
####In version 1.3.0, add_focus change a little. And a new method add_focus! has added.
|
66
|
+
|
67
|
+
Favor.add_focus!(@host, @favorable)
|
68
|
+
#=> raise exception when add was failed
|
65
69
|
|
66
70
|
# remove a record. @host remove focus on @favorable
|
67
71
|
Favor.del_focus(@host, @favorable)
|
72
|
+
#=> false if favor not found
|
68
73
|
|
69
74
|
### Query Methods
|
70
75
|
The most useful methods would be: favor_to, favored_by, allstars. They are all class methods.
|
data/spec/fake_app.rb
CHANGED
@@ -23,7 +23,7 @@ class Favor < ActiveRecord::Base
|
|
23
23
|
# CONTEXT is an array of string and do not change the string to symbol
|
24
24
|
# CONTEXT_MATCH is all symbols, do not change it
|
25
25
|
CONTEXT = Set.new(%w[dig focus favorite follow dislike])
|
26
|
-
CONTEXT_MATCH = { :focus => [:Tag]
|
26
|
+
CONTEXT_MATCH = { :focus => [:Tag], :favorite => [:Tag],
|
27
27
|
:follow => [:User,:Investor], :dislike => [:User] }
|
28
28
|
CONTEXT_RELATION = { :oppose => [:follow, :dislike], :contain => [:favorite, :focus] }
|
29
29
|
|
@@ -17,6 +17,10 @@ describe OneTouch::ActsAsFavor do
|
|
17
17
|
Favor.favorable_klasses.should include(:User,:Tag)
|
18
18
|
end
|
19
19
|
|
20
|
+
it "find by three elements host, favorable and context" do
|
21
|
+
Favor.find_by_three_elements(foo, tag, :favorite).should eq(afavor)
|
22
|
+
end
|
23
|
+
|
20
24
|
it "should check context against entity type when the context is invalid" do
|
21
25
|
afavor.update_attributes context: "error_context"
|
22
26
|
afavor.valid?.should be_false
|
@@ -45,13 +49,14 @@ describe OneTouch::ActsAsFavor do
|
|
45
49
|
|
46
50
|
it "should add a focus to a tag " do
|
47
51
|
fly_favor = Favor.add_focus foo,fly_tag
|
48
|
-
fly_favor.should be_true
|
52
|
+
# fly_favor.should be_true
|
53
|
+
fly_favor.errors.blank?.should be_true
|
49
54
|
foo.favors.where(context: 'focus',favorable_id: fly_tag.id,favorable_type: 'Tag').first.should be_true
|
50
55
|
end
|
51
56
|
|
52
57
|
it " foo can not follow him self ,return an instance of Favor" do
|
53
58
|
Favor.add_follow(foo,foo).class.should eq(Favor)
|
54
|
-
|
59
|
+
lambda {Favor.add_follow!(foo,foo)}.should raise_error(ActiveRecord::RecordInvalid)
|
55
60
|
end
|
56
61
|
|
57
62
|
it " keys of context_match should belongs to context " do
|
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.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-12-06 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &10068800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>'
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10068800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rails
|
27
|
+
requirement: &10067760 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>'
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *10067760
|
25
36
|
description: ! ' Make one click operation simple '
|
26
37
|
email:
|
27
38
|
- raykincoldxiao@gmail.com
|
@@ -36,6 +47,7 @@ files:
|
|
36
47
|
- lib/one_touch/engine.rb
|
37
48
|
- lib/one_touch/models/active_record_extension.rb
|
38
49
|
- lib/one_touch/models/acts_as_favor.rb
|
50
|
+
- lib/one_touch/models/after_as_favor.rb
|
39
51
|
- lib/one_touch/models/array.rb
|
40
52
|
- lib/one_touch/models/as_favor_host.rb
|
41
53
|
- lib/one_touch/models/as_favorable.rb
|