Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/non user namespace #11

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ drafts = Message.drafts(current_user)
messages = drafts.restore_all
```

### Migration

#### 0.5.x

Starting from 0.5.x, you will be able to save drafts under a non `User` model as such:
```
message.save_draft(author)
```

If you are upgrading from previous versions, simply run `rails g drafting:migration` again to generate the mising migration files.

### Linking to parent instance

```ruby
Expand Down Expand Up @@ -100,7 +111,6 @@ message.save!
* The `user` argument can be nil if you don't want to distinguish between multiple users
* Saving draft stores the data via `Marshal.dump` and `Marshal.load`. If you don't like this or need some customization, you can override the instance methods `dump_to_draft` and `load_from_draft` (see source)


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions drafting.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "coveralls"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "appraisal"
spec.add_development_dependency "generator_spec"
end
12 changes: 10 additions & 2 deletions lib/drafting/base_class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ def has_drafts(options={})
unless parent_class.method_defined? :drafts
parent_class.class_eval do
def drafts(user)
Draft.where(user: user, parent: self)
Draft.where(
user: user,
user_type: user.try(:class).try(:name),
parent: self
)
end

def self.child_drafts(user)
Draft.where(user: user, parent_type: self.base_class.name)
Draft.where(
user: user,
user_type: user.try(:class).try(:name),
parent_type: self.base_class.name
)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/drafting/draft.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Draft < ActiveRecord::Base
belongs_to :user
belongs_to :user, polymorphic: true
belongs_to :parent, polymorphic: true

validates_presence_of :data, :target_type
Expand Down
3 changes: 2 additions & 1 deletion lib/drafting/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def save_draft(user=nil)

draft.data = dump_to_draft
draft.target_type = self.class.name
draft.user = user
draft.user_id = user.try(:id)
draft.user_type = user.try(:class).try(:name)
draft.parent = self.send(self.class.draft_parent) if self.class.draft_parent

result = draft.save
Expand Down
2 changes: 1 addition & 1 deletion lib/drafting/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Drafting
VERSION = "0.4.2"
VERSION = "0.5.0"
end
1 change: 1 addition & 0 deletions lib/generators/drafting/migration/migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MigrationGenerator < Rails::Generators::Base

def create_migration_file
migration_template 'migration.rb', 'db/migrate/drafting_migration.rb'
migration_template 'non_user_migration.rb', 'db/migrate/non_user_drafting_migration.rb'
end

def self.next_migration_number(dirname)
Expand Down
13 changes: 13 additions & 0 deletions lib/generators/drafting/migration/templates/non_user_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class NonUserDraftingMigration < Drafting::MIGRATION_BASE_CLASS
def self.up
add_column :drafts, :user_type, :string, index: true

# add in user_type for existing drafts table
# for migration from old version
Draft.update_all(user_type: 'User')
end

def self.down
remove_column :drafts, :user_type
end
end
10 changes: 10 additions & 0 deletions spec/drafting/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe Drafting::ClassMethods do
let(:user) { FactoryBot.create(:user) }
let(:admin_user) { FactoryBot.create(:admin_user) }
let(:topic) { FactoryBot.create(:topic) }
let(:message) { topic.messages.build user: user, content: 'foo' }
let(:page) { Page.new title: 'First post' }
Expand All @@ -20,6 +21,15 @@
expect(Page.drafts(nil).count).to eq(1)
expect(Page.drafts(nil).first).to be_a(Draft)
end

it 'should find Draft objects with non user' do
page.save_draft(admin_user)

expect(Page.drafts(admin_user).count).to eq(1)
expect(Page.drafts(admin_user).first).to be_a(Draft)

expect(Page.drafts(user).count).to eq(0)
end
end

