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

add support for use_sti_base_class option #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions lib/sequential/generator.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module Sequential
class Generator
attr_reader :record, :scope, :column, :start_at, :skip
attr_reader :record, :scope, :column, :start_at, :skip, :use_sti_base_class

def initialize(record, options = {})
@record = record
@scope = options[:scope]
@column = (options[:column] || :sequential_id).to_sym
@start_at = options[:start_at] || 1
@skip = options[:skip]
@record = record
@scope = options[:scope]
@column = (options[:column] || :sequential_id).to_sym
@start_at = options[:start_at] || 1
@skip = options[:skip]
@use_sti_base_class = options[:use_sti_base_class] || false
end

def set
unless id_set? || skip?
where_opts = {
model: record.class.name,
model: use_sti_base_class ? record.class.base_class.name : record.class.name,
column: column
}

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/models/administrator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Administrator < User
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Author < User
end
3 changes: 3 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
sequential column: :user_number, use_sti_base_class: true
end
10 changes: 9 additions & 1 deletion 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: 20131126174222) do
ActiveRecord::Schema.define(version: 20141128230544) do

create_table "comments", force: true do |t|
t.string "title"
Expand Down Expand Up @@ -59,4 +59,12 @@
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.string "password_hash"
t.string "type"
t.integer "user_number"
end

end
6 changes: 6 additions & 0 deletions test/sequential_include_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ class SequentialIncludeTest < ActiveSupport::TestCase
assert_equal 1, c.sequential_id
end

test 'use sti base class' do
admin = Administrator.create
author = Author.create
assert_equal 1, admin.user_number
assert_equal 2, author.user_number
end
end