Skip to content

Commit

Permalink
Merge pull request #2 from mabeller/rake_install
Browse files Browse the repository at this point in the history
Installation generator
  • Loading branch information
mabeller committed Jul 1, 2015
2 parents d5967d7 + 5d1efcf commit 058bae2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 71 deletions.
89 changes: 52 additions & 37 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -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
=> #<Oat id: 1, created_at: "2015-06-09 23:13:41", updated_at: "2015-06-09 23:13:41">
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
=> #<Oat id: 1, created_at: "2015-06-09 23:13:41", updated_at: "2015-06-09 23:13:41">
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.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/toastr/next_migration_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def next_migration_number(dirname)
end
end
end
end
end
16 changes: 6 additions & 10 deletions lib/generators/toastr/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -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
end
4 changes: 0 additions & 4 deletions lib/tasks/toastr_tasks.rake

This file was deleted.

11 changes: 0 additions & 11 deletions test/dummy/db/migrate/20150609182601_create_toasts.rb

This file was deleted.

13 changes: 13 additions & 0 deletions test/dummy/db/migrate/20150630235854_create_toasts.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 9 additions & 6 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit 058bae2

Please sign in to comment.