Class: Krant::News
- Inherits:
-
Object
- Object
- Krant::News
- Defined in:
- lib/krant/news.rb
Overview
A collection of news items
Instance Attribute Summary collapse
- #scope ⇒ Object readonly private
Class Method Summary collapse
-
.about(scope) ⇒ Object
Create a news collection for the given scope.
Instance Method Summary collapse
- #all(for_user:) ⇒ Object private
-
#initialize(scope:) ⇒ News
constructor
private
A new instance of News.
-
#item(name, attributes) ⇒ Object
Define a new item that will be stored in the database on next ‘persist` call.
- #persist ⇒ Object private
- #seen_by!(user) ⇒ Object private
- #unseen_items?(for_user:) ⇒ Boolean private
Constructor Details
#initialize(scope:) ⇒ News
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 a new instance of News.
14 15 16 17 |
# File 'lib/krant/news.rb', line 14 def initialize(scope:) @templates = {} @scope = scope end |
Instance Attribute Details
#scope ⇒ Object (readonly)
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.
20 21 22 |
# File 'lib/krant/news.rb', line 20 def scope @scope end |
Class Method Details
.about(scope) ⇒ Object
Create a news collection for the given scope.
clashes between different news collections, when news item are persited to the database.
9 10 11 |
# File 'lib/krant/news.rb', line 9 def self.about(scope) new(scope: scope.to_s) end |
Instance Method Details
#all(for_user:) ⇒ Object
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.
69 70 71 72 73 74 75 76 |
# File 'lib/krant/news.rb', line 69 def all(for_user:) last_seen_at = last_seen(for_user) persisted_items.order('created_at desc').select do |news_item| news_item.seen = last_seen_at && news_item.created_at <= last_seen_at news_item.template = @templates[news_item.name] end end |
#item(name, attributes) ⇒ Object
Define a new item that will be stored in the database on next ‘persist` call. Usually called from an initializer, either in the main application or a plugin gem.
52 53 54 |
# File 'lib/krant/news.rb', line 52 def item(name, attributes) @templates[name.to_s] = attributes end |
#persist ⇒ Object
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.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/krant/news.rb', line 57 def persist @templates.each do |name, template| NewsItem.find_or_create_by(scope: @scope, name: name) do |news_item| if block_given? news_item.template = template yield news_item end end end end |
#seen_by!(user) ⇒ Object
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.
88 89 90 |
# File 'lib/krant/news.rb', line 88 def seen_by!(user) LastSeenState.find_or_create_by(scope: @scope, user: user).touch end |
#unseen_items?(for_user:) ⇒ Boolean
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.
79 80 81 82 83 84 85 |
# File 'lib/krant/news.rb', line 79 def unseen_items?(for_user:) last_seen_at = last_seen(for_user) scope = persisted_items scope = scope.where('created_at > ?', last_seen_at) if last_seen_at scope.exists? end |