From 5d1efcff0a040967de5bf42efc76722af706dde8 Mon Sep 17 00:00:00 2001 From: Matt Beller Date: Tue, 30 Jun 2015 17:01:13 -0700 Subject: [PATCH] Installation generator --- README.rdoc | 89 +++++++++++-------- ...astr_generator.rb => install_generator.rb} | 3 +- .../toastr/next_migration_version.rb | 2 +- lib/generators/toastr/templates/migration.rb | 16 ++-- lib/tasks/toastr_tasks.rake | 4 - .../migrate/20150609182601_create_toasts.rb | 11 --- .../migrate/20150630235854_create_toasts.rb | 13 +++ test/dummy/db/schema.rb | 15 ++-- 8 files changed, 82 insertions(+), 71 deletions(-) rename lib/generators/toastr/{toastr_generator.rb => install_generator.rb} (80%) delete mode 100644 lib/tasks/toastr_tasks.rake delete mode 100644 test/dummy/db/migrate/20150609182601_create_toasts.rb create mode 100644 test/dummy/db/migrate/20150630235854_create_toasts.rb diff --git a/README.rdoc b/README.rdoc index 82dbfb6..e07fe29 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,64 +1,79 @@ = Toastr -Acts like standard cache, but kicks off a background job and serves stale data rather than blocking and recalculating. +Acts like standard cache, but instead of blocking and recalculating, it serves stale data and kicks off a job to refresh it in the background. == INSTALLATION 1. Install the gem -```ruby -gem 'toastr', github: 'drush/toastr' -``` + + ``` + gem 'toastr', github: 'drush/toastr' + ``` 2. Run the generator + ``` + rails generate toastr:install + ``` + 3. Migrate your database + ``` + rake db:migrate + ``` + == USAGE 1. Define an instance method on an ActiveRecord. -2. After defining the method, declare ```has_toast :method_name``` +2. After defining the method, declare + ```has_toast :method_name``` 3. Three options for expiring the cached data are available, shown below -```ruby -class Oat < ActiveRecord::Base + ``` + class Oat < ActiveRecord::Base + has_many :related_records - def breakfast - sleep 2 - {oat: :meal} - end - # by default only updates if this Oat instance is updated after the toast was calculated - has_toast :breakfast + def breakfast + sleep 2 + {oat: :meal} + end + # by default only updates if this Oat instance is updated after the toast was calculated + has_toast :breakfast - def daily_report - sleep 2 - {daily: :report} - end - # update if toast is older than 1 day - has_toast :daily_report, expire_in: 1.day + def daily_report + sleep 2 + {daily: :report} + end + # update if toast is older than 1 day + has_toast :daily_report, expire_in: 1.day + + def arbitrary_expiration + sleep 2 + {arbitrary: :expiration} + end + # update on arbitrary block. example updates toast if any of its :related_records + # have been updated since the toast was last updated + has_toast :special, expire_if: -> (toast) { toast.parent.related_records.pluck(:updated_at).max > toast.updated_at } - def arbitrary_expiration - sleep 2 - {arbitrary: :expiration} end - # update on arbitrary block. example updates toast every time it's accessed if - # parent object was created on 8th day of the month - has_toast :special, expire_if: -> (toast) { toast.parent.created_at.day == 8 } - -end - -2.1.5 :001 > @oat = Oat.create - => # -2.1.5 :002 > @oat.breakfast - => {:error=>"Data not yet available"} -2.1.5 :003 > @oat.breakfast # after waiting enough time for the toast to calculate - => {"oat"=>"meal", "toastr"=>{"elapsed"=>2.010508, "cached_at"=>"2015-06-09T16:14:25.701-07:00"}} -``` + ``` + + ``` + 2.1.5 :001 > @oat = Oat.create + => # + 2.1.5 :002 > @oat.breakfast + => {:error=>"Data not yet available"} + 2.1.5 :003 > @oat.breakfast # after waiting enough time for the toast to calculate + => {"oat"=>"meal", "toastr"=>{"elapsed"=>2.010508, "cached_at"=>"2015-06-09T16:14:25.701-07:00"}} + ``` + == TODO -* Basic test coverage -* Generator for migration +* Extended test coverage * How to resolve conflicts between job_state and cache_state when errors occur == CHANGELOG +* Installation generator +* Basic test coverage * Adopt ActiveJob support instead of DJ This project rocks and uses MIT-LICENSE. \ No newline at end of file diff --git a/lib/generators/toastr/toastr_generator.rb b/lib/generators/toastr/install_generator.rb similarity index 80% rename from lib/generators/toastr/toastr_generator.rb rename to lib/generators/toastr/install_generator.rb index 1b42818..f72fc3f 100644 --- a/lib/generators/toastr/toastr_generator.rb +++ b/lib/generators/toastr/install_generator.rb @@ -2,9 +2,8 @@ require 'rails/generators/migration' require 'rails/generators/active_record' -# Extend the DelayedJobGenerator so that it creates an AR migration module Toastr - class ActiveRecordGenerator < Rails::Generators::Base + class InstallGenerator < Rails::Generators::Base include Rails::Generators::Migration extend NextMigrationVersion diff --git a/lib/generators/toastr/next_migration_version.rb b/lib/generators/toastr/next_migration_version.rb index 414f5cb..49ac06b 100644 --- a/lib/generators/toastr/next_migration_version.rb +++ b/lib/generators/toastr/next_migration_version.rb @@ -9,4 +9,4 @@ def next_migration_number(dirname) end end end -end \ No newline at end of file +end diff --git a/lib/generators/toastr/templates/migration.rb b/lib/generators/toastr/templates/migration.rb index dfc24c0..2686073 100644 --- a/lib/generators/toastr/templates/migration.rb +++ b/lib/generators/toastr/templates/migration.rb @@ -1,17 +1,13 @@ class CreateToasts < ActiveRecord::Migration - def self.up + def change create_table :toastr_toasts, force: true do |t| - t.references :parent, polymorphic: true - t.string :category + t.references :parent, polymorphic: true, null: false, index: true + t.string :category, null: false t.text :cache_json - t.text :status + t.text :status, null: false t.timestamps null: true end - # add_index :toasts, :status, name: 'toasts_status' + add_index :toastr_toasts, [:parent_id, :parent_type, :category], name: 'toasts_parent_category' end - - def self.down - drop_table :toastr_toasts - end -end \ No newline at end of file +end diff --git a/lib/tasks/toastr_tasks.rake b/lib/tasks/toastr_tasks.rake deleted file mode 100644 index 1f654a9..0000000 --- a/lib/tasks/toastr_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :toastr do -# # Task goes here -# end diff --git a/test/dummy/db/migrate/20150609182601_create_toasts.rb b/test/dummy/db/migrate/20150609182601_create_toasts.rb deleted file mode 100644 index bf71ee2..0000000 --- a/test/dummy/db/migrate/20150609182601_create_toasts.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateToasts < ActiveRecord::Migration - def change - create_table :toastr_toasts, force: true do |t| - t.references :parent, polymorphic: true - t.string :category - t.jsonb :cache_json - t.text :status - t.timestamps null: true - end - end -end diff --git a/test/dummy/db/migrate/20150630235854_create_toasts.rb b/test/dummy/db/migrate/20150630235854_create_toasts.rb new file mode 100644 index 0000000..2686073 --- /dev/null +++ b/test/dummy/db/migrate/20150630235854_create_toasts.rb @@ -0,0 +1,13 @@ +class CreateToasts < ActiveRecord::Migration + def change + create_table :toastr_toasts, force: true do |t| + t.references :parent, polymorphic: true, null: false, index: true + t.string :category, null: false + t.text :cache_json + t.text :status, null: false + t.timestamps null: true + end + + add_index :toastr_toasts, [:parent_id, :parent_type, :category], name: 'toasts_parent_category' + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 5f5c907..e17a9fe 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150609182601) do +ActiveRecord::Schema.define(version: 20150630235854) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,13 +22,16 @@ end create_table "toastr_toasts", force: :cascade do |t| - t.integer "parent_id" - t.string "parent_type" - t.string "category" - t.jsonb "cache_json" - t.text "status" + t.integer "parent_id", null: false + t.string "parent_type", null: false + t.string "category", null: false + t.text "cache_json" + t.text "status", null: false t.datetime "created_at" t.datetime "updated_at" end + add_index "toastr_toasts", ["parent_id", "parent_type", "category"], name: "toasts_parent_category", using: :btree + add_index "toastr_toasts", ["parent_type", "parent_id"], name: "index_toastr_toasts_on_parent_type_and_parent_id", using: :btree + end