Feeder
Feeder gives you a mountable engine that provides a route to a feed page in your Ruby on Rails application.
Installation
Add this line to your application's Gemfile:
gem 'feeder'
And then execute:
$ bundle
Or install it yourself as:
$ gem install feeder
Install the migrations:
rake feeder:install:migrations
Run the migrations:
rake db:migrate
Usage
To make Feeder available, mount it to a route by adding the following somewhere in your config/routes.rb:
mount Feeder::Engine => "/feed"
You will now be able to display a feed on /feed in your application. In order
for Feeder to display anything in your feed, however, you will need to make
views per item type in the feed. Feeder looks up these views in
app/views/feeder/types by default, and then checks for a partial with the same
name as your item type. As an example, if you have a Message
model that you
wish to list out on your feed, you would make a file called _message.html.erb
in app/views/feeder/types.
Feeder also comes with an observer for automatically generating wrapper items
for your feedables (e.g. messages). In order to use it, you only need to register
Feeder::FeedableObserver
into your app, which can be done in
config/application.rb like this:
config.active_record.observers = [ 'Feeder::FeedableObserver' ]
Then, all you need to do is tell Feeder what to observe, which is done through an initializer, like this:
Feeder.configure do |config|
config.observe Message
end
... and declare that your Message
model is feedable:
class Message < ActiveRecord::Base
feedable
end
If you don't want to publish every message in the feed, you can supply a condition
to observe
:
Feeder.configure do |config|
config.observe Message, if: -> { .show_in_feed? }
end
Pretty neat.
Filtering
Want to filter out what feedables to display in your feed? We've got you covered
through the all-powerful filter
scope! Give it a hash of feedables and the
IDs that you want to fetch, and Feeder makes sure to only return feed items with
the specified feedables. You may also pass in the symbol :all
instead of a
list of IDs, which would fetch each of them. For example: say you have the
following feedables:
ShortMessage
Tweet
NewsArticle
To get Feeder::Item
s with news articles having IDs [1, 2, 3, 4, 5]
, tweets
[2, 4, 6, 7]
and all short message, you could do like this:
Feeder::Item.filter(
NewsArticle => [1, 2, 3, 4, 5],
Tweet => [2, 4, 6, 7],
ShortMessage => :all,
)
NOTE: The filter
scope is exclusive, meaning that anything you do not
pass in to it will also not be brought back. With the above feedables, if you
only want short messages [1, 3, 4]
, but all of the tweets and news articles,
you would have to specify them as well, like this:
Feeder::Item.filter(
ShortMessage => [1, 3, 4],
Tweet => :all,
NewsArticle => :all
)
The following would only return feed items with short messages:
Feeder::Item.filter(ShortMessage => [1, 3, 4])
Configuration
Feeder.configure do |config|
# A list of scopes that will be applied to the feed items in the controller.
config.scopes << proc { limit 5 }
end
Stickies
You can "sticky" messages in your feed so they're pinned at the top regardless of when
they were created. Just set the sticky
attribute and Feeder will take care of the rest.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Credits
Hyper made this. We're a digital communications agency with a passion for good code, and if you're using this library we probably want to hire you.
License
Feeder is available under the MIT license. See the MIT-LICENSE file for more info.