describe 'from_draft' do
Expand Down
15 changes: 15 additions & 0 deletions spec/drafting/instance_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe Drafting::InstanceMethods do
let(:user) { FactoryBot.create(:user) }
let(:other_user) { FactoryBot.create(:user) }
let(:admin_user) { FactoryBot.create(:admin_user) }
let(:topic) { FactoryBot.create(:topic) }
let(:message) { topic.messages.build user: user, content: 'foo' }
let(:page) { Page.new title: 'First post' }
Expand Down Expand Up @@ -62,6 +63,20 @@
expect(draft.user_id).to eq(nil)
end

it 'should store Draft object for non user' do
expect {
result = page.save_draft(admin_user)

expect(result).to eq(true)
}.to change(Draft, :count).by(1).and \
change(Message, :count).by(0)

draft = Draft.find(page.draft_id)
expect(draft.user).to eq(admin_user)
expect(draft.user_type).to eq(admin_user.class.name)
expect(draft.user_id).to eq(admin_user.id)
end

it 'should store extra attributes to Draft' do
message.priority = 5
message.save_draft(user)
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/admin_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :admin_user do
name { 'Doe' }
end
end
54 changes: 54 additions & 0 deletions spec/lib/generators/drafting/migration/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'generator_spec'
require 'spec_helper'
require 'generators/drafting/migration/migration_generator'

module Drafting
describe MigrationGenerator, type: :generator do
root_dir = File.expand_path("../../../../../../tmp", __FILE__)
destination root_dir

describe 'new app' do
before :each do
prepare_destination
run_generator
end

it "creates two installation db migration" do
migration_files =
Dir.glob("#{root_dir}/db/migrate/*drafting*.rb").sort

assert_file migration_files[0],
/class DraftingMigration < Drafting::MIGRATION_BASE_CLASS/
assert_file migration_files[1],
/class NonUserDraftingMigration < Drafting::MIGRATION_BASE_CLASS/
end
end

describe 'existing app' do
before :each do
prepare_destination
run_generator
FileUtils.rm Dir.glob("#{root_dir}/db/migrate/*non_user_drafting_migration.rb")

migration_files =
Dir.glob("#{root_dir}/db/migrate/*drafting*.rb").sort
expect(migration_files.count).to eq 1
assert_file migration_files[0],
/class DraftingMigration < Drafting::MIGRATION_BASE_CLASS/

run_generator
end

it "creates only one more db migration" do
migration_files =
Dir.glob("#{root_dir}/db/migrate/*drafting*.rb").sort
expect(migration_files.count).to eq 2

assert_file migration_files[0],
/class DraftingMigration < Drafting::MIGRATION_BASE_CLASS/
assert_file migration_files[1],
/class NonUserDraftingMigration < Drafting::MIGRATION_BASE_CLASS/
end
end
end
end
3 changes: 3 additions & 0 deletions spec/models/admin_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AdminUser < ActiveRecord::Base
validates_presence_of :name
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'drafting'
require 'generators/drafting/migration/templates/migration.rb'
require 'generators/drafting/migration/templates/non_user_migration.rb'

require 'factory_bot'
FactoryBot.find_definitions

# Require some example models
require 'models/user'
require 'models/admin_user'
require 'models/topic'
require 'models/message'
require 'models/page'
Expand All @@ -32,6 +34,7 @@
RSpec.configure do |config|
config.after(:suite) do
SpecMigration.down
NonUserDraftingMigration.down
DraftingMigration.down
end
end
Expand All @@ -43,6 +46,7 @@ def setup_db
ActiveRecord::Migration.verbose = false

DraftingMigration.up
NonUserDraftingMigration.up
SpecMigration.up
end

Expand Down
5 changes: 5 additions & 0 deletions spec/support/spec_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def self.up
t.string :name, null: false
end

create_table :admin_users, force: true do |t|
t.string :name, null: false
end

create_table :authors, force: true do |t|
t.string :name, null: false
end
Expand Down Expand Up @@ -44,5 +48,6 @@ def self.down
drop_table :topics
drop_table :authors
drop_table :users
drop_table :admin_users
end
end