Skip to content
Browse files

- added thor file to generate migrationadded src_twitter_status_id an…

…d specs for its uniqueness, added
  • Loading branch information...
1 parent b6626bc commit f364e4baf1f432ce78cdc514c89c658953de673a @cyx cyx committed Mar 10, 2010
View
64 Thorfile
@@ -1,61 +1,7 @@
-class Monk < Thor
- include Thor::Actions
-
- desc "test", "Run all tests"
- def test
- verify_config(:test)
-
- $:.unshift File.join(File.dirname(__FILE__), "test")
-
- Dir['test/**/*_test.rb'].each do |file|
- load file unless file =~ /^-/
- end
+Dir[ 'lib/thor/*.thor' ].each do |thorfile|
+ begin
+ load thorfile
+ rescue LoadError
+ puts "Unable to load #{thorfile}"
end
-
- desc "stories", "Run user stories."
- method_option :pdf, :type => :boolean
- def stories
- $:.unshift(Dir.pwd, "test")
-
- ARGV << "-r"
- ARGV << (options[:pdf] ? "stories-pdf" : "stories")
- ARGV.delete("--pdf")
-
- Dir["test/stories/*_test.rb"].each do |file|
- load file
- end
- end
-
- desc "start ENV", "Start Monk in the supplied environment"
- def start(env = ENV["RACK_ENV"] || "development")
- verify_config(env)
-
- exec "env RACK_ENV=#{env} ruby init.rb"
- end
-
- desc "copy_example EXAMPLE, TARGET", "Copies an example file to its destination"
- def copy_example(example, target = target_file_for(example))
- File.exists?(target) ? return : say_status(:missing, target)
- File.exists?(example) ? copy_file(example, target) : say_status(:missing, example)
- end
-
-private
-
- def self.source_root
- File.dirname(__FILE__)
- end
-
- def target_file_for(example_file)
- example_file.sub(".example", "")
- end
-
- def verify_config(env)
- verify "config/settings.example.yml"
- verify "config/redis/#{env}.example.conf"
- end
-
- def verify(example)
- copy_example(example) unless File.exists?(target_file_for(example))
- end
-
end
View
3 app/models/item.rb
@@ -14,6 +14,9 @@ class Item < ActiveRecord::Base
:presence => true,
:numericality => { :allow_blank => true, :greater_than_or_equal_to => 0 }
+ validates :src_twitter_status_id,
+ :uniqueness => { :allow_blank => true }
+
scope :latest, order('id DESC')
scope :most_viewed, order('id DESC')
scope :most_liked, where('likes_count != 0').order('likes_count DESC')
View
2 app/routes/items.rb
@@ -5,7 +5,7 @@ class Main
haml :'items/index'
else
- haml "%h1 No Homepage yet"
+ haml :'home'
end
end
View
1 app/views/home.haml
@@ -0,0 +1 @@
+%h1 Find me in app/views/home.haml
View
11 db/migrate/20100310091736_add_src_twitter_status_id_to_items.rb
@@ -0,0 +1,11 @@
+class AddSrcTwitterStatusIdToItems < ActiveRecord::Migration
+ def self.up
+ add_column :items, :src_twitter_status_id, :integer
+ add_index :items, :src_twitter_status_id
+ end
+
+ def self.down
+ remove_index :items, :src_twitter_status_id
+ remove_column :items, :src_twitter_status_id
+ end
+end
View
8 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100305054208) do
+ActiveRecord::Schema.define(:version => 20100310091736) do
create_table "items", :force => true do |t|
t.integer "user_id"
@@ -26,10 +26,12 @@
t.datetime "photo_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "likes_count", :default => 0
- t.integer "views_count", :default => 0
+ t.integer "likes_count", :default => 0
+ t.integer "views_count", :default => 0
+ t.integer "src_twitter_status_id"
end
+ add_index "items", ["src_twitter_status_id"], :name => "index_items_on_src_twitter_status_id"
add_index "items", ["views_count"], :name => "index_items_on_views_count"
create_table "likes", :force => true do |t|
View
2 lib/tasks/database.rake
@@ -29,7 +29,7 @@ namespace :db do
if File.exists?(file)
load(file)
else
- abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded}
+ abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{ROOT_DIR}/config/boot.rb to limit the frameworks that will be loaded}
end
end
end
View
29 lib/thor/db.thor
@@ -0,0 +1,29 @@
+module Gen
+ class Migration < Thor::Group
+ include Thor::Actions
+
+ argument :name
+
+ def self.source_root
+ File.dirname(__FILE__)
+ end
+
+ def create_the_migration
+ template('templates/migration.tt', "db/migrate/#{timestamp}_#{filename}")
+ end
+
+ private
+ def camelized_name
+ name.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ end
+
+ def filename
+ "#{name}.rb"
+ end
+
+ def timestamp
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
+ end
+ end
+end
+
View
61 lib/thor/monk.thor
@@ -0,0 +1,61 @@
+class Monk < Thor
+ include Thor::Actions
+
+ desc "test", "Run all tests"
+ def test
+ verify_config(:test)
+
+ $:.unshift File.join(File.dirname(__FILE__), "test")
+
+ Dir['test/**/*_test.rb'].each do |file|
+ load file unless file =~ /^-/
+ end
+ end
+
+ desc "stories", "Run user stories."
+ method_option :pdf, :type => :boolean
+ def stories
+ $:.unshift(Dir.pwd, "test")
+
+ ARGV << "-r"
+ ARGV << (options[:pdf] ? "stories-pdf" : "stories")
+ ARGV.delete("--pdf")
+
+ Dir["test/stories/*_test.rb"].each do |file|
+ load file
+ end
+ end
+
+ desc "start ENV", "Start Monk in the supplied environment"
+ def start(env = ENV["RACK_ENV"] || "development")
+ verify_config(env)
+
+ exec "env RACK_ENV=#{env} ruby init.rb"
+ end
+
+ desc "copy_example EXAMPLE, TARGET", "Copies an example file to its destination"
+ def copy_example(example, target = target_file_for(example))
+ File.exists?(target) ? return : say_status(:missing, target)
+ File.exists?(example) ? copy_file(example, target) : say_status(:missing, example)
+ end
+
+private
+
+ def self.source_root
+ File.dirname(__FILE__)
+ end
+
+ def target_file_for(example_file)
+ example_file.sub(".example", "")
+ end
+
+ def verify_config(env)
+ verify "config/settings.example.yml"
+ verify "config/redis/#{env}.example.conf"
+ end
+
+ def verify(example)
+ copy_example(example) unless File.exists?(target_file_for(example))
+ end
+
+end
View
7 lib/thor/templates/migration.tt
@@ -0,0 +1,7 @@
+class <%= camelized_name %> < ActiveRecord::Migration
+ def self.up
+ end
+
+ def self.down
+ end
+end
View
28 spec/models/item_spec.rb
@@ -107,7 +107,7 @@
end
end
-describe "Item#broadcast_to_twitter" do
+describe Item, "#broadcast_to_twitter" do
before(:each) do
FakeWeb.register_uri(:post, "http://twitter.com/statuses/update.json",
:body => {:id => 123145}.to_json)
@@ -133,3 +133,29 @@
end
end
end
+
+describe Item, "with no src twitter status id" do
+ it "should be creatable" do
+ lambda {
+ Factory(:item)
+ }.should_not raise_error(ActiveRecord::RecordInvalid)
+ end
+end
+
+describe Item, "with a src twitter status id that doesn't exist yet" do
+ it "should be creatable" do
+ lambda {
+ Factory(:item, :src_twitter_status_id => 1001)
+ }.should_not raise_error(ActiveRecord::RecordInvalid)
+ end
+
+ context "when another item comes in with the same src twitter status id" do
+ it "should not be creatable" do
+ Factory(:item, :src_twitter_status_id => 1001)
+
+ lambda {
+ Factory(:item, :src_twitter_status_id => 1001)
+ }.should raise_error(ActiveRecord::RecordInvalid)
+ end
+ end
+end

0 comments on commit f364e4b

Please sign in to comment.
Something went wrong with that request. Please try again.