one_touch 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/Rakefile +3 -2
- data/lib/one_touch/core/bridge.rb +7 -6
- data/lib/one_touch/core/bridge_context.rb +16 -0
- data/lib/one_touch/models/active_record_extension.rb +6 -40
- data/lib/one_touch/models/after_as_favor.rb +17 -1
- data/lib/one_touch/models/as_favorable.rb +1 -0
- data/lib/one_touch/version.rb +1 -1
- data/readme.md +2 -0
- data/spec/core/relation_recorder_spec.rb +1 -11
- data/spec/models/as_favorable_spec.rb +2 -1
- data/spec/spec_helper.rb +59 -12
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34cb00212443af916d927fa834c4cd6e6b67fd46
|
4
|
+
data.tar.gz: 98a3670dd6b8fb6e2a617f4e4c9efdf254d6407a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1f6abd1563021cdb01749dbef9cbcc19c0670e3c2113510d61755e8a2ff93d94ef72569f98d654ffe6f1a30eb8a2970ad13177708fbac3931e9a4e44349d5d8
|
7
|
+
data.tar.gz: da50c4fc6e74136788fca80ff866aac68c5fc7034f2d23b86f4313686252ed9419787f155a1b54386145d3fe2d325346f58be5da3c836562b4ed5e5542fe1f60
|
data/CHANGELOG
ADDED
data/Rakefile
CHANGED
@@ -2,7 +2,8 @@ require 'bundler/gem_tasks'
|
|
2
2
|
|
3
3
|
desc "Running Test"
|
4
4
|
task :test do
|
5
|
-
# forgot how to include all spec files
|
6
|
-
system "bundle exec rspec spec/models/*spec.rb --debugger --backtrace --fail-fast"
|
7
5
|
system "bundle exec rspec spec/core/*spec.rb --debugger --backtrace --fail-fast"
|
6
|
+
(0..5).each do |num|
|
7
|
+
system "OneTouchSeq=#{num} bundle exec rspec spec/models/*spec.rb --debugger --backtrace --fail-fast"
|
8
|
+
end
|
8
9
|
end
|
@@ -32,10 +32,12 @@ module OneTouch
|
|
32
32
|
# add only one port, make sure pg is {portname: klassname}
|
33
33
|
def add_new_port(pg)
|
34
34
|
portname = pg.keys.first
|
35
|
-
if
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
if portname # pg maybe equal {} and portname is nil
|
36
|
+
if port_group.has_key? portname
|
37
|
+
self.port_group[portname].concat pg[portname]
|
38
|
+
else
|
39
|
+
self.port_group[portname] = pg[portname]
|
40
|
+
end
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -75,9 +77,8 @@ module OneTouch
|
|
75
77
|
module Bridge
|
76
78
|
|
77
79
|
def self.included(base)
|
78
|
-
RelationRecorder.update_or_new(bridge_klass: base.name, bridge_class: base)
|
80
|
+
RelationRecorder.update_or_new(bridge_klass: base.name, bridge_class: base, port_group: {})
|
79
81
|
end
|
80
82
|
|
81
|
-
|
82
83
|
end # END Bridge
|
83
84
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Bridge with Context style
|
2
|
+
# The bridge class contains context attribute which defines the relation context between two ports
|
3
|
+
module OneTouch
|
4
|
+
module BridgeContext
|
5
|
+
|
6
|
+
class DependencyError
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
raise DependencyError unless new.respond_to? :context
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
end # END BridgeContext
|
16
|
+
end
|
@@ -9,10 +9,7 @@ module OneTouch
|
|
9
9
|
module ActiveRecordExtension
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
12
|
+
# methods added here are added to ActiveRecord::Base, so make it as thin as possible
|
16
13
|
module ClassMethods
|
17
14
|
|
18
15
|
# Default is define two relations:
|
@@ -28,9 +25,8 @@ module OneTouch
|
|
28
25
|
def acts_as_favor(opt={})
|
29
26
|
include ::OneTouch::Bridge
|
30
27
|
opt.reverse_merge!({:include_host_relation_module => true, :host_class_name => "User", :no_default_relations => false})
|
31
|
-
class_attribute :host_relation_type, :favorable_relation_type, :instance_writer => false
|
32
|
-
|
33
|
-
# method_name_of_host_klasses = "host_klasses_for_#{name.underscore}"
|
28
|
+
class_attribute :host_relation_type, :favorable_relation_type, :include_host_relation_module, :instance_writer => false
|
29
|
+
self.include_host_relation_module = opt[:include_host_relation_module]
|
34
30
|
|
35
31
|
include ActsAsFavor
|
36
32
|
|
@@ -51,25 +47,10 @@ module OneTouch
|
|
51
47
|
end
|
52
48
|
end
|
53
49
|
|
50
|
+
# include Relation model and make favorable classes also include HostRelation if needed
|
51
|
+
# make sure the former loaded host class run method after_load_acts_as_favor
|
54
52
|
include AfterAsFavor
|
55
53
|
|
56
|
-
# TODO:
|
57
|
-
# I should add more files to make here more clear
|
58
|
-
# Some methods should be include first
|
59
|
-
# And others should be include later
|
60
|
-
# The following code should be in another file
|
61
|
-
|
62
|
-
if opt[:include_host_relation_module]
|
63
|
-
class_attribute :include_host_relation_module, :instance_writer => false
|
64
|
-
self.include_host_relation_module = true
|
65
|
-
add_host_relation_map(relation_recorder.port_group[:favorable], :be_favors, :host)
|
66
|
-
end
|
67
|
-
|
68
|
-
relation_recorder.port_group[:host].each do |klassname|
|
69
|
-
klass = klassname.to_s.constantize
|
70
|
-
klass.after_load_acts_as_favor if klass.respond_to? :after_load_acts_as_favor
|
71
|
-
end
|
72
|
-
|
73
54
|
end
|
74
55
|
|
75
56
|
# Used in host class
|
@@ -80,7 +61,6 @@ module OneTouch
|
|
80
61
|
include ::OneTouch::PortBuild
|
81
62
|
opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :host)
|
82
63
|
append_relation(opt)
|
83
|
-
# assign_cattr_in_favor_class(:host_klasses,opt)
|
84
64
|
# Programmer Doc
|
85
65
|
# The Name of has_many is stick to favors, to make it changable would cause some query problem
|
86
66
|
# :as => :host can not be configured too, cause in act_as_favor the relation name was sticked
|
@@ -95,25 +75,11 @@ module OneTouch
|
|
95
75
|
include ::OneTouch::PortBuild
|
96
76
|
opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :favorable)
|
97
77
|
append_relation(opt)
|
98
|
-
# assign_cattr_in_favor_class(:favorable_klasses,opt)
|
99
78
|
unless opt[:no_default_relations]
|
100
79
|
has_many :be_favors, :class_name => opt[:bridge_klass], :as => :favorable
|
101
80
|
end
|
102
81
|
include AsFavorable
|
103
|
-
include HostRelation if favor_class.respond_to? :include_host_relation_module and favor_class.include_host_relation_module
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def add_host_relation_map(favorable_klasses, *two_connection_name)
|
109
|
-
include Relations
|
110
|
-
self.target_to_media = :be_favors
|
111
|
-
self.media_to_host = :host
|
112
|
-
|
113
|
-
favorable_klasses.each do |k|
|
114
|
-
klass = k.to_s.constantize
|
115
|
-
klass.send :include, HostRelation # host_relation model was added to favorable klass
|
116
|
-
end
|
82
|
+
include HostRelation if favor_class.respond_to? :include_host_relation_module and favor_class.include_host_relation_module
|
117
83
|
end
|
118
84
|
|
119
85
|
end # End ClassMethods
|
@@ -17,7 +17,23 @@ module OneTouch
|
|
17
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
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
if include_host_relation_module
|
21
|
+
include Relations
|
22
|
+
self.target_to_media = :be_favors
|
23
|
+
self.media_to_host = :host
|
24
|
+
(relation_recorder.port_group[:favorable] || []).each do |k|
|
25
|
+
klass = k.to_s.constantize
|
26
|
+
klass.send :include, HostRelation # host_relation model was added to favorable klass
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
(relation_recorder.port_group[:host] || []).each do |klassname|
|
32
|
+
klass = klassname.to_s.constantize
|
33
|
+
klass.after_load_acts_as_favor if klass.respond_to? :after_load_acts_as_favor
|
34
|
+
end
|
35
|
+
|
36
|
+
end # END class_eval
|
21
37
|
|
22
38
|
module ClassMethods
|
23
39
|
|
data/lib/one_touch/version.rb
CHANGED
data/readme.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
### Upgrade warnning
|
2
|
+
There is a bug in 2.0.0 which happened on special loading sequence.I am fixing on it. So dont upgrade it to 2.0 right now.
|
1
3
|
### Intro
|
2
4
|
In webapp, many data is generated by just one click like focus someone, dislike some post.This gem make it easy.
|
3
5
|
|
@@ -1,22 +1,12 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
require 'one_touch'
|
3
3
|
|
4
|
-
class Bridge; end
|
5
|
-
|
6
4
|
class Favor; end
|
7
5
|
|
8
|
-
class User
|
9
|
-
include OneTouch::PortBuild
|
10
|
-
end
|
6
|
+
class User; include OneTouch::PortBuild; end
|
11
7
|
|
12
8
|
class Admin; include OneTouch::PortBuild; end
|
13
9
|
|
14
|
-
class Post; end
|
15
|
-
|
16
|
-
class Comment; end
|
17
|
-
|
18
|
-
class Port; end
|
19
|
-
|
20
10
|
User.append_relation(bridge_klass: 'Favor', portname: :host)
|
21
11
|
Admin.append_relation(bridge_klass: 'Favor', portname: :host)
|
22
12
|
|
@@ -38,7 +38,8 @@ describe OneTouch::AsFavorable do
|
|
38
38
|
context Favor do
|
39
39
|
it "build relations should return a hash of relation between favorable and host" do
|
40
40
|
Favor.build_relations.instance_of?(Hash).should be_true
|
41
|
-
Favor.build_relations.first.first.should eq(["favorite", Tag.first]) # means first key
|
41
|
+
# Favor.build_relations.first.first.should eq(["favorite", Tag.first]) # means first key
|
42
|
+
Favor.build_relations.keys.should include(["favorite", Tag.first]) # means first key
|
42
43
|
end
|
43
44
|
|
44
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -7,24 +7,71 @@ require File.join(File.dirname(__FILE__), 'fake_app')
|
|
7
7
|
require 'rspec/rails'
|
8
8
|
require 'models/load_test_data'
|
9
9
|
|
10
|
+
def puts_marks
|
11
|
+
puts ""
|
12
|
+
puts "#####################"
|
13
|
+
end
|
14
|
+
|
15
|
+
# The three different model type loading sequence
|
16
|
+
def loading_sequence
|
17
|
+
|
18
|
+
case ENV['OneTouchSeq'].to_i
|
19
|
+
when 0
|
20
|
+
puts_marks
|
21
|
+
puts "Loading Sequence 0: favorable -> host -> favor"
|
22
|
+
Tag.as_favorable
|
23
|
+
User.as_favor_host
|
24
|
+
User.as_favorable
|
25
|
+
Favor.acts_as_favor :host_class_name => nil
|
26
|
+
when 1
|
27
|
+
puts_marks
|
28
|
+
puts "Loading Sequence 1: favorable -> favor -> host"
|
29
|
+
Tag.as_favorable
|
30
|
+
Favor.acts_as_favor :host_class_name => nil
|
31
|
+
User.as_favor_host
|
32
|
+
User.as_favorable
|
33
|
+
when 2
|
34
|
+
puts_marks
|
35
|
+
puts "Loading Sequence 2: host -> favorable -> favor"
|
36
|
+
User.as_favor_host
|
37
|
+
Tag.as_favorable
|
38
|
+
Favor.acts_as_favor :host_class_name => nil
|
39
|
+
User.as_favorable
|
40
|
+
when 3
|
41
|
+
puts_marks
|
42
|
+
puts "Loading Sequence 3: host -> favor -> favorable"
|
43
|
+
User.as_favor_host
|
44
|
+
Favor.acts_as_favor :host_class_name => nil
|
45
|
+
Tag.as_favorable
|
46
|
+
User.as_favorable
|
47
|
+
when 4
|
48
|
+
puts_marks
|
49
|
+
puts "Loading Sequence 4: favor -> host -> favorable"
|
50
|
+
Favor.acts_as_favor :host_class_name => nil
|
51
|
+
User.as_favor_host
|
52
|
+
User.as_favorable
|
53
|
+
Tag.as_favorable
|
54
|
+
when 5
|
55
|
+
puts_marks
|
56
|
+
puts "Loading Sequence 5: favor -> favorable -> host"
|
57
|
+
Favor.acts_as_favor :host_class_name => nil
|
58
|
+
User.as_favorable
|
59
|
+
User.as_favor_host
|
60
|
+
Tag.as_favorable
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
10
64
|
|
11
65
|
RSpec.configure do |config|
|
12
66
|
config.use_transactional_fixtures = true
|
13
67
|
|
14
68
|
config.before :all do
|
15
69
|
#CreateAllTables.down if ActiveRecord::Base.connection.table_exists? 'favors'
|
16
|
-
|
17
|
-
|
18
|
-
|
70
|
+
unless ActiveRecord::Base.connection.table_exists? 'favors'
|
71
|
+
CreateAllTables.up
|
72
|
+
loading_sequence
|
73
|
+
end
|
74
|
+
# we has at least three type of loading sequence
|
19
75
|
|
20
|
-
User.as_favor_host
|
21
|
-
|
22
|
-
User.as_favorable
|
23
|
-
Favor.acts_as_favor :host_class_name => nil
|
24
|
-
# TestingFoo.acts_as_favor
|
25
|
-
# Tag.as_favorable :be_tagging_favors, :klass => "Tagging"
|
26
|
-
# Ask.as_favor_host :tagging_favors, :klass => "Tagging"
|
27
|
-
# Tagging.acts_as_favor :host_class_name => nil, :favorable_klass_name => :Tag, :include_favor_relations_module => true
|
28
|
-
# relation should be in another module
|
29
76
|
end
|
30
77
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: one_touch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- raykin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -46,10 +46,12 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
+
- CHANGELOG
|
49
50
|
- Gemfile
|
50
51
|
- Rakefile
|
51
52
|
- lib/one_touch.rb
|
52
53
|
- lib/one_touch/core/bridge.rb
|
54
|
+
- lib/one_touch/core/bridge_context.rb
|
53
55
|
- lib/one_touch/engine.rb
|
54
56
|
- lib/one_touch/models/active_record_extension.rb
|
55
57
|
- lib/one_touch/models/acts_as_favor.rb
|