Class: Krant::News

Inherits:
Object
  • Object
show all
Defined in:
lib/krant/news.rb

Overview

A collection of news items

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#scopeObject (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.

Parameters:

  • scope (Module|Class|String)

    Used to prevent naming



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.

Examples:

Add a news item

MyApp.news.item('some_plugin.social_share_links',
                title: {
                  en: 'Social Share Links',
                  de: 'Teilen Links'
                },
                text: {
                  en: 'Now displays links to [Twitter](http://twitter.com).'
                  en: 'Zeigt nun Links zu [Twitter](http://twitter.com).'
                })

Parameters:

  • name (String)

    Unique name for the item. It is good practice to use the name of the library or plugin as a prefix and separate different parts of the name with dots. Especially when multiple plugin gems define news for a common scope, this prevents naming clashes.

  • options (Hash)


52
53
54
# File 'lib/krant/news.rb', line 52

def item(name, attributes)
  @templates[name.to_s] = attributes
end

#persistObject

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.

Returns:

  • (Boolean)


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