inkwell 1.0.5 → 1.1.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.
Files changed (44) hide show
  1. data/app/models/inkwell/blog_item.rb +1 -1
  2. data/app/models/inkwell/comment.rb +22 -24
  3. data/app/models/inkwell/favorite_item.rb +1 -2
  4. data/app/models/inkwell/timeline_item.rb +1 -2
  5. data/db/migrate/20130202130030_change_is_comment_to_item_type.rb +18 -0
  6. data/db/migrate/20130202130040_add_owner_type_to_lines.rb +16 -0
  7. data/db/migrate/20130212130858_refactor_comment_table.rb +7 -0
  8. data/db/migrate/20130212130868_rename_parent_id_to_parent_comment_id_in_comment_table.rb +5 -0
  9. data/lib/acts_as_inkwell_community/base.rb +16 -16
  10. data/lib/acts_as_inkwell_post/base.rb +15 -14
  11. data/lib/acts_as_inkwell_user/base.rb +41 -34
  12. data/lib/common/base.rb +28 -4
  13. data/lib/inkwell/version.rb +1 -1
  14. data/test/dummy/db/development.sqlite3 +0 -0
  15. data/test/dummy/db/migrate/20130210231424_change_is_comment_to_item_type.inkwell.rb +19 -0
  16. data/test/dummy/db/migrate/20130212130848_add_owner_type_to_lines.inkwell.rb +17 -0
  17. data/test/dummy/db/migrate/20130213101736_refactor_comment_table.inkwell.rb +8 -0
  18. data/test/dummy/db/migrate/20130213121414_rename_parent_id_to_parent_comment_id_in_comment_table.inkwell.rb +6 -0
  19. data/test/dummy/db/schema.rb +14 -11
  20. data/test/dummy/db/test.sqlite3 +0 -0
  21. data/test/dummy/log/development.log +699 -0
  22. data/test/dummy/log/test.log +0 -0
  23. data/test/dummy/spec/functional/blogline_spec.rb +7 -7
  24. data/test/dummy/spec/functional/comments_spec.rb +129 -132
  25. data/test/dummy/spec/functional/community_spec.rb +119 -120
  26. data/test/dummy/spec/functional/favorite_spec.rb +27 -27
  27. data/test/dummy/spec/functional/following_spec.rb +26 -26
  28. data/test/dummy/spec/functional/reblog_spec.rb +32 -32
  29. data/test/dummy/spec/functional/timeline_spec.rb +1 -1
  30. data/test/dummy_without_community/db/development.sqlite3 +0 -0
  31. data/test/dummy_without_community/db/migrate/20130213115833_change_is_comment_to_item_type.inkwell.rb +19 -0
  32. data/test/dummy_without_community/db/migrate/20130213115834_add_owner_type_to_lines.inkwell.rb +17 -0
  33. data/test/dummy_without_community/db/migrate/20130213115835_refactor_comment_table.inkwell.rb +8 -0
  34. data/test/dummy_without_community/db/schema.rb +13 -10
  35. data/test/dummy_without_community/db/test.sqlite3 +0 -0
  36. data/test/dummy_without_community/log/development.log +111 -0
  37. data/test/dummy_without_community/log/test.log +11393 -0
  38. data/test/dummy_without_community/spec/functional/blogline_spec.rb +7 -7
  39. data/test/dummy_without_community/spec/functional/comments_spec.rb +128 -131
  40. data/test/dummy_without_community/spec/functional/favorite_spec.rb +27 -27
  41. data/test/dummy_without_community/spec/functional/following_spec.rb +26 -26
  42. data/test/dummy_without_community/spec/functional/reblog_spec.rb +32 -32
  43. data/test/dummy_without_community/spec/functional/timeline_spec.rb +1 -1
  44. metadata +32 -14
@@ -1,5 +1,5 @@
1
1
  module Inkwell
2
2
  class BlogItem < ActiveRecord::Base
3
- attr_accessible :item_id, :owner_id, :is_reblog, :is_comment, :created_at, :updated_at, :is_owner_user
3
+ attr_accessible :item_id, :owner_id, :is_reblog, :item_type, :created_at, :updated_at, :owner_type
4
4
  end
5
5
  end
@@ -1,13 +1,16 @@
1
1
  module Inkwell
2
2
  class Comment < ActiveRecord::Base
3
- attr_accessible :body, :post_id, :parent_id
3
+ require_relative '../../../lib/common/base.rb'
4
+ include ::Inkwell::Constants
5
+ include ::Inkwell::Common
6
+
7
+ attr_accessible :body, :parent_comment_id, :topmost_obj_id, :topmost_obj_type
4
8
  attr_accessor :is_reblogged
