Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ActsAsNoteworthy improvements #16

Merged
merged 2 commits into from

2 participants

@NateBarnes
Collaborator

Alrighty, added some documentation to the ActsAsNoteworthy class, as well as adding migrations and models to the generators. The generators have been throughly tested under rails3, though not as much under rails2, as I'm not as familiar with it. Perhaps someone else would be so kind as to test these changes under rails2.

Let me know if anyone spots any big problems,and I'll fix them, if not I'll merge this in after a day or two has passed so that people can look it over.

@opengovernment opengovernment merged commit 033c522 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 generators/govkit/govkit_generator.rb
@@ -8,6 +8,10 @@ def manifest
record do |m|
m.directory File.join('config', 'initializers')
m.template 'govkit.rb', File.join('config', 'initializers', 'govkit.rb')
+ m.directory File.join('app', 'models')
+ m.template 'mention.rb', File.join('app', 'models', 'mention.rb')
+ m.directory File.join('db', 'migrate')
+ m.template 'create_mentions.rb', File.join('db', 'migrate', 'create_mentions.rb')
end
end
View
21 generators/govkit/templates/create_mentions.rb
@@ -0,0 +1,21 @@
+class CreateMentions < ActiveRecord::Migration
+ def self.up
+ create_table :mentions, :force => true do |t|
+ t.string "url", :limit => 8000
+ t.string "excerpt", :limit => 4000
+ t.string "title", :limit => 1000
+ t.string "source"
+ t.datetime "date"
+ t.float "weight"
+ t.integer "owner_id"
+ t.string "owner_type"
+ t.string "search_source"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+ end
+
+ def self.down
+ drop_table :mentions
+ end
+end
View
6 generators/govkit/templates/govkit.rb
@@ -11,13 +11,9 @@
# API key for NIMSP. Request one here:
# http://www.followthemoney.org/membership/settings.phtml
config.ftm_apikey = 'YOUR_FTM_API_KEY'
-
- # Api key for OpenCongress
- # http://www.opencongress.org/api
- config.opencongress_apikey = 'YOUR_OPENCONGRESS_API_KEY'
# Technorati API key
- config.technorati_apikey = 'YOUR_TECHNORATI_APIKEY'
+ # config.technorati_apikey = 'YOUR_TECHNORATI_APIKEY'
# Bing App ID
config.bing_appid = 'YOUR_BING_APPID'
View
15 generators/govkit/templates/mention.rb
@@ -0,0 +1,15 @@
+# A model to contain mentions of the :owner in the media
+class Mention < ActiveRecord::Base
+ belongs_to :owner, :polymorphic => true
+
+ scope :since, lambda { |d| where(["mentions.date > ?", d]) }
+
+ # Returns the mentions in JSON form
+ #
+ # @params [Hash] A hash of options
+ # @return The mentions in JSON form
+ def as_json(opts = {})
+ default_opts = {:except => [:owner_id, :owner_type]}
+ super(default_opts.merge(opts))
+ end
+end
View
2  govkit.gemspec
@@ -31,6 +31,8 @@ Gem::Specification.new do |s|
"init.rb",
"lib/generators/govkit/govkit_generator.rb",
"lib/generators/govkit/templates/govkit.rb",
+ "lib/generators/govkit/templates/mention.rb",
+ "lib/generators/govkit/templates/create_mentions.rb",
"lib/gov_kit.rb",
"lib/gov_kit/acts_as_noteworthy.rb",
"lib/gov_kit/configuration.rb",
View
8 lib/generators/govkit/govkit_generator.rb
@@ -1,17 +1,21 @@
require 'rails/generators'
+# Generator to setup rails app for using GovKit
class GovkitGenerator < Rails::Generators::Base
def initialize(*runtime_args)
super
end
- desc "Copies a config initializer to config/initializers/govkit.rb"
+ desc "Copies files necessary to use govkit"
source_root File.join(File.dirname(__FILE__), 'templates')
-
+
+ # Copies the files necessary to use govkit (initializer, migrations, and models)
def copy_initializer_file
copy_file 'govkit.rb', File.join('config', 'initializers', 'govkit.rb')
+ copy_file 'mention.rb', File.join('app', 'models', 'mention.rb')
+ copy_file 'create_mentions.rb', File.join('db', 'migrate', "#{ Time.now.utc.strftime "%Y%m%d%H%M%S" }create_mentions.rb")
end
end
View
21 lib/generators/govkit/templates/create_mentions.rb
@@ -0,0 +1,21 @@
+class CreateMentions < ActiveRecord::Migration
+ def self.up
+ create_table :mentions, :force => true do |t|
+ t.string "url", :limit => 8000
+ t.string "excerpt", :limit => 4000
+ t.string "title", :limit => 1000
+ t.string "source"
+ t.datetime "date"
+ t.float "weight"
+ t.integer "owner_id"
+ t.string "owner_type"
+ t.string "search_source"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+ end
+
+ def self.down
+ drop_table :mentions
+ end
+end
View
9 lib/generators/govkit/templates/govkit.rb
@@ -11,13 +11,12 @@
# API key for NIMSP. Request one here:
# http://www.followthemoney.org/membership/settings.phtml
config.ftm_apikey = 'YOUR_FTM_API_KEY'
-
- # Api key for OpenCongress
- # http://www.opencongress.org/api
- config.opencongress_apikey = 'YOUR_OPENCONGRESS_API_KEY'
# Technorati API key
- config.technorati_apikey = 'YOUR_TECHNORATI_APIKEY'
+ # config.technorati_apikey = 'YOUR_TECHNORATI_APIKEY'
+
+ # Bing App ID
+ config.bing_appid = 'YOUR_BING_APPID'
# Other things you could set here include alternate URLs for
# the APIs. See GovKit::Configuration for available attributes.
View
15 lib/generators/govkit/templates/mention.rb
@@ -0,0 +1,15 @@
+# A model to contain mentions of the :owner in the media
+class Mention < ActiveRecord::Base
+ belongs_to :owner, :polymorphic => true
+
+ scope :since, lambda { |d| where(["mentions.date > ?", d]) }
+
+ # Returns the mentions in JSON form
+ #
+ # @params [Hash] A hash of options
+ # @return The mentions in JSON form
+ def as_json(opts = {})
+ default_opts = {:except => [:owner_id, :owner_type]}
+ super(default_opts.merge(opts))
+ end
+end
View
13 lib/gov_kit/acts_as_noteworthy.rb
@@ -4,7 +4,11 @@ def self.included(base)
base.extend ActMethods
end
+ # Module to make a rails model act as a noteworthy object, linking it to govkit's mention model
module ActMethods
+ # Sets up the relationship between the model and the mention model
+ #
+ # @param [Hash] opts a hash of options to be used by the relationship
def acts_as_noteworthy(options={})
class_inheritable_accessor :options
self.options = options
@@ -16,7 +20,7 @@ def acts_as_noteworthy(options={})
with_options :as => :owner, :class_name => "Mention" do |c|
c.has_many :google_news_mentions, :conditions => {:search_source => "Google News"}, :order => 'date desc'
c.has_many :google_blog_mentions, :conditions => {:search_source => "Google Blogs"}, :order => 'date desc'
- c.has_many :technorati_mentions, :conditions => {:search_source => "Technorati"}, :order => 'date desc'
+# c.has_many :technorati_mentions, :conditions => {:search_source => "Technorati"}, :order => 'date desc'
c.has_many :bing_mentions, :conditions => {:search_source => "Bing"}, :order => 'date desc'
end
end
@@ -29,9 +33,12 @@ def acts_as_noteworthy(options={})
module ClassMethods
end
-
+
+ # A module that adds methods to individual instances of the noteworthy class.
module InstanceMethods
-
+ # Generates the raw mentions to be loaded into the Mention objects
+ #
+ # @return [Hash] a hash of all the mentions found of the object in question.
def raw_mentions
opts = self.options.clone
attributes = opts.delete(:with)
Something went wrong with that request. Please try again.