app/models/inkwell/comment.rb in inkwell-1.0.5 vs app/models/inkwell/comment.rb in inkwell-1.1.1

- old
+ new

@@ -1,23 +1,25 @@ module Inkwell class Comment < ActiveRecord::Base - attr_accessible :body, :post_id, :parent_id + require_relative '../../../lib/common/base.rb' + include ::Inkwell::Constants + include ::Inkwell::Common + + attr_accessible :body, :parent_comment_id, :topmost_obj_id, :topmost_obj_type attr_accessor :is_reblogged attr_accessor :is_favorited attr_accessor :item_id_in_line attr_accessor :is_reblog_in_blogline attr_accessor :from_sources_in_timeline - validates :"#{::Inkwell::Engine::config.post_table.to_s.singularize}_id", :presence => true validates :"#{::Inkwell::Engine::config.user_table.to_s.singularize}_id", :presence => true validates :body, :presence => true after_create :processing_a_comment before_destroy :destroy_comment_processing belongs_to ::Inkwell::Engine::config.user_table.to_s.singularize - belongs_to ::Inkwell::Engine::config.post_table.to_s.singularize def commentline(options = {}) options.symbolize_keys! last_shown_comment_id = options[:last_shown_comment_id] limit = options[:limit] || 10 @@ -61,13 +63,13 @@ end protected def remove_info_from_upper_comments(comments_info) - return unless self.parent_id - parent_comment = ::Inkwell::Comment.find self.parent_id - raise "There is no comment with id = #{self.parent_id}" unless parent_comment + return unless self.parent_comment_id + parent_comment = ::Inkwell::Comment.find self.parent_comment_id + raise "There is no comment with id = #{self.parent_comment_id}" unless parent_comment users_ids_who_comment_it = ActiveSupport::JSON.decode parent_comment.users_ids_who_comment_it users_ids_who_comment_it -= comments_info parent_comment.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it parent_comment.save parent_comment.remove_info_from_upper_comments(comments_info) @@ -82,43 +84,39 @@ child_comments_ids_to_deleted << comment['comment_id'] end ::Inkwell::Comment.delete child_comments_ids_to_deleted comment_with_child_comments_ids_to_deleted = child_comments_ids_to_deleted << self.id - ::Inkwell::TimelineItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true - ::Inkwell::FavoriteItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true - ::Inkwell::BlogItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :is_comment => true + ::Inkwell::TimelineItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT + ::Inkwell::FavoriteItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT + ::Inkwell::BlogItem.delete_all :item_id => comment_with_child_comments_ids_to_deleted, :item_type => ItemTypes::COMMENT user_id = self.send("#{::Inkwell::Engine::config.user_table.to_s.singularize}_id") comment_with_child_comments_info = child_comments << Hash['user_id' => user_id, 'comment_id' => self.id] remove_info_from_upper_comments comment_with_child_comments_info - post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize - post_id_attr = "#{::Inkwell::Engine::config.post_table.to_s.singularize}_id" - parent_post = post_class.find self.send post_id_attr - users_ids_who_comment_it = ActiveSupport::JSON.decode parent_post.users_ids_who_comment_it + parent_obj = get_class_for_item_type(self.topmost_obj_type).find self.topmost_obj_id + users_ids_who_comment_it = ActiveSupport::JSON.decode parent_obj.users_ids_who_comment_it users_ids_who_comment_it -= comment_with_child_comments_info - parent_post.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it - parent_post.save + parent_obj.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it + parent_obj.save end def processing_a_comment - post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize - post_id_attr = "#{::Inkwell::Engine::config.post_table.to_s.singularize}_id" - parent_post = post_class.find self.send post_id_attr + parent_obj = get_class_for_item_type(self.topmost_obj_type).find self.topmost_obj_id user_id = self.send "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id" - users_ids_who_comment_it = ActiveSupport::JSON.decode parent_post.users_ids_who_comment_it + users_ids_who_comment_it = ActiveSupport::JSON.decode parent_obj.users_ids_who_comment_it users_ids_who_comment_it << Hash['user_id' => user_id, 'comment_id' => self.id] - parent_post.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it - parent_post.save + parent_obj.users_ids_who_comment_it = ActiveSupport::JSON.encode users_ids_who_comment_it + parent_obj.save add_user_info_to_upper_comments end def add_user_info_to_upper_comments - if self.parent_id - parent_comment = ::Inkwell::Comment.find self.parent_id - raise "Comment with id #{comment.parent_id} is not found" unless parent_comment + if self.parent_comment_id + parent_comment = ::Inkwell::Comment.find self.parent_comment_id + raise "Comment with id #{comment.parent_comment_id} is not found" unless parent_comment parent_upper_comments_tree = ActiveSupport::JSON.decode parent_comment.upper_comments_tree self_upper_comments_tree = parent_upper_comments_tree << parent_comment.id self.upper_comments_tree = ActiveSupport::JSON.encode self_upper_comments_tree user_id = self.send "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"