5
9
  attr_accessor :is_favorited
6
10
  attr_accessor :item_id_in_line
7
11
  attr_accessor :is_reblog_in_blogline
8
12
  attr_accessor :from_sources_in_timeline
9
13
 
10
- validates :"#{::Inkwell::Engine::config.post_table.to_s.singularize}_id", :presence => true
11
14
  validates :"#{::Inkwell::Engine::config.user_table.to_s.singularize}_id", :presence => true
12
15
  validates :body, :presence => true
13
16
 
@@ -15,7 +18,6 @@ module Inkwell
15
18
  before_destroy :destroy_comment_processing
16
19
 
17
20
  belongs_to ::Inkwell::Engine::config.user_table.to_s.singularize
18
- belongs_to ::Inkwell::Engine::config.post_table.to_s.singularize
19
21
 
20
22
  def commentline(options = {})
21
23
  options.symbolize_keys!
@@ -63,9 +65,9 @@ module Inkwell
63
65
  protected
64
66
 
65
67
  def remove_info_from_upper_comments(comments_info)
66
- return unless self.parent_id
67
- parent_comment = ::Inkwell::Comment.find self.parent_id
68
- raise "There is no comment with id = #{self.parent_id}" unless parent_comment
68
+ return unless self.parent_comment_id
69
+ parent_comment = ::Inkwell::Comment.find self.parent_comment_id
70
+ raise "There is no comment with id = #{self.parent_comment_id}" unless parent_comment
69
71
  users_ids_who_comment_it = ActiveSupport::JSON.decode parent_comment.users_ids_who_comment_it
70
72
  users_ids_who_comment_it -= comments_info
71
73
  parent_comment.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it
@@ -84,40 +86,36 @@ module Inkwell
84
86
  ::Inkwell::Comment.delete child_comments_ids_to_deleted
85
87
 
86
88
  comment_with_child_comments_ids_to_deleted = child_comments_ids_to_deleted << self.id
87
- ::Inkwell::TimelineItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true
88
- ::Inkwell::FavoriteItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true
89
- ::Inkwell::BlogItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true
89
+ ::Inkwell::TimelineItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT
90
+ ::Inkwell::FavoriteItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT
91
+ ::Inkwell::BlogItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT
90
92
 
91
93
  user_id = self.send("#{::Inkwell::Engine::config.user_table.to_s.singularize}_id")
92
94
  comment_with_child_comments_info = child_comments << Hash['user_id' => user_id, 'comment_id' => self.id]
93
95
  remove_info_from_upper_comments comment_with_child_comments_info
94
96
 
95
- post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
96
- post_id_attr = "#{::Inkwell::Engine::config.post_table.to_s.singularize}_id"
97
- parent_post = post_class.find self.send post_id_attr
98
- users_ids_who_comment_it = ActiveSupport::JSON.decode parent_post.users_ids_who_comment_it
97
+ parent_obj = get_class_for_item_type(self.topmost_obj_type).find self.topmost_obj_id
98
+ users_ids_who_comment_it = ActiveSupport::JSON.decode parent_obj.users_ids_who_comment_it
99
99
  users_ids_who_comment_it -= comment_with_child_comments_info
100
- parent_post.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it
101
- parent_post.save
100
+ parent_obj.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it
101
+ parent_obj.save
102
102
  end
103
103
 
104
104
  def processing_a_comment
105
- post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
106
- post_id_attr = "#{::Inkwell::Engine::config.post_table.to_s.singularize}_id"
107
- parent_post = post_class.find self.send post_id_attr
105
+ parent_obj = get_class_for_item_type(self.topmost_obj_type).find self.topmost_obj_id
108
106
  user_id = self.send "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
109
- users_ids_who_comment_it = ActiveSupport::JSON.decode parent_post.users_ids_who_comment_it
107
+ users_ids_who_comment_it = ActiveSupport::JSON.decode parent_obj.users_ids_who_comment_it
110
108
  users_ids_who_comment_it << Hash['user_id' => user_id, 'comment_id' => self.id]
111
- parent_post.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it
112
- parent_post.save
109
+ parent_obj.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it
110
+ parent_obj.save
113
111
 
114
112
  add_user_info_to_upper_comments
115
113
  end
116
114
 
117
115
  def add_user_info_to_upper_comments
118
- if self.parent_id
119
- parent_comment = ::Inkwell::Comment.find self.parent_id
120
- raise "Comment with id #{comment.parent_id} is not found" unless parent_comment
116
+ if self.parent_comment_id
117
+ parent_comment = ::Inkwell::Comment.find self.parent_comment_id
118
+ raise "Comment with id #{comment.parent_comment_id} is not found" unless parent_comment
121
119
  parent_upper_comments_tree = ActiveSupport::JSON.decode parent_comment.upper_comments_tree
