Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
fixed a bug in has_settings, added an rss2-only (for now) feed reader…
Browse files Browse the repository at this point in the history
… page plugin.
  • Loading branch information
Josh Adams committed Dec 22, 2008
1 parent 0ffef94 commit 5aa6b5b
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -10,6 +10,7 @@
map.from_plugin :ansuz_scrollable_content
map.from_plugin :ansuz_testimonials
map.from_plugin :ansuz_form_builder
map.from_plugin :ansuz_feed_reader

map.resources :tags
map.resources :users
Expand Down
97 changes: 97 additions & 0 deletions public/javascripts/jquery.feedreader.js
@@ -0,0 +1,97 @@
/*
* A simple JQuery plugin for an RSS-fed news box
*
* @author Francesco Vivoli <f.vivoli@gmail.com> - http://atalayasec.org
* Based on code found on the JQuery mailing list
*/
(function($) {

/**
* Configure the news box container with url, maximum number of posts
* to be fetched and their text length.
* @example $('#newsbox').feedreader({
* targeturl: 'http://blogs.atalayasec.org/atalaya/?feed=rss2',
* items: 3,
* descLength: 15
* });
* @desc fill the #newsbox element with at most 3 posts taken from the above url, and showig
* a teaser of at most 15 words.
*
*/
$.fn.feedreader = function(options) {
var defaults = {
targeturl: 'http://blogs.atalayasec.org/atalaya/?feed=rss2',
items: 30,
descLength: 150
}
if(!options.targeturl) return false;
var opts = $.extend(defaults, options);
$(this).each(function(){
var container = this;
$.get(opts.targeturl,function(xml){
var posts=[];
var i=0;
$("item", xml).each(function(){
if(i>opts.items-1) return;
var post={};
$(this).find("link").each(function(){
post.link=getNodeText(this);
});
$(this).find("title").each(function(){
post.title=getNodeText(this);
});
$(this).find("pubDate").each(function(){
post.date=getNodeText(this);
});
$(this).find("description").each(function(){
var t=getNodeText(this);
post.desc=trimtext(t,opts.descLength)+'[...]';
});
posts[i++]=post;
});
writeposts(container,posts);

})
});

};

function trimtext(text,length){
var t = text.replace(/\s/g,' ');
var words = t.split(' ');
if(words.length<=length) return text;
var ret='';
for(var i=0;i<length;i++){
ret+=words[i]+' ';
}
return ret;
}

function writeposts(container,posts){
$(container).empty();
var html = '<dl>';
for(var k in posts){
html+=format(posts[k])
}
html += '</dl>';
$(container).append(html);
}

function format(post){
var html='<dt> <a href="'+post.link+'">'+post.title+'</a><br /></dt>';
html+='<dd>'+post.desc+'</dd>';
if(!post.title){
html = '';
}
return html;
}

function getNodeText(node)
{
var text = "";
if(node.text) text = node.text;
if(node.firstChild) text = node.firstChild.nodeValue;
return text;
}

})(jQuery);
@@ -0,0 +1,30 @@
class Admin::FeedReadersController < Admin::BaseController
unloadable

before_filter :load_feed_reader, :only => [ :show, :edit, :update ]

protected
def load_feed_reader
@feed_reader = Ansuz::JAdams::FeedReader.find params[:id]
end

public
def show
redirect_to edit_admin_feed_reader_path(@feed_reader)
end

def edit
end

def update
if @feed_reader.update_attributes(params[:feed_reader])
@feed_reader.settings = params[:settings]
@feed_reader.save
flash[:notice] = "The feed reader has been updated."
redirect_to edit_admin_feed_reader_path(@feed_reader)
else
flash.now[:error] = "There was a problem updating the feed reader."
render :action => "edit"
end
end
end
@@ -0,0 +1,13 @@
require 'open-uri'

class FeedReadersController < ApplicationController
unloadable

def get_feed
str = ""
open params[:targeturl] do |f|
str = f.read
end
render :xml => str
end
end
@@ -0,0 +1,23 @@
module Ansuz
module JAdams
class FeedReader < ActiveRecord::Base
has_settings

def edit_path
"/admin/feed_readers/#{id}"
end

def self.admin_partial
"/admin/feed_readers/feed_reader"
end

def self.view_partial
"/feed_readers/feed_reader"
end

def view_partial
self.class.view_partial
end
end
end
end
@@ -0,0 +1 @@
You should really just modify this plugin's content by clicking this link: <%= link_to "Feed Reader", admin_feed_reader_path(plugin_module) %>
@@ -0,0 +1,12 @@
<%= title "Edit Feed Reader" -%>
<% form_for :feed_reader, :url => admin_feed_reader_path(@feed_reader), :html => { :method => :put } do |f| %>
<div class='fullwidth'>
<table class='form-table'>
<%= form_row "Title", text_field_tag("settings[title]", @feed_reader.settings[:title]) -%>
<%= form_row "Feed URL", text_field_tag("settings[feed_url]", @feed_reader.settings[:feed_url]) -%>
</table>
</div>
<br />
<%= submit_tag "Update Feed Reader" %>
<% end %>
@@ -0,0 +1,4 @@
<h3><%= plugin_module.settings['title'] %></h3>
<div id="<%= dom_id(plugin_module) %>">
</div>
<%= javascript_tag "jQuery('##{dom_id(plugin_module)}').feedreader({ targeturl: '#{get_feed_feed_readers_url(:targeturl => plugin_module.settings['feed_url'])}'})" %>
@@ -0,0 +1,9 @@
class AddFeedReaders < ActiveRecord::Migration
def self.up
create_table "feed_readers" do |t|
end
end

def self.down
end
end
@@ -0,0 +1,11 @@
class AddNameToFeedReaders < ActiveRecord::Migration
def self.up
add_column :feed_readers, :name, :string
add_index :feed_readers, :name
end

def self.down
remove_index :feed_readers, :column => :name
remove_column :feed_readers, :name
end
end
1 change: 1 addition & 0 deletions vendor/plugins/ansuz_feed_reader/init.rb
@@ -0,0 +1 @@
Ansuz::PluginManagerInstance.register_plugin(Ansuz::JAdams::FeedReader)
4 changes: 4 additions & 0 deletions vendor/plugins/ansuz_feed_reader/routes.rb
@@ -0,0 +1,4 @@
namespace :admin do |admin|
admin.resources :feed_readers
end
resources :feed_readers, :collection => [:get_feed]
@@ -1,7 +1,9 @@
<%= title "Edit Photo Album" -%>
<% content_for :sidebar do -%>
<%= link_to "Back", admin_photo_album_path(@photo_album), :class => 'button icon back' %>
<% end -%>
<% form_for :photo_album, :url => admin_photo_album_path(@photo_album), :html => { :method => :put } do |f| %>
<div class='fullwidth'>
<table class='form-table'>
Expand Down
6 changes: 5 additions & 1 deletion vendor/plugins/has_settings/lib/has_settings.rb
Expand Up @@ -30,8 +30,12 @@ def has_settings
@the_has_settings_setting.settings
end

define_method "settings=" do |the_settings|
self.has_settings_setting.settings = the_settings
end

define_method "save_settings" do
has_settings_setting.save
get_has_settings_setting.save
end

define_method "get_has_settings_setting" do
Expand Down

0 comments on commit 5aa6b5b

Please sign in to comment.