Sha256: dd56add0086d8fb951eb3fe5d6764364ce64b89f77600ec70615b085ba642b1d
Contents?: true
Size: 1.84 KB
Versions: 2
Compression:
Stored size: 1.84 KB
Contents
module TheCommentsCommentable extend ActiveSupport::Concern included do has_many :comments, as: :commentable # *define_denormalize_flags* - should be placed before title or url builder filters before_validation :define_denormalize_flags after_save :denormalize_for_comments end # Default Denormalization methods # Overwrite it with your Application def commentable_title # My first blog post try(:title) || TheComments.config.default_title end def commentable_url # /pages/1 ['', self.class.to_s.tableize, self.to_param].join('/') end def commentable_state # 'draft' try(:state) end # Helper methods def comments_sum published_comments_count + draft_comments_count end def recalculate_comments_counters! self.draft_comments_count = comments.with_state(:draft).count self.published_comments_count = comments.with_state(:published).count self.deleted_comments_count = comments.with_state(:deleted).count save end private def define_denormalize_flags @trackable_commentable_title = commentable_title @trackable_commentable_state = commentable_state @trackable_commentable_url = commentable_url end # Can you make it better? I don't know how. def denormalization_fields_changed? @title_field_changed = @trackable_commentable_title != commentable_title @state_field_changed = @trackable_commentable_state != commentable_state @url_field_changed = @trackable_commentable_url != commentable_url @title_field_changed || @url_field_changed || @state_field_changed end def denormalize_for_comments if denormalization_fields_changed? comments.update_all({ commentable_title: commentable_title, commentable_state: commentable_state, commentable_url: commentable_url }) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
the_comments-1.1.0 | app/models/concerns/the_comments_commentable.rb |
the_comments-1.0.0 | app/models/concerns/the_comments_commentable.rb |