122
120
  self_upper_comments_tree = parent_upper_comments_tree << parent_comment.id
123
121
  self.upper_comments_tree = ActiveSupport::JSON.encode self_upper_comments_tree
@@ -1,6 +1,5 @@
1
1
  module Inkwell
2
2
  class FavoriteItem < ActiveRecord::Base
3
- attr_accessible :item_id, :user_id, :is_comment, :created_at, :updated_at
4
- belongs_to ::Inkwell::Engine::config.user_table.to_s.singularize
3
+ attr_accessible :item_id, :owner_id, :item_type, :created_at, :updated_at, :owner_type
5
4
  end
6
5
  end
@@ -1,6 +1,5 @@
1
1
  module Inkwell
2
2
  class TimelineItem < ActiveRecord::Base
3
- attr_accessible :item_id, :user_id, :from_source, :has_many_sources, :is_comment, :created_at, :updated_at
4
- belongs_to ::Inkwell::Engine::config.user_table.to_s.singularize
3
+ attr_accessible :item_id, :owner_id, :from_source, :has_many_sources, :item_type, :created_at, :updated_at, :owner_type
5
4
  end
6
5
  end
@@ -0,0 +1,18 @@
1
+ class ChangeIsCommentToItemType < ActiveRecord::Migration
2
+ def change
3
+ add_column :inkwell_blog_items, :item_type, :string
4
+ ::Inkwell::BlogItem.where(:is_comment => true).update_all(:item_type => 'c')
5
+ ::Inkwell::BlogItem.where(:is_comment => false).update_all(:item_type => 'p')
6
+ remove_column :inkwell_blog_items, :is_comment
7
+
8
+ add_column :inkwell_favorite_items, :item_type, :string
9
+ ::Inkwell::FavoriteItem.where(:is_comment => true).update_all(:item_type => 'c')
10
+ ::Inkwell::FavoriteItem.where(:is_comment => false).update_all(:item_type => 'p')
11
+ remove_column :inkwell_favorite_items, :is_comment
12
+
13
+ add_column :inkwell_timeline_items, :item_type, :string
14
+ ::Inkwell::TimelineItem.where(:is_comment => true).update_all(:item_type => 'c')
15
+ ::Inkwell::TimelineItem.where(:is_comment => false).update_all(:item_type => 'p')
16
+ remove_column :inkwell_timeline_items, :is_comment
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ class AddOwnerTypeToLines < ActiveRecord::Migration
2
+ def change
3
+ add_column :inkwell_blog_items, :owner_type, :string
4
+ ::Inkwell::BlogItem.where(:is_owner_user => true).update_all(:owner_type => 'u')
5
+ ::Inkwell::BlogItem.where(:is_owner_user => false).update_all(:owner_type => 'c')
6
+ remove_column :inkwell_blog_items, :is_owner_user
7
+
8
+ add_column :inkwell_favorite_items, :owner_type, :string
9
+ ::Inkwell::FavoriteItem.update_all(:owner_type => 'u')
10
+ rename_column :inkwell_favorite_items, "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id", :owner_id
11
+
12
+ add_column :inkwell_timeline_items, :owner_type, :string
13
+ ::Inkwell::TimelineItem.update_all(:owner_type => 'u')
14
+ rename_column :inkwell_timeline_items, "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id", :owner_id
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ class RefactorCommentTable < ActiveRecord::Migration
2
+ def change
3
+ rename_column :inkwell_comments, "#{::Inkwell::Engine::config.post_table.to_s.singularize}_id", :topmost_obj_id
4
+ add_column :inkwell_comments, :topmost_obj_type, :string
5
+ ::Inkwell::Comment.update_all :topmost_obj_type => 'p'
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class RenameParentIdToParentCommentIdInCommentTable < ActiveRecord::Migration
2
+ def change
3
+ rename_column :inkwell_comments, :parent_id, :parent_comment_id
4
+ end
5
+ end
@@ -16,11 +16,13 @@ module Inkwell
16
16
  before_destroy :destroy_community_processing
17
17
 
18
18
  include ::Inkwell::ActsAsInkwellCommunity::InstanceMethods
19
- include ::Inkwell::Common
20
19
  end
21
20
  end
22
21
 
23
22
  module InstanceMethods
23
+ require_relative '../common/base.rb'
24
+ include ::Inkwell::Constants
25
+ include ::Inkwell::Common
24
26
 
25
27
  def add_user(options = {})
