Module: Thredded::PostCommon Abstract
- Extended by:
- ActiveSupport::Concern
- Included in:
- Post, PrivatePost
- Defined in:
- app/models/concerns/thredded/post_common.rb
Overview
This module is abstract.
Classes that include this module are expected to implement #readers.
Instance Method Summary collapse
- #avatar_url ⇒ Object
- #calculate_page(postable_posts, per_page) ⇒ Object
-
#filtered_content(view_context, users_provider: ->(names) { readers_from_user_names(names) }, **options) ⇒ String
Formatted and sanitized html-safe post content.
- #first_post_in_topic? ⇒ Boolean
-
#mark_as_unread(user, page) ⇒ Object
Marks all the posts from the given one as unread for the given user.
- #previous_post ⇒ Object
-
#readers ⇒ ActiveRecord::Relation<Thredded.user_class>
abstract
Users from that can read this post.
-
#readers_from_user_names(user_names) ⇒ ActiveRecord::Relation<Thredded.user_class>
private
Users from the list of user names that can read this post.
Instance Method Details
#avatar_url ⇒ Object
24 25 26 |
# File 'app/models/concerns/thredded/post_common.rb', line 24 def avatar_url Thredded.avatar_url.call(user) end |
#calculate_page(postable_posts, per_page) ⇒ Object
28 29 30 |
# File 'app/models/concerns/thredded/post_common.rb', line 28 def calculate_page(postable_posts, per_page) 1 + postable_posts.where(postable_posts.arel_table[:created_at].lt(created_at)).count / per_page end |
#filtered_content(view_context, users_provider: ->(names) { readers_from_user_names(names) }, **options) ⇒ String
Returns formatted and sanitized html-safe post content.
34 35 36 |
# File 'app/models/concerns/thredded/post_common.rb', line 34 def filtered_content(view_context, users_provider: ->(names) { readers_from_user_names(names) }, **) Thredded::ContentFormatter.new(view_context, users_provider: users_provider, **).format_content(content) end |
#first_post_in_topic? ⇒ Boolean
38 39 40 |
# File 'app/models/concerns/thredded/post_common.rb', line 38 def first_post_in_topic? postable.first_post == self end |
#mark_as_unread(user, page) ⇒ Object
Marks all the posts from the given one as unread for the given user
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/concerns/thredded/post_common.rb', line 53 def mark_as_unread(user, page) if previous_post.nil? read_state = postable.user_read_states.find_by(user_id: user.id) read_state.destroy if read_state else read_state = postable.user_read_states.create_with( read_at: previous_post.created_at, page: page ).find_or_create_by(user_id: user.id) read_state.update_columns(read_at: previous_post.created_at, page: page) end end |
#previous_post ⇒ Object
66 67 68 |
# File 'app/models/concerns/thredded/post_common.rb', line 66 def previous_post @previous_post ||= postable.posts.order_newest_first.find_by('created_at < ?', created_at) end |
#readers ⇒ ActiveRecord::Relation<Thredded.user_class>
This method is abstract.
Returns users from that can read this post.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/concerns/thredded/post_common.rb', line 8 module PostCommon extend ActiveSupport::Concern included do paginates_per 50 delegate :email, to: :user, prefix: true, allow_nil: true validates :content, presence: true scope :order_oldest_first, -> { order(created_at: :asc, id: :asc) } scope :order_newest_first, -> { order(created_at: :desc, id: :desc) } before_validation :ensure_user_detail, on: :create end def avatar_url Thredded.avatar_url.call(user) end def calculate_page(postable_posts, per_page) 1 + postable_posts.where(postable_posts.arel_table[:created_at].lt(created_at)).count / per_page end # @param view_context [Object] the context of the rendering view. # @return [String] formatted and sanitized html-safe post content. def filtered_content(view_context, users_provider: ->(names) { readers_from_user_names(names) }, **) Thredded::ContentFormatter.new(view_context, users_provider: users_provider, **).format_content(content) end def first_post_in_topic? postable.first_post == self end # @return [ActiveRecord::Relation<Thredded.user_class>] users from the list of user names that can read this post. # @api private def readers_from_user_names(user_names) DbTextSearch::CaseInsensitive .new(readers, Thredded.user_name_column) .in(user_names) end # Marks all the posts from the given one as unread for the given user # @param user [Thredded.user_class] # @param page [Integer] def mark_as_unread(user, page) if previous_post.nil? read_state = postable.user_read_states.find_by(user_id: user.id) read_state.destroy if read_state else read_state = postable.user_read_states.create_with( read_at: previous_post.created_at, page: page ).find_or_create_by(user_id: user.id) read_state.update_columns(read_at: previous_post.created_at, page: page) end end def previous_post @previous_post ||= postable.posts.order_newest_first.find_by('created_at < ?', created_at) end private def ensure_user_detail build_user_detail if user && !user_detail end end |
#readers_from_user_names(user_names) ⇒ ActiveRecord::Relation<Thredded.user_class>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns users from the list of user names that can read this post.
44 45 46 47 48 |
# File 'app/models/concerns/thredded/post_common.rb', line 44 def readers_from_user_names(user_names) DbTextSearch::CaseInsensitive .new(readers, Thredded.user_name_column) .in(user_names) end |