my_feeds 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/README.md +9 -10
- data/lib/my_feeds/identify.rb +20 -13
- data/lib/my_feeds/version.rb +1 -1
- data/lib/rails/generators/templates/like_model.rb +1 -1
- data/spec/models/feed.rb +10 -0
- data/spec/models/like.rb +7 -0
- data/spec/models/post.rb +12 -0
- data/spec/models/user.rb +20 -0
- data/spec/my_feeds_spec.rb +76 -0
- data/spec/schema.rb +41 -0
- data/spec/spec_helper.rb +14 -0
- metadata +23 -9
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
# MyFeeds
|
2
2
|
|
3
|
-
-------------------------------------
|
4
3
|
Help you implement feeds / timeline in rails application.
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
8
|
-
------------------------------------
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
11
9
|
gem 'my_feeds'
|
@@ -20,12 +18,9 @@ Or install it yourself as:
|
|
20
18
|
|
21
19
|
## Usage
|
22
20
|
|
23
|
-
------------------------------------
|
24
21
|
|
25
22
|
####before usage
|
26
|
-
my_feeds need
|
27
|
-
|
28
|
-
`feeder`, `eater` and associate model
|
23
|
+
my_feeds need `feeder`, `eater` and associate model
|
29
24
|
|
30
25
|
feeder publish feeds
|
31
26
|
|
@@ -50,12 +45,17 @@ just make sure your model have a polymorphic field for feeder.
|
|
50
45
|
|
51
46
|
and
|
52
47
|
```
|
53
|
-
class
|
48
|
+
class YourCustomModel < ActiveRecord::Base
|
54
49
|
include MyFeeds::Identify #make sure include this module
|
55
|
-
self.
|
50
|
+
self.feed_polymorphic = :likeable #and set this, in this case is likeable, it should same as the polymorphic belongs_to
|
56
51
|
|
57
|
-
|
52
|
+
#same as self.feed_polymorphic
|
53
|
+
belongs_to :likeable, polymorphic: true
|
58
54
|
end
|
55
|
+
|
56
|
+
# you should create a migration like below
|
57
|
+
# in this case, this column name is likeable_identify, it depends on feed_polymorphic
|
58
|
+
#add_column :your_customs, :likeable_identify, :string, null: false
|
59
59
|
```
|
60
60
|
|
61
61
|
####feeder
|
@@ -165,7 +165,6 @@ end
|
|
165
165
|
|
166
166
|
## Contributing
|
167
167
|
|
168
|
-
-------------------
|
169
168
|
|
170
169
|
1. Fork it
|
171
170
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
data/lib/my_feeds/identify.rb
CHANGED
@@ -4,32 +4,39 @@ module MyFeeds
|
|
4
4
|
|
5
5
|
included do
|
6
6
|
before_save :save_feed_identify
|
7
|
-
|
8
|
-
|
9
|
-
delegate :default_feed_polymorphic_name, :polymorphic_identify_column, :polymorphic_id_column, :polymorphic_type_column, to: :"self.class"
|
7
|
+
self.feed_polymorphic = self == Feed ? :source : default_feed_polymorphic
|
8
|
+
delegate :default_feed_polymorphic, :polymorphic_identify_column, :polymorphic_id_column, :polymorphic_type_column, to: :"self.class"
|
10
9
|
end
|
11
10
|
|
12
|
-
protected
|
13
|
-
def save_feed_identify
|
14
|
-
instance_eval %Q{self.#{polymorphic_identify_column} = "#\{#{polymorphic_id_column}.to_s\}@#\{#{polymorphic_type_column}.to_s.underscore\}"}
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
11
|
module ClassMethods
|
19
|
-
def
|
12
|
+
def feed_polymorphic
|
13
|
+
@_feed_polymorphic
|
14
|
+
end
|
15
|
+
|
16
|
+
def feed_polymorphic= polymorphic
|
17
|
+
@_feed_polymorphic = polymorphic
|
18
|
+
class_eval %Q{
|
19
|
+
def save_feed_identify
|
20
|
+
self.#{polymorphic_identify_column} = "#\{#{polymorphic_id_column}.to_s\}@#\{#{polymorphic_type_column}.to_s.underscore\}"
|
21
|
+
end
|
22
|
+
protected :save_feed_identify
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_feed_polymorphic
|
20
27
|
self.to_s.underscore
|
21
28
|
end
|
22
29
|
|
23
30
|
def polymorphic_identify_column
|
24
|
-
:"#{
|
31
|
+
:"#{feed_polymorphic}_identify"
|
25
32
|
end
|
26
33
|
|
27
34
|
def polymorphic_id_column
|
28
|
-
:"#{
|
35
|
+
:"#{feed_polymorphic}_id"
|
29
36
|
end
|
30
37
|
|
31
38
|
def polymorphic_type_column
|
32
|
-
:"#{
|
39
|
+
:"#{feed_polymorphic}_type"
|
33
40
|
end
|
34
41
|
end
|
35
42
|
end
|
data/lib/my_feeds/version.rb
CHANGED
data/spec/models/feed.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
class Feed < ActiveRecord::Base
|
2
|
+
include MyFeeds::Identify
|
3
|
+
attr_accessible :source_id, :source_type, :event, :target_id, :target_type
|
4
|
+
|
5
|
+
#verify event uniqueness
|
6
|
+
#validates :event, uniqueness: {scope: [:source_id, :source_type, :target_id, :target_type]}, :if => :target_id
|
7
|
+
|
8
|
+
belongs_to :source, polymorphic: true
|
9
|
+
belongs_to :target, polymorphic: true
|
10
|
+
end
|
data/spec/models/like.rb
ADDED
data/spec/models/post.rb
ADDED
data/spec/models/user.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
attr_accessible :name
|
3
|
+
has_many :posts
|
4
|
+
has_many :likes
|
5
|
+
|
6
|
+
include MyFeeds::Feeder
|
7
|
+
include MyFeeds::Eater
|
8
|
+
|
9
|
+
define_feed_event :write_post, target_type: Post.to_s
|
10
|
+
define_feed_event :write_some_code
|
11
|
+
|
12
|
+
def post title, content
|
13
|
+
post = self.posts.create title: title, content: content
|
14
|
+
write_post target_id: post.id
|
15
|
+
end
|
16
|
+
|
17
|
+
def feeds
|
18
|
+
feeds_for likes
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MyFeeds do
|
4
|
+
before :all do
|
5
|
+
@user1 = User.create name: 'jjyr'
|
6
|
+
@user2 = User.create name: 'writer'
|
7
|
+
end
|
8
|
+
|
9
|
+
describe MyFeeds::Feeder do
|
10
|
+
before :all do
|
11
|
+
@user2.write_some_code
|
12
|
+
@user2.post "i write a gem", "content is not important"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "when call feed_event should create feed" do
|
16
|
+
@user2.source_feeds.count.should == 2
|
17
|
+
@user2.source_feeds.count.should == Feed.count
|
18
|
+
@user2.source_feeds.first.event.should == 'write_some_code'
|
19
|
+
@user2.source_feeds.last.event.should == 'write_post'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "scope version source_feeds should work" do
|
23
|
+
@user2.write_some_code_feeds.count.should == 1
|
24
|
+
@user2.write_some_code_feeds.first.event.should == 'write_some_code'
|
25
|
+
@user2.write_post_feeds.count.should == 1
|
26
|
+
@user2.write_post_feeds.first.event.should == 'write_post'
|
27
|
+
(@user2.write_post_feeds.count + @user2.write_some_code_feeds.count).should == @user2.source_feeds.count
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
describe MyFeeds::Eater do
|
33
|
+
before :all do
|
34
|
+
@user1.feeds.count.should == 0
|
35
|
+
like = @user1.likes.new
|
36
|
+
like.likeable = @user2
|
37
|
+
like.save!
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have some feeds" do
|
41
|
+
@user1.feeds.count.should == Feed.count
|
42
|
+
end
|
43
|
+
|
44
|
+
it "feeds should change" do
|
45
|
+
@user2.posts.first.update_attributes content: "update"
|
46
|
+
@user1.feeds.count.should == Feed.count - 1
|
47
|
+
like = @user1.likes.new
|
48
|
+
like.likeable = @user2.posts.first
|
49
|
+
like.save!
|
50
|
+
@user1.feeds.count.should == Feed.count
|
51
|
+
end
|
52
|
+
|
53
|
+
it "scope should work" do
|
54
|
+
@user1.feeds_for(@user1.likes.where likeable_type: Post.to_s).count.should == @user1.feeds.where(source_type: Post.to_s).count
|
55
|
+
@user1.feeds_for(@user1.likes.where likeable_type: User.to_s).count.should == @user1.feeds.where(source_type: User.to_s).count
|
56
|
+
end
|
57
|
+
|
58
|
+
it "eater's feeds should match feeder's write_post_feeds" do
|
59
|
+
post_feeds = @user2.write_post_feeds.to_a
|
60
|
+
user_post_feeds = @user1.feeds.where(target_type: Post.to_s).to_a
|
61
|
+
post_feeds.count.should == user_post_feeds.count
|
62
|
+
user_post_feeds.each do |post|
|
63
|
+
post_feeds.should include(post)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "eater's feeds should match feeder's write_some_code_feeds" do
|
68
|
+
code_feeds = @user2.write_some_code_feeds.to_a
|
69
|
+
user_feeds = @user1.feeds.where(event: 'write_some_code').to_a
|
70
|
+
code_feeds.count.should == user_feeds.count
|
71
|
+
user_feeds.each do |feed|
|
72
|
+
code_feeds.should include(feed)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
|
3
|
+
create_table "feeds", :force => true do |t|
|
4
|
+
t.integer "source_id", :null => false
|
5
|
+
t.string "source_type", :null => false
|
6
|
+
t.string "source_identify", :null => false
|
7
|
+
t.string "event", :null => false
|
8
|
+
t.integer "target_id"
|
9
|
+
t.string "target_type"
|
10
|
+
t.datetime "created_at", :null => false
|
11
|
+
t.datetime "updated_at", :null => false
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index "feeds", ["source_identify"], :name => "index_feeds_on_source_identify"
|
15
|
+
|
16
|
+
create_table "likes", :force => true do |t|
|
17
|
+
t.string "likeable_identify", :null => false
|
18
|
+
t.integer "likeable_id", :null => false
|
19
|
+
t.string "likeable_type", :null => false
|
20
|
+
t.integer "user_id"
|
21
|
+
t.datetime "created_at", :null => false
|
22
|
+
t.datetime "updated_at", :null => false
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table "posts", :force => true do |t|
|
26
|
+
t.string "title"
|
27
|
+
t.string "content"
|
28
|
+
t.integer "user_id"
|
29
|
+
t.datetime "created_at", :null => false
|
30
|
+
t.datetime "updated_at", :null => false
|
31
|
+
end
|
32
|
+
|
33
|
+
add_index "posts", ["user_id"], :name => "index_posts_on_user_id"
|
34
|
+
|
35
|
+
create_table "users", :force => true do |t|
|
36
|
+
t.string "name"
|
37
|
+
t.datetime "created_at", :null => false
|
38
|
+
t.datetime "updated_at", :null => false
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
require 'active_record'
|
5
|
+
require 'my_feeds'
|
6
|
+
|
7
|
+
require 'models/feed'
|
8
|
+
require 'models/like'
|
9
|
+
require 'models/post'
|
10
|
+
require 'models/user'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
13
|
+
ActiveRecord::Migration.verbose = false
|
14
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
metadata
CHANGED
@@ -1,48 +1,48 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_feeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- jjy
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
none: false
|
17
18
|
requirements:
|
18
19
|
- - ~>
|
19
20
|
- !ruby/object:Gem::Version
|
20
21
|
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
22
25
|
none: false
|
23
26
|
requirements:
|
24
27
|
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '1.3'
|
27
|
-
prerelease: false
|
28
|
-
type: :development
|
29
|
-
name: bundler
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
31
32
|
requirement: !ruby/object:Gem::Requirement
|
32
33
|
none: false
|
33
34
|
requirements:
|
34
35
|
- - ! '>='
|
35
36
|
- !ruby/object:Gem::Version
|
36
37
|
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
37
40
|
version_requirements: !ruby/object:Gem::Requirement
|
38
41
|
none: false
|
39
42
|
requirements:
|
40
43
|
- - ! '>='
|
41
44
|
- !ruby/object:Gem::Version
|
42
45
|
version: '0'
|
43
|
-
prerelease: false
|
44
|
-
type: :development
|
45
|
-
name: rake
|
46
46
|
description: Help you implement feeds / timeline in rails application.
|
47
47
|
email:
|
48
48
|
- jjyruby@gmail.com
|
@@ -67,6 +67,13 @@ files:
|
|
67
67
|
- lib/rails/generators/templates/migration.rb
|
68
68
|
- lib/rails/generators/templates/model.rb
|
69
69
|
- my_feeds.gemspec
|
70
|
+
- spec/models/feed.rb
|
71
|
+
- spec/models/like.rb
|
72
|
+
- spec/models/post.rb
|
73
|
+
- spec/models/user.rb
|
74
|
+
- spec/my_feeds_spec.rb
|
75
|
+
- spec/schema.rb
|
76
|
+
- spec/spec_helper.rb
|
70
77
|
homepage: https://github.com/jjyr/my_feeds
|
71
78
|
licenses:
|
72
79
|
- MIT
|
@@ -92,4 +99,11 @@ rubygems_version: 1.8.25
|
|
92
99
|
signing_key:
|
93
100
|
specification_version: 3
|
94
101
|
summary: my_feeds provide feed model and some helper methods, allow you to customize.
|
95
|
-
test_files:
|
102
|
+
test_files:
|
103
|
+
- spec/models/feed.rb
|
104
|
+
- spec/models/like.rb
|
105
|
+
- spec/models/post.rb
|
106
|
+
- spec/models/user.rb
|
107
|
+
- spec/my_feeds_spec.rb
|
108
|
+
- spec/schema.rb
|
109
|
+
- spec/spec_helper.rb
|