26
28
  options.symbolize_keys!
@@ -39,10 +41,10 @@ module Inkwell
39
41
 
40
42
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
41
43
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
42
- ::Inkwell::BlogItem.where(:owner_id => self.id, :is_owner_user => false).order("created_at DESC").limit(10).each do |blog_item|
44
+ ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY).order("created_at DESC").limit(10).each do |blog_item|
43
45
  next if post_class.find(blog_item.item_id).send(user_id_attr) == user.id
44
46
 
45
- item = ::Inkwell::TimelineItem.send "find_by_item_id_and_#{user_id_attr}_and_is_comment", blog_item.item_id, user.id, blog_item.is_comment
47
+ item = ::Inkwell::TimelineItem.where(:item_id => blog_item.item_id, :item_type => blog_item.item_type, :owner_id => user.id, :owner_type => OwnerTypes::USER).first
46
48
  if item
47
49
  item.has_many_sources = true unless item.has_many_sources
48
50
  sources = ActiveSupport::JSON.decode item.from_source
@@ -51,7 +53,7 @@ module Inkwell
51
53
  item.save
52
54
  else
53
55
  sources = [Hash['community_id' => self.id]]
54
- ::Inkwell::TimelineItem.create :item_id => blog_item.item_id, :is_comment => blog_item.is_comment, :user_id => user.id,
56
+ ::Inkwell::TimelineItem.create :item_id => blog_item.item_id, :item_type => blog_item.item_type, :owner_id => user.id, :owner_type => OwnerTypes::USER,
55
57
  :from_source => ActiveSupport::JSON.encode(sources), :created_at => blog_item.created_at
56
58
  end
57
59
  end
@@ -78,9 +80,7 @@ module Inkwell
78
80
  user.communities_ids = ActiveSupport::JSON.encode communities_ids
79
81
  user.save
80
82
 
81
- user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
82
-
83
- timeline_items = ::Inkwell::TimelineItem.where "from_source like '%{\"community_id\":#{self.id}%' and #{user_id_attr} = #{user.id}"
83
+ timeline_items = ::Inkwell::TimelineItem.where(:owner_id => user.id, :owner_type => OwnerTypes::USER).where "from_source like '%{\"community_id\":#{self.id}%'"
84
84
  timeline_items.delete_all :has_many_sources => false
85
85
  timeline_items.each do |item|
86
86
  from_source = ActiveSupport::JSON.decode item.from_source
@@ -166,15 +166,15 @@ module Inkwell
166
166
  raise "user tried to add post of another user" unless post.send(user_id_attr) == user.id
167
167
  raise "post is already added to this community" if post.communities_row.include? self.id
168
168
 
169
- ::Inkwell::BlogItem.create :owner_id => self.id, :is_owner_user => false, :item_id => post.id, :is_comment => false
169
+ ::Inkwell::BlogItem.create :owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY, :item_id => post.id, :item_type => ItemTypes::POST
170
170
  communities_ids = ActiveSupport::JSON.decode post.communities_ids
171
171
  communities_ids << self.id
172
172
  post.communities_ids = ActiveSupport::JSON.encode communities_ids
173
173
  post.save
174
174
 
175
175
  users_with_existing_items = [user.id]
176
- ::Inkwell::TimelineItem.where(:item_id => post.id, :is_comment => false).each do |item|
177
- users_with_existing_items << item.send(user_id_attr)
176
+ ::Inkwell::TimelineItem.where(:item_id => post.id, :item_type => ItemTypes::POST).each do |item|
177
+ users_with_existing_items << item.owner_id
178
178
  item.has_many_sources = true
179
179
  from_source = ActiveSupport::JSON.decode item.from_source
180
180
  from_source << Hash['community_id' => self.id]
@@ -184,7 +184,7 @@ module Inkwell
184
184
 
185
185
  self.users_row.each do |user_id|
186
186
  next if users_with_existing_items.include? user_id
187
- ::Inkwell::TimelineItem.create :item_id => post.id, user_id_attr => user_id, :is_comment => false,
187
+ ::Inkwell::TimelineItem.create :item_id => post.id, :owner_id => user_id, :owner_type => OwnerTypes::USER, :item_type => ItemTypes::POST,
188
188
  :from_source => ActiveSupport::JSON.encode([Hash['community_id' => self.id]])
189
189
  end
190
190
  end
@@ -208,13 +208,13 @@ module Inkwell
208
208
  end
209
209
  raise "post isn't in community" unless post.communities_row.include? self.id
210
210
 
211
- ::Inkwell::BlogItem.delete_all :owner_id => self.id, :is_owner_user => false, :item_id => post.id, :is_comment => false
211
+ ::Inkwell::BlogItem.delete_all :owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY, :item_id => post.id, :item_type => ItemTypes::POST
212
212
  communities_ids = ActiveSupport::JSON.decode post.communities_ids
213
213
  communities_ids.delete self.id
214
214
  post.communities_ids = ActiveSupport::JSON.encode communities_ids
215
215
  post.save
216
216
 
217
- items = ::Inkwell::TimelineItem.where(:item_id => post.id, :is_comment => false).where("from_source like '%{\"community_id\":#{self.id}%'")
217
+ items = ::Inkwell::TimelineItem.where(:item_id => post.id, :item_type => ItemTypes::POST).where("from_source like '%{\"community_id\":#{self.id}%'")
218
218
  items.where(:has_many_sources => false).delete_all
219
219
  items.where(:has_many_sources => true).each do |item|
220
220
  from_source = ActiveSupport::JSON.decode item.from_source
@@ -232,9 +232,9 @@ module Inkwell
232
232
  for_user = options[:for_user]
233
233
 
234
234
  if last_shown_obj_id
235
- blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :is_owner_user => false).where("created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
235
+ blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY).where("created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
236
236
  else
237
- blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :is_owner_user => false).order("created_at DESC").limit(limit)
237
+ blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY).order("created_at DESC").limit(limit)
238
238
  end
239
239
 
240
240
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
@@ -306,7 +306,7 @@ module Inkwell
306
306
  item.save
307
307
  end
308
308
 
309
- ::Inkwell::BlogItem.delete_all :owner_id => self.id, :is_owner_user => false
309
+ ::Inkwell::BlogItem.delete_all :owner_id => self.id, :owner_type => OwnerTypes::COMMUNITY
310
310
 
311
311
  end
312
312
  end
@@ -19,13 +19,15 @@ module Inkwell
19
19
  after_create :processing_a_post
20
20
  before_destroy :destroy_post_processing
21
21
 
22
- has_many :comments, :class_name => 'Inkwell::Comment'
23
-
24
22
  include ::Inkwell::ActsAsInkwellPost::InstanceMethods
23
+
25
24
  end
26
25
  end
27
26
 
28
27
  module InstanceMethods
28
+ require_relative '../common/base.rb'
29
+ include ::Inkwell::Constants
30
+
29
31
  def commentline(options = {})
30
32
  options.symbolize_keys!
31
33
  last_shown_comment_id = options[:last_shown_comment_id]
@@ -33,9 +35,9 @@ module Inkwell
33
35
  for_user = options[:for_user]
34
36
 
35
37
  if last_shown_comment_id
36
- comments = self.comments.where("created_at < ?", Inkwell::Comment.find(last_shown_comment_id).created_at).order("created_at DESC").limit(limit)
38
+ comments = ::Inkwell::Comment.where(:topmost_obj_id => self.id, :topmost_obj_type => ItemTypes::POST).where("created_at < ?", Inkwell::Comment.find(last_shown_comment_id).created_at).order("created_at DESC").limit(limit)
37
39
  else
38
- comments = self.comments.order("created_at DESC").limit(limit)
40
+ comments = ::Inkwell::Comment.where(:topmost_obj_id => self.id, :topmost_obj_type => ItemTypes::POST).order("created_at DESC").limit(limit)
39
41
  end
40
42
 
41
43
  if for_user
@@ -75,24 +77,23 @@ module Inkwell
75
77
  user_class = Object.const_get ::Inkwell::Engine::config.user_table.to_s.singularize.capitalize
76
78
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
77
79
  user = user_class.find self.send(user_id_attr)
78
- ::Inkwell::BlogItem.create :item_id => self.id, :is_reblog => false, :owner_id => self.send(user_id_attr), :is_owner_user => true, :is_comment => false
79
-
80
+ ::Inkwell::BlogItem.create :item_id => self.id, :is_reblog => false, :owner_id => self.send(user_id_attr), :owner_type => OwnerTypes::USER, :item_type => ItemTypes::POST
80
81
  user.followers_row.each do |user_id|
81
82
  encode_sources = [ Hash['user_id' => user.id, 'type' => 'following'] ]
82
- ::Inkwell::TimelineItem.create :item_id => self.id, user_id_attr => user_id, :is_comment => false,
83
+ ::Inkwell::TimelineItem.create :item_id => self.id, :owner_id => user_id, :owner_type => OwnerTypes::USER, :item_type => ItemTypes::POST,
83
84
  :from_source => ActiveSupport::JSON.encode(encode_sources)
84
85
  end
85
86
  end
86
87
 
87
88
  def destroy_post_processing
88
- ::Inkwell::TimelineItem.delete_all :item_id => self.id, :is_comment => false
89
- ::Inkwell::FavoriteItem.delete_all :item_id => self.id, :is_comment => false
90
- ::Inkwell::BlogItem.delete_all :item_id => self.id, :is_comment => false
91
- comments = self.comments
89
+ ::Inkwell::TimelineItem.delete_all :item_id => self.id, :item_type => ItemTypes::POST
90
+ ::Inkwell::FavoriteItem.delete_all :item_id => self.id, :item_type => ItemTypes::POST
91
+ ::Inkwell::BlogItem.delete_all :item_id => self.id, :item_type => ItemTypes::POST
92
+ comments = ::Inkwell::Comment.where(:topmost_obj_id => self.id, :topmost_obj_type => ItemTypes::POST)
92
93
  comments.each do |comment|
93
- ::Inkwell::TimelineItem.delete_all :item_id => comment.id, :is_comment => true
94
- ::Inkwell::FavoriteItem.delete_all :item_id => comment.id, :is_comment => true
95
- ::Inkwell::BlogItem.delete_all :item_id => comment.id, :is_comment => true
94
+ ::Inkwell::TimelineItem.delete_all :item_id => comment.id, :item_type => ItemTypes::COMMENT
95
+ ::Inkwell::FavoriteItem.delete_all :item_id => comment.id, :item_type => ItemTypes::COMMENT
96
+ ::Inkwell::BlogItem.delete_all :item_id => comment.id, :item_type => ItemTypes::COMMENT
96
97
  ::Inkwell::Comment.delete comment
97
98
  end
98
99
  end
@@ -11,14 +11,15 @@ module Inkwell
11
11
  module Config
12
12
  def acts_as_inkwell_user
13
13
  has_many :comments, :class_name => 'Inkwell::Comment'
14
- has_many :favorite_items, :class_name => 'Inkwell::FavoriteItem'
15
- has_many :timeline_items, :class_name => 'Inkwell::TimelineItem'
16
14
  include ::Inkwell::ActsAsInkwellUser::InstanceMethods
17
- include ::Inkwell::Common
18
15
  end
19
16
  end
20
17
 
21
18
  module InstanceMethods
19
+ require_relative '../common/base.rb'
20
+ include ::Inkwell::Constants
21
+ include ::Inkwell::Common
22
+
22
23
  def blogline(options = {})
23
24
  options.symbolize_keys!
24
25
  last_shown_obj_id = options[:last_shown_obj_id]
@@ -26,15 +27,15 @@ module Inkwell
26
27
  for_user = options[:for_user]
27
28
 
28
29
  if last_shown_obj_id
29
- blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :is_owner_user => true).where("created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
30
+ blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
30
31
  else
31
- blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :is_owner_user => true).order("created_at DESC").limit(limit)
32
+ blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
32
33
  end
33
34
 
34
35
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
35
36
  result = []
36
37
  blog_items.each do |item|
37
- if item.is_comment
38
+ if item.item_type == ItemTypes::COMMENT
38
39
  blog_obj = ::Inkwell::Comment.find item.item_id
39
40
  else
40
41
  blog_obj = post_class.find item.item_id
@@ -53,6 +54,17 @@ module Inkwell
53
54
  result
54
55
  end
55
56
 
57
+ def create_comment(options={})
58
+ options.symbolize_keys!
59
+ raise "for_object should be passed" unless options[:for_object]
60
+ raise "comment body should be passed" unless options[:body]
61
+ for_object = options[:for_object]
62
+ options[:topmost_obj_id] = for_object.id
63
+ options[:topmost_obj_type] = get_item_type for_object
64
+ options.delete :for_object
65
+ self.comments.create options
66
+ end
67
+
56
68
  def communities_row
57
69
  ActiveSupport::JSON.decode self.communities_ids
58
70
  end
@@ -60,7 +72,7 @@ module Inkwell
60
72
  def favorite(obj)
61
73
  return if self.favorite? obj
62
74
 
63
- FavoriteItem.create :item_id => obj.id, :user_id => self.id, :is_comment => is_comment(obj)
75
+ FavoriteItem.create :item_id => obj.id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => get_item_type(obj)
64
76
 
65
77
  users_ids_who_favorite_it = ActiveSupport::JSON.decode obj.users_ids_who_favorite_it
66
78
  users_ids_who_favorite_it << self.id
@@ -69,16 +81,13 @@ module Inkwell
69
81
  end
70
82
 
71
83
  def favorite?(obj)
72
- user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
73
- (FavoriteItem.send("find_by_item_id_and_is_comment_and_#{user_id_attr}", obj.id, is_comment(obj), self.id)) ? true : false
84
+ FavoriteItem.where(:item_id => obj.id, :item_type => get_item_type(obj), :owner_id => self.id, :owner_type => OwnerTypes::USER).first ? true : false
74
85
  end
75
86
 
76
87
  def unfavorite(obj)
77
88
  return unless self.favorite? obj
78
89
 
79
- user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
80
- record = FavoriteItem.send "find_by_item_id_and_is_comment_and_#{user_id_attr}", obj.id, is_comment(obj), self.id
81
- record.destroy
90
+ ::Inkwell::FavoriteItem.where(:item_id => obj.id, :item_type => get_item_type(obj), :owner_id => self.id, :owner_type => OwnerTypes::USER).destroy_all
82
91
 
83
92
  users_ids_who_favorite_it = ActiveSupport::JSON.decode obj.users_ids_who_favorite_it
84
93
  users_ids_who_favorite_it.delete self.id
@@ -93,15 +102,15 @@ module Inkwell
93
102
  for_user = options[:for_user]
94
103
 
95
104
  if last_shown_obj_id
96
- favorites = self.favorite_items.where("created_at < ?", Inkwell::FavoriteItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
105
+ favorites = ::Inkwell::FavoriteItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::FavoriteItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
97
106
  else
98
- favorites = self.favorite_items.order("created_at DESC").limit(limit)
107
+ favorites = ::Inkwell::FavoriteItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
99
108
  end
100
109
 
101
110
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
102
111
  result = []
103
112
  favorites.each do |item|
104
- if item.is_comment
113
+ if item.item_type == ItemTypes::COMMENT
105
114
  favorited_obj = ::Inkwell::Comment.find item.item_id
106
115
  else
107
116
  favorited_obj = post_class.find item.item_id
@@ -135,13 +144,13 @@ module Inkwell
135
144
 
136
145
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
137
146
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
138
- ::Inkwell::BlogItem.where(:owner_id => user.id, :is_owner_user => true).order("created_at DESC").limit(10).each do |blog_item|
147
+ ::Inkwell::BlogItem.where(:owner_id => user.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(10).each do |blog_item|
139
148
  if blog_item.is_reblog
140
- item_class = blog_item.is_comment? ? ::Inkwell::Comment : post_class
149
+ item_class = blog_item.item_type == ItemTypes::COMMENT ? ::Inkwell::Comment : post_class
141
150
  next if item_class.find(blog_item.item_id).send(user_id_attr) == self.id
142
151
  end
143
152
 
144
- item = ::Inkwell::TimelineItem.send "find_by_item_id_and_#{user_id_attr}_and_is_comment", blog_item.item_id, self.id, blog_item.is_comment
153
+ item = TimelineItem.where(:item_id => blog_item.item_id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => blog_item.item_type).first
145
154
  if item
146
155
  item.has_many_sources = true unless item.has_many_sources
147
156
  sources = ActiveSupport::JSON.decode item.from_source
@@ -159,7 +168,7 @@ module Inkwell
159
168
  else
160
169
  sources << Hash['user_id' => user.id, 'type' => 'following']
161
170
  end
162
- ::Inkwell::TimelineItem.create :item_id => blog_item.item_id, :is_comment => blog_item.is_comment, :user_id => self.id,
171
+ ::Inkwell::TimelineItem.create :item_id => blog_item.item_id, :item_type => blog_item.item_type, :owner_id => self.id, :owner_type => OwnerTypes::USER,
163
172
  :from_source => ActiveSupport::JSON.encode(sources), :created_at => blog_item.created_at
164
173
  end
165
174
  end
@@ -179,9 +188,7 @@ module Inkwell
179
188
  self.followings_ids = ActiveSupport::JSON.encode followings
180
189
  self.save
181
190
 
182
- user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
183
-
184
- timeline_items = ::Inkwell::TimelineItem.where "from_source like '%{\"user_id\":#{user.id}%' and #{user_id_attr} = #{self.id}"
191
+ timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where "from_source like '%{\"user_id\":#{user.id}%'"
185
192
  timeline_items.delete_all :has_many_sources => false
186
193
  timeline_items.each do |item|
187
194
  from_source = ActiveSupport::JSON.decode item.from_source
@@ -209,10 +216,10 @@ module Inkwell
209
216
  return if self.reblog? obj
210
217
  raise "User tries to reblog his post." if self.id == obj.user_id
211
218
 
212
- is_comment = is_comment(obj)
219
+ item_type = get_item_type(obj)
213
220
 
214
221
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
215
- BlogItem.create :item_id => obj.id, :is_reblog => true, :owner_id => self.id, :is_owner_user => true, :is_comment => is_comment
222
+ BlogItem.create :item_id => obj.id, :is_reblog => true, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => item_type
216
223
 
217
224
  users_ids_who_reblog_it = ActiveSupport::JSON.decode obj.users_ids_who_reblog_it
218
225
  users_ids_who_reblog_it << self.id
@@ -221,7 +228,7 @@ module Inkwell
221
228
 
222
229
  self.followers_row.each do |user_id|
223
230
  next if obj.send(user_id_attr) == user_id
224
- item = TimelineItem.send "find_by_item_id_and_#{user_id_attr}_and_is_comment", obj.id, user_id, is_comment
231
+ item = TimelineItem.where(:item_id => obj.id, :owner_id => user_id, :owner_type => OwnerTypes::USER, :item_type => item_type).first
225
232
  if item
226
233
  item.has_many_sources = true unless item.has_many_sources
227
234
  sources = ActiveSupport::JSON.decode item.from_source
@@ -230,30 +237,30 @@ module Inkwell
230
237
  item.save
231
238
  else
232
239
  encode_sources = ActiveSupport::JSON.encode [Hash['user_id' => self.id, 'type' => 'reblog']]
233
- TimelineItem.create :item_id => obj.id, :created_at => obj.created_at, user_id_attr => user_id, :from_source => encode_sources, :is_comment => is_comment
240
+ TimelineItem.create :item_id => obj.id, :created_at => obj.created_at, :owner_id => user_id, :owner_type => OwnerTypes::USER, :from_source => encode_sources, :item_type => item_type
234
241
  end
235
242
  end
236
243
  end
237
244
 
238
245
  def reblog?(obj)
239
- BlogItem.exists? :item_id => obj.id, :owner_id => self.id, :is_owner_user => true, :is_reblog => true, :is_comment => is_comment(obj)
246
+ BlogItem.exists? :item_id => obj.id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :is_reblog => true, :item_type => get_item_type(obj)
240
247
  end
241
248
 
242
249
  def unreblog(obj)
243
250
  return unless self.reblog? obj
244
251
  raise "User tries to unreblog his post." if self.id == obj.user_id
245
252
 
246
- is_comment = is_comment(obj)
253
+ item_type = get_item_type(obj)
247
254
 
248
255
  users_ids_who_reblog_it = ActiveSupport::JSON.decode obj.users_ids_who_reblog_it
249
256
  users_ids_who_reblog_it.delete self.id
250
257
  obj.users_ids_who_reblog_it = ActiveSupport::JSON.encode users_ids_who_reblog_it
251
258
  obj.save
252
259
 
253
- ::Inkwell::BlogItem.delete_all :owner_id => self.id, :is_owner_user => true, :item_id => obj.id, :is_reblog => true, :is_comment => is_comment
260
+ ::Inkwell::BlogItem.delete_all :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_id => obj.id, :is_reblog => true, :item_type => item_type
254
261
 
255
- TimelineItem.delete_all :user_id => self.followers_row, :has_many_sources => false, :item_id => obj.id, :is_comment => is_comment
256
- TimelineItem.where(:user_id => self.followers_row, :item_id => obj.id, :is_comment => is_comment).each do |item|
262
+ TimelineItem.delete_all :owner_id => self.followers_row, :owner_type => OwnerTypes::USER, :has_many_sources => false, :item_id => obj.id, :item_type => item_type
263
+ TimelineItem.where(:owner_id => self.followers_row, :owner_type => OwnerTypes::USER, :item_id => obj.id, :item_type => item_type).each do |item|
257
264
  sources = ActiveSupport::JSON.decode item.from_source
258
265
  sources.delete Hash['user_id' => self.id, 'type' => 'reblog']
259
266
  item.has_many_sources = false if sources.size < 2
@@ -269,15 +276,15 @@ module Inkwell
269
276
  for_user = options[:for_user]
270
277
 
271
278
  if last_shown_obj_id
272
- timeline_items = self.timeline_items.where("created_at < ?", Inkwell::TimelineItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
279
+ timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::TimelineItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
273
280
  else
274
- timeline_items = self.timeline_items.order("created_at DESC").limit(limit)
281
+ timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
275
282
  end
276
283
 
277
284
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
278
285
  result = []
279
286
  timeline_items.each do |item|
280
- if item.is_comment
287
+ if item.item_type == ItemTypes::COMMENT
281
288
  timeline_obj = ::Inkwell::Comment.find item.item_id
282
289
  else
283
290
  timeline_obj = post_class.find item